I use okhttp library for download files in android. I download successfully. But something is wrong when I pause and resume download.
Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();
File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
input.skip(downloadedSize);
} else {
output = new FileOutputStream(file, false);
}
long totalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
downloadedSize += count;
output.write(data, 0, count);
}
The problem is that for example size of file is 10MB. I pause when it downloaded 3MB and then resume to download and when download is finish size of file become 13MB. It doesnt start from downloaded size on resume, it start download from begining of bytestream. so file become 13MB. What is wrong with code?