Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is piece of code is using to download images. Can somebody tell me how to optimize this code to decrease download time for each image?

        URL url;
        HttpURLConnection connection = null;
        InputStream input = null;
        System.setProperty("http.keepAlive", "true");
        try {
            url = new URL(urlString);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(HTTP_CONNECTION_TIMEOUT);
            connection.setRequestProperty("Connection", "Keep-Alive");
            input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            currentRequestInProgress.remove(urlString);
            if (connection != null)
                connection.disconnect();
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
share|improve this question
add comment

2 Answers 2

Buffered streams usually result in greatly improved performance:

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

The buffer size is 8kb per default, tuning it may be an option (but not without measuring).

And, if you download multiple images: parallelize it with multiple threads (most of the time is consumed by I/O waits anyway), this usually improves performance by factors.

share|improve this answer
2  
Thanks. Will try this and will share the stats –  Mohit Sharma Apr 9 at 11:59
    
Keep in mind, this is tagged android. I thus suspect you have a very limited bandwidth. Your CPU waiting times don't really decrease when you try to squeeze more through the bandwith... –  Vogel612 Apr 9 at 12:01
2  
Good catch, from developer.android.com/reference/java/net/HttpURLConnection.html : The input and output streams returned by this class are not buffered. Most callers should wrap the returned streams with BufferedInputStream or BufferedOutputStream. Callers that do only bulk reads or writes may omit buffering. –  Marc-Andre Apr 9 at 13:04
add comment

Resources:

You should really use try-with-resources:

URL url = new URL(urlstring);
System.setProperty("http.keepAlive","true");
try(HTTPUrlConnection connection = (HTTPUrlConnection) url.openConnection()){
    connection.setConnectTimeout(HTTP_CONNECTION_TIMEOUT);
    connection.setRequestProperty("Connection", "Keep-Alive");
    try(InputStream input = connection.getInputStream()){
       return BitmapFactory.decodeStream(input);
    }
    catch(IOException e){
       e.printStackTrace();
       return null;
    }
}
catch(MalformedURLException e){
    e.printStackTrace();
    return null;
}

This code should do exactly the same. The finally becomes useless, as try-with-resources does the job for you. Keep in mind, your resources should implement java.lang.AutoCloseable, this is required to use try-with-resources.

As mentioned in some comments:

Try-with-resources works only as of Android 4.4.

furthermore, if HTTPUrlConnection does not implement AutoCloseable you will have to restructure the code like this:

HTTPUrlConnection connection
try{
    connection = (HTTPUrlConnection) url.openConnection();
    //[...]
} 
catch(MalformedURLException e){
    e.printStackTrace();
    return null;
}
finally {
    if(connection != null)
        connection.disconnect();
}
share|improve this answer
    
resource means ?? –  Mohit Sharma Apr 9 at 11:58
    
@MohitSharma about everything you .open(); and .close(); –  Vogel612 Apr 9 at 11:59
1  
This is a good advice in general but I don't think HTTPUrlConnection implements Autocloseable. There is a connection.disconnect() in the Op code, which is not in this code. –  Marc-Andre Apr 9 at 13:03
1  
@MohitSharma - Android is based on Java6 - no try-with-resources. –  rolfl Apr 9 at 13:20
1  
@MohitSharma - actually, I may be wrong - but it is complicated. –  rolfl Apr 9 at 13:23
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.