trying to download using "in/out streams" with pause/resume functionality. the problem is when i setrequestproperty to the amount i downloaded : connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); and read the length : int fileLength = connection.getContentLength(); it gives me the right amount but in the download code(while loop) it downloads the full size of the file even though i set the correct request.
example: it starts downloading fine
total:2048 data length:1570024
07-21 11:26:17.650 D: zz path:v_3_1 total:4096 file length(should be full):1570024
07-21 11:26:17.650 D: zz path:v_3_1 total:6144 file length(should be full):1570024
07-21 11:26:17.651 D: zz path:v_3_1 total:8192 file length(should be full):1570024
07-21 11:26:17.652 D: zz path:v_3_1 total:10240 file length(should be full):1570024
07-21 11:26:17.652 D: zz path:v_3_1 total:12288 file length(should be full):1570024
07-21 11:26:17.653 D: zz path:v_3_1 total:14336 file length(should be full):1570024
07-21 11:26:17.653 D: zz path:v_3_1 total:16384 file length(should be full):1570024
here i pause and then resume it(works fine):
data length is:830696
07-21 11:26:30.949 D: zz path:v_3_1 total:741376 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:743424 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:745472 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:747520 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:749568 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:751616 file length(should be full):1570024
07-21 11:26:30.951 D: zz path:v_3_1 total:753664 file length(should be full):1570024
07-21 11:26:30.951 D: zz path:v_3_1 total:755712 file length(should be full):1570024
07-21 11:26:31.070 D: zz path:v_3_1 total:757760 file length(should be full):1570024
07-21 11:26:31.071 D: zz path:v_3_1 total:759808 file length(should be full):1570024
07-21 11:26:31.074 D: zz path:v_3_1 total:761856 file length(should be full):1570024
07-21 11:26:31.076 D: zz path:v_3_1 total:763904 file length(should be full):1570024
07-21 11:26:31.077 D: zz path:v_3_1 total:765952 file length(should be full):1570024
07-21 11:26:31.078 D: zz path:v_3_1 total:768000 file length(should be full):1570024
but then it goes past the full size of the file and stops at 1570024(full size)+739328(size before i paused)... it should only go up to 1570024 and stop.
07-21 11:26:32.125 D: zz path:v_3_1 total:1750816 file length(should be full):1570024
code:
try {
URL url = new URL(sUrl[1]);
URLConnection connection = url.openConnection();
if(resume){downloaded=viv.getIsdown2(path);
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
int fileLength = connection.getContentLength();
Log.d("dt", "zz "+path+" data length is:" + fileLength);
fos = new FileOutputStream(outfile);
fis = new FileInputStream(outfile);
encipher = Cipher.getInstance("AES");
encipher.init(Cipher.ENCRYPT_MODE, secretKey);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(outfile);
cis = new CipherInputStream(fis, encipher);
byte data[] = new byte[8 * 1024];
total = downloaded;
// total = 0;
fileLength+=downloaded;
int count;
while ((count = input.read(data)) !=-1) {
total += count;
progpercent = (int) (total * 100 / fileLength);
publishProgress(progpercent, total); Log.d("dt", "zz path:"+path+" total:" + total+" file length(should be full):" + fileLength);
output.write(data, 0, count);
if(isCancelled())break;
}
output.flush();
output.close();
input.close();
UPDATE: now that i figured out what was the answer of my main question, another problem is that even using append=true in fileoutputStream the written file overwrites current one i dont know why ... any suggestions ?