0
votes

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 ?

2

2 Answers

0
votes

Keep track of size already downloaded before pausing the download and in your on resume function, you can ensure that you subtract the amount already downloaded before pause from the total amount to get the amount left to download.

connection.setRequestProperty("Range", "bytes=" + (total - downloaded) + "-");}
0
votes

THANKFULLY i found the problem : changed this because it seems it doesnt send the request property

InputStream input = new BufferedInputStream(url.openStream());

to this

InputStream input = new BufferedInputStream(connection.getInputStream());

which worked

07-21 18:17:59.336 D: zz    path:v_3_2      total:1914716      file length(should be full):1916873
07-21 18:17:59.336 D: zz    path:v_3_2      total:1916764      file length(should be full):1916873
07-21 18:17:59.338 D: zz    path:v_3_2      total:1916873      file length(should be full):1916873