0

I know this question might sound really basic for most of you. I need to download a large file from server. The first line of this file contains a time tag. I want to download entire file only if my time tag mismatches to that of file. For this I'm using the given code. However, I'm not sure if this actually prevents file from uselessly downloading entire file.

Please help me out !

public String downloadString(String url,String myTime)
{
    try {
           URL url1 = new URL(url);
           URLConnection tc = url1.openConnection();
           tc.setConnectTimeout(timeout);
           tc.setReadTimeout(timeout);
           BufferedReader br = new BufferedReader(new InputStreamReader(tc.getInputStream()));
           StringBuilder sb = new StringBuilder();
           String line;
           while ((line = br.readLine()) != null) {

                    if(line.contains(myTime))
                    {
                        Log.d("TIME CHECK", "Article already updated");
                        break;
                    }
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
        }
    catch(Exception e)
    {
        Log.d("Error","In JSON downloading");
    }

    return null;
}
Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44

3 Answers3

3

No, there is no easy way to control exactly to the last byte what will be downloaded. Even at the Java level you are involving a BufferedReader, which will obviously download more than you ask for, buffering it. There are other buffers as well, including at the OS level, which you cannot control. The proper technique to download only new files with HTTP is to use the IfModifiedSince header.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • I'm sorry its not actually a file. Its rather a webpage containing json. Now can we read it line by line instead of downloading it completely ? – Gaurav Arora Nov 30 '12 at 10:01
0

Your code won't download the whole file but as the BufferedReader has a default buffer size of 8192 you will read at least that many characters.

pillingworth
  • 3,238
  • 2
  • 24
  • 49
-1

You can go byte-by-byte or chunk-by-chunk if it is the size

BufferedInputStream in = new BufferedInputStream(url).openStream())
byte data[] = new byte[1024];
int count;
while((count = in.read(data,0,1024)) != -1)
{
    out.write(data, 0, count);
}

Check this question please

How to download and save a file from Internet using Java?

Community
  • 1
  • 1