I read in this post that a low level file handled is allocated whenever fileinputstream or outputstream is used and hence it is always necessary to close io stream. In case of other streams(eg: ByteArrayInputStream) which do not deal with file or network io , is it necessary to close the streams? If so , why?
-
Possible duplicate of [Will not closing a stringwriter cause a leak?](https://stackoverflow.com/questions/14542535/will-not-closing-a-stringwriter-cause-a-leak) – Oleg Sep 17 '17 at 01:44
-
If you close *every* stream, you can never *not* close a stream. Worrying about which ones are which isn't a good use of your time. – user207421 Sep 17 '17 at 02:03
-
@Oleg: The question specifically for `ByteArrayInputStream` is not answered by the Question you cite (but the answer is the same...) – sruetti Sep 17 '17 at 02:28
-
@sruetti I think it's similar enough to be a duplicate. I've seen users with gold badges close questions claiming they are "effectively" duplicate that were much less similar. – Oleg Sep 17 '17 at 02:38
3 Answers
You generally have to close Streams, either explicitly (Stream.close()
) or using try-with-resources, as described in the post you cite or in the article on javapractices.com @SassyRegards201 cites.
If you take your programming serious, there's no "it usually works without". The only way you might get away without closing a stream is if it is clearly documented that the specific implementation does not need closing (as with ByteArrayInputStream of Oracle JDK 7 and in the OpenJDK code) and you are sure that only this implementation is used. Changing the version or vendor of the JDK could void your assumption!
The problem is that you should program against interfaces. And as all streams implement the Closeable
interface that defines the close()
method (intended to be called to release resources), an implementation may change in a way that might some day require that you call the close method.
Closing every stream doesn't cost much (performance and developer time), is just good practice and nowadays checked by bug finding software.

- 532
- 2
- 7
I read in this post that a low level file handled is allocated whenever fileinputstream or outputstream is used and hence it is always necessary to close io stream.
A file input or output stream yes. Other network streams must be closed as well because they have a file-descriptor.
In case of other streams(eg: ByteArrayInputStream) which do not deal with file or network io , is it necessary to close the streams? If so , why?
Is it necessary, no, but's it's certainly a good practice. If you look at the close()
method in ByteArrayInputStream
and ByteArrayOutputStream
, you can see that it is a no-op:
public void close() throws IOException {
}
In terms of practice, if you get into the habit of always closing streams, then you will be better off if you switch later to use a different stream. We copy code and patterns from one place in code to others so that doing the right thing and closing every stream may help later.
In Java 7+ you can use the try with resources to have the stream auto-closed:
try (OutputStream stream = new ByteArrayOutputStream()) {
...
}
Or you can use try/finally:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
...
} finally {
baos.close();
}
byte[] bytes = baos.toByteArray();
...

- 115,027
- 24
- 293
- 354
-
That's where try-finally gets dirty: Imho `baos.close();` should be enclosed in a `try {...} catch (IOException ioe) {//ignore}` as `close()` is declared to throw an `IOException`... – sruetti Sep 17 '17 at 02:41
-
1Typically I use the apache `IOUtils.closeQuietly(...)` which handles null streams and ignores `IOException` @sruetti. – Gray Sep 17 '17 at 03:43
It's not absolutely necessary but it's a preferred practice like a lot of things in java.
http://www.javapractices.com/topic/TopicAction.do?Id=8
Here's a link on which you can read up on streams and closing them and why they should be closed.
Hope this helped!

- 66
- 1
- 9
-
Thanks @Sassy. I had a look at this link before I posted. It says , chained streams will be closed. However I wasn't sure why it had to be closed when none of the chained calls lead to a file or network io. – Punter Vicky Sep 17 '17 at 01:38
-
Ok so think of it this way. Think of your stream like a portal. And that portal can lead to one place but after that the connection closes. The portal can't have infinite connections. – SassyRegards201 Sep 17 '17 at 01:41
-
You need to close the portals so you don't have leaks. So people aren't teleporting all over the place. I mean I think that's the best explanation I can give lol. – SassyRegards201 Sep 17 '17 at 01:50
-