File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Closing ByteArrayOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Closing ByteArrayOutputStream" Watch "Closing ByteArrayOutputStream" New topic

Closing ByteArrayOutputStream

Richa Sharma
Ranch Hand

Joined: Dec 06, 2008
Posts: 47
Hi Guys

I am using java API ByteArrayOutputStream. How do I close the stream?

The API documentation says that
"Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException".

Will the reset method of ByteArrayOutputStream work instead?


Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Why do you need to close the stream? If you're done writing to it, get the contents using toByteArray() and discard the reference.

How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Typically it's a good idea, sometimes necessary, to close a stream when you're done with it. In the case of a ByteArrayOutputStream though, there's nothing really that needs to be done. So they define close() to do nothing. You're welcome to close the stream by calling close() just like you would for any other stream - it just doesn't do anything. Because there's nothing that needs to be done.
I agree. Here's the link:
subject: Closing ByteArrayOutputStream
It's not a secret anymore!