5
\$\begingroup\$

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();
                }
            }

        }
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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... \$\endgroup\$
    – Vogel612
    Apr 9, 2014 at 12:01
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Marc-Andre
    Apr 9, 2014 at 13:04
3
\$\begingroup\$

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();
}
\$\endgroup\$
8
  • \$\begingroup\$ resource means ?? \$\endgroup\$
    – Code_Life
    Apr 9, 2014 at 11:58
  • \$\begingroup\$ @MohitSharma about everything you .open(); and .close(); \$\endgroup\$
    – Vogel612
    Apr 9, 2014 at 11:59
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Marc-Andre
    Apr 9, 2014 at 13:03
  • 1
    \$\begingroup\$ @MohitSharma - Android is based on Java6 - no try-with-resources. \$\endgroup\$
    – rolfl
    Apr 9, 2014 at 13:20
  • 1
    \$\begingroup\$ @MohitSharma - actually, I may be wrong - but it is complicated. \$\endgroup\$
    – rolfl
    Apr 9, 2014 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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