12

In an Android application I'm sending a picture taken from the Camera Intent so I need to transform a Bitmap to a byte array. To do this I use a ByteArrayOutputStream as follow:

private byte[] getRawImageData(Bitmap source) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] rawImageData = null;
    try {
        source.compress(CompressFormat.JPEG, DEFAULT_COMRESSION, baos);
        rawImageData = baos.toByteArray();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            // handle exception here
        }
    }
    return rawImageData;
}

Everything works fine and all, the real question is the difference in the documentation of ByteArrayOutputStream between the javadoc and the doc from Android.

The Javadoc reads

Closing a ByteArrayOutputStream has no effect.

the Android doc reads:

Closes this stream. This releases system resources used for this stream.

I am closing the stream not matter what but I would like to know which documentation is correct and why they are different.

El Bert
  • 2,958
  • 1
  • 28
  • 36
  • 2
    the first and second are correct, depends on the outputstrem implementation, closing a baos, does nothing. however it is good practice to always close streams, no matter from where they come – epoch Mar 13 '13 at 09:58
  • Are you worried about what happens to the streams underlying byte array, when `close` is called? – Perception Mar 13 '13 at 10:02
  • @epoch but the problem is: **why is it required to close this stream when it does nothing at all?** – waqaslam Mar 13 '13 at 10:05
  • @Perception I don't think that what happens to the underlying byte array is the problem since the stream object will be GCed once out of scope. – El Bert Mar 13 '13 at 10:31
  • 1
    @bvidal - so your question is just plain curiosity then? In any case, the first documentation is correct, the `close` operation on a BAOS is a no-op. As has already been stated and you are currently doing, you should close it regardless. – Perception Mar 13 '13 at 10:37

3 Answers3

10

ByteArrayOutputStream is a memory based stream (managed and populated by user in code), so there is no effect when you call close() on it. The only way to clean up its memory foot-print is by revoking all the references to this object. By then, the Garbage Collector will kick-in any time soon in future and do its job to clean up such objects.

However, closing a stream is required when you engage resources like files or input/output socket streams (e.g. OutputStream, InputStream). When you call close() on such streams, the JVM safely releases their local-storage/occupied-memory and avoids any OutOfMemory issues.

So in general, it is good (and sometimes vital) to call close() on any type of stream when it is no longer required, but more importantly, it is better to know why should we call.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
3

This is the implementation of ByteArrayOutputStrem.close

public void close() throws IOException {
}

API says Closing a ByteArrayOutputStream has no effect

so it's safe to omit close(), on the other hand it does not harm, JVM skips calls to empty methods anyway. It may be used to avoid warnings resource is not closed.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Closing the stream won't do any harm, but not closing it might. So it's better to close it.

Kishore
  • 819
  • 9
  • 20