Search code examples
inputstreamjava-io

How to properly close a BufferedInputStream


I create a BufferedInputStream from an InputStream. I can close the BufferedInputStream by calling its close() method. Should I also close the InputStream from which it was created, or does that happen sutomatically?


Solution

  • Closing BufferedInputStream is sufficient since it closes the underlying stream as well.

    This can also be seen in the implementation: https://github.com/openjdk/jdk/blob/9a7209ef346e4f78b6153e998ecdfac72edc5580/src/java.base/share/classes/java/io/BufferedInputStream.java#L482