*
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
Author

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?


Thanks

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
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: http://aspose.com/file-tools
 
subject: Closing ByteArrayOutputStream