File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 19541
    
  16

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: 2969
    
    9
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
 
Similar Threads
IOException when i close a stream in Applet WHY!!!!
Help: Attaching generated pdf file with an email
need help: inputstream to String or StringBuffer
Convert Ascii String to Tif file format content
Does the order of stream closing matters?