When I close a BufferedInputStream, is the underlying InputStream also closed? [duplicate]
Asked Answered
R

5

21
InputStream in = SomeClass.getInputStream(...);
BufferedInputStream bis = new BufferedInputStream(in);

try {
    // read data from bis
} finally {
    bis.close();
    in.close();    
}

The javadoc for BufferedInputStream.close() doesn't mention whether or not the underlying stream is closed:

Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Is the explicit call to in.close() necessary, or should it be closed by the call to bis.close()?

Reassure answered 23/6, 2014 at 9:51 Comment(1)
Short answer: Yes. Long answer: Yeeeeesssss. Seriously, look at docjar.com/html/api/java/io/BufferedInputStream.java.html#472Spaetzle
O
29

From the source code of BufferedInputStream :

public void close() throws IOException {
    byte[] buffer;
    while ( (buffer = buf) != null) {
        if (bufUpdater.compareAndSet(this, buffer, null)) {
            InputStream input = in;
            in = null;
            if (input != null)
                input.close();
            return;
        }
        // Else retry in case a new buf was CASed in fill()
    }
}

So the answer would be : YES

Owenism answered 23/6, 2014 at 9:57 Comment(0)
T
10

BufferedInputStream doesn't hold any system resources itself; it simply wraps around an InputStream which holds those resources. Therefore the BufferedInputStream forwards the close operation onto the wrapped InputStream which will then release its resources.

Tocharian answered 23/6, 2014 at 9:58 Comment(0)
M
7

When you close a BufferedInputStream, the underlying InputStream is indeed also closed. :)

Marielamariele answered 23/6, 2014 at 9:55 Comment(5)
Can you provide a reference?Reassure
@GrahamBorland you can simply look the source code of JavaAfreet
Also, that would be my reading of "releases any system resources associated with the stream". But yeah, they could be more explicit.Rubin
@GrahamBorland There are dozens of similar questions on stackoverflow already. I'll link a few: #12199642 #1389102 #11264426Marielamariele
@Marielamariele Thanks, I couldn't find any when I searched. Those all refer to Readers rather than InputStreams but they're close enough I guess.Reassure
C
3

Yes. The underlying stream will be closed.

Cassiecassil answered 23/6, 2014 at 9:57 Comment(0)
A
2

Here is the Java implementation

/**
 * Closes this input stream and releases any system resources
 * associated with the stream.
 * Once the stream has been closed, further read(), available(), reset(),
 * or skip() invocations will throw an IOException.
 * Closing a previously closed stream has no effect.
 *
 * @exception  IOException  if an I/O error occurs.
 */
public void close() throws IOException {
    byte[] buffer;
    while ( (buffer = buf) != null) {
        if (bufUpdater.compareAndSet(this, buffer, null)) {
            InputStream input = in;
            in = null;
            if (input != null)
                input.close();
            return;
        }
        // Else retry in case a new buf was CASed in fill()
    }
}

So, the stream will be closed

Afreet answered 23/6, 2014 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.