Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does closing a BufferedOutputStream also close the underlying OutputStream?

I am streaming binary data (a CSV file extracted from the database as a Clob) to the browser by calling response.getOutputStream() and would normally wrap the OutputStream in a BufferedOutputStream when copying the data.

Should I close the BufferedOutputStream or will this also close the underlying OutputStream?

[Extra question: Do I need to use a BufferedOutputStream in this case or is the response already buffered?]

like image 512
Caroline Orr Avatar asked Sep 15 '09 16:09

Caroline Orr


People also ask

Do we need to close Outputstreamwriter?

No, the topmost level Stream or reader will ensure that all underlying streams / readers are closed.

Do we need to close OutputStream in Java?

Close an OutputStreamOnce you are done writing data to a Java OutputStream you should close it.

What will happen if you forget to close the stream of input or output file?

OutputStream , say FileOutputStream , after writing to a file, if we don't close() the output stream, the data that we intended to write in the file remains in the buffer and is not written to the file. So it becomes necessary to close() an OutputStream .

What is BufferedOutputStream in Java?

BufferedOutputStream(OutputStream out) Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.


2 Answers

Yes, it closes it. As for whether you should close it - are you expecting to write anything else to the response stream? If not, I think it's fine to close it. If you don't close it, you should obviously flush it instead - but I suspect you could figure that bit out for yourself :)

The behaviour is actually inherited from FilterOutputStream. The Javadocs for for FilterOutputStream.close state:

The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

As for whether you should buffer it - I'm not sure that this is well defined. It may be buried in the servlet spec somewhere - and it may even be configurable (sometimes you really don't want buffering, but if you can buffer the whole response it means you can serve a nicer error page if things go wrong after you've started writing).

like image 87
Jon Skeet Avatar answered Oct 25 '22 20:10

Jon Skeet


Closing the BufferedOutputStream will also close the underlying OutputStream. You should close the BufferedOutputStream so that it flushes its contents before closing the underlying stream. See the implementation of FilterOutputStream.close() (from which BufferedOutputStream extends) to convince yourself.

I guess that whether or not the response stream given to your servlet is buffered or not depends on the implementation of your Servlet Container. FWIW I know that Tomcat does buffer its servlet response streams by default, in order to attempt to set the content-length HTTP header.

like image 34
CarlG Avatar answered Oct 25 '22 20:10

CarlG