I am trying to implement the functionality to resume a large file
download, but have not found how to start downloading from a position
that is not at the beginning of an InputStream. I am currently using
the InputStream.skip() method to go to the position where I want to
resume the download, but have found that this method actually reads
all the data over the network and then throws it away.
I would appreciate some help in learning the correct way to start
reading from the resume offset of the internet file being downloaded,
so that I can avoid wasting the phone network bandwidth, and also
avoid the extra time delay caused by re-downloading all the data that
was already previously downloaded.
My current code is as follows:
File file = new File(mLocalFileName);
URL url = new URL(mInternetFileName);
URLConnection connection = url.openConnection();
connection.connect();
mInternetFileSize = connection.getContentLength();
if (!file.exists()) {
file.createNewFile();
mLocalFileSize = 0;
} else {
mLocalFileSize = file.length();
}
InputStream inputStream = connection.getInputStream();
if (mLocalFileSize > 0) {
long amountSkipped = 0;
do {
amountSkipped += inputStream.skip(
mLocalFileSize - amountSkipped);
} while (amountSkipped < mLocalFileSize);
}
FileOutputStream outputStream = new FileOutputStream(file,
mLocalFileSize > 0);
byte buffer[] = new byte[2048];
do {
int bytesRead = inputStream.read(buffer);
if (bytesRead <= 0) {
break;
}
outputStream.write(buffer, 0, bytesRead);
mLocalFileSize += bytesRead;
} while (!mCancelDownloads);
inputStream.close();
outputStream.close();
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---