Closing ByteArrayOutputStream in an Android application
Asked Answered
D

3

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.

Dubois answered 13/3, 2013 at 9:56 Comment(5)
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 comeRosaniline
Are you worried about what happens to the streams underlying byte array, when close is called?Merla
@Rosaniline but the problem is: why is it required to close this stream when it does nothing at all?Thereinto
@Merla 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.Dubois
@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.Merla
T
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.

Thereinto answered 13/3, 2013 at 10:15 Comment(0)
T
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.

Tiossem answered 13/3, 2013 at 10:0 Comment(0)
K
0

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

Kameko answered 13/3, 2013 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.