Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed critical issue in copying large files #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

l3utterfly
Copy link
Contributor

Critical issue in android for copying large files: previous version downloads the large file but does not copy anything over 2GB.

Sorry! This PR fixes it.

@kesha-antonov
Copy link
Owner

Is it working on android 7.0?

@kesha-antonov
Copy link
Owner

https://developer.android.com/reference/java/nio/file/package-summary

Added in API level 26
java.nio.file

I previously added this API but changed to FileChannel because we need to support android 7.0+ (api 24+)

@l3utterfly
Copy link
Contributor Author

Hmm.. my app has min version 26 so this was not a problem.

How about this code?

private static void moveFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        long bytesTransferred = 0;
        long totalBytes = inChannel.size();
        while (bytesTransferred < totalBytes) {
            long remainingBytes = totalBytes - bytesTransferred;
            long chunkSize = Math.min(remainingBytes, Integer.MAX_VALUE);
            long transferredBytes = inChannel.transferTo(bytesTransferred, chunkSize, outChannel);
            bytesTransferred += transferredBytes;
        }
        src.delete();
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

(untested, but should work). The main problem with the previous code is that transferTo takes an Int, which cannot go past 2GB

@kesha-antonov
Copy link
Owner

Could you test in android emulator with api 24?

I can do it also but on Wednesday.

@Prashant13Pawar
Copy link

Not able to download files above 1GB . Please provide fix for this

@l3utterfly
Copy link
Contributor Author

Did you try the code in the PR? I tested it and it works for files over 4GB with no problems

@Prashant13Pawar
Copy link

I tried code from PR still same issue

@l3utterfly
Copy link
Contributor Author

Hmm.. not sure, will take a look later. The code works fine, and it is already in production, so not sure what's wrong with your version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants