Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Does this piece of code make sense? The idea is to throw any exceptions that may occur but always run the finally block to close the streams.

private void streamToFile(HttpResponse transferResponse) throws Exception {
    OutputStream output = null;
    InputStream input = null;
    try {
        input = new BufferedInputStream(transferResponse.getEntity().getContent());
        output = new FileOutputStream(transferFile);
        final int BUFFER_SIZE = 1024*10;
        byte data[] = new byte[BUFFER_SIZE];
        int count;
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
            publishProgress(count);
            remainingBytes = remainingBytes - count;
        }
    } finally {
        input.close();
        output.close();
    }
}
share|improve this question
2  
This is what a finally block is for. – Konrad Rudolph Jul 9 '12 at 14:29
It runs the code in all circumstances. ie weather the exception is thrown or not – jiduvah Jul 9 '12 at 15:03
My comment wasn’t a question, it was a statement. – Konrad Rudolph Jul 9 '12 at 15:19
Sorry Konrad, its not so clear. What is? – jiduvah Jul 9 '12 at 15:30

2 Answers

up vote 6 down vote accepted

You should use the closeQuietly methods of IOUtils (Apache Commons IO) (or similar ones). It ignores exceptions and accepts null values.

Its implementation is just a few lines of code, so you can copy it directly to your Android application without the jar file:

public static void closeQuietly(Closeable closeable) {
    try {
        if (closeable != null) {
            closeable.close();
        }
    } catch (IOException ioe) {
        // ignore
    }
}

It accepts null values which means that you can call it with null, the closeQuietly will handle that and you don't have to check null values in the finally block:

} finally {
    if (input != null) {
        closeQuietly(input);
    }
    if (output != null) {
        closeQuietly(output);
    }
}

Instead of this the following is completely enough:

} finally {
    closeQuietly(input);
    closeQuietly(output);
}

It's much simpler.

closeQuietly ignores exceptions which means that it catches them but don't do anything with them. Notice the empty catch block:

    } catch (IOException ioe) {
        // ignore
    }

It ensures that if closeable.close() throws an IOException the closeQuietly(output) will run and it will close the output stream too.

I'm a little bit paranoid about this so I always put a logging statement to the catch block:

    } catch (IOException ioe) {
        logger.warn("Could not close the stream", ioe);
    }

To be honest, I've never seen this exception, so I think it's safe to left this block empty.

share|improve this answer
its actually Android so thats not really an option – jiduvah Jul 9 '12 at 15:01
@jiduvah: check the update, please. – palacsint Jul 9 '12 at 15:36
what is the point of this code? You mentioned that I should use closeQuietly methods as it ignores exceptions and accepts null. But here you are catching an exception and checking for null – jiduvah Jul 9 '12 at 15:44
2  
... so you include this function, and call it from your finally blocks - instead of checking for nulls and exceptions and calling .close(). Seems clear enough for me - not for you? – ANeves Jul 9 '12 at 15:48
Ah because in my code if the .close() throws an exception it will be passed up. I didn't think about that – jiduvah Jul 9 '12 at 22:30

Yes this is the right way to do it but you should check output and input variables for null value before calling on them the close() method.

share|improve this answer

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.