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();
}
}
finallyblock is for. – Konrad Rudolph Jul 9 '12 at 14:29