Does closing a BufferedOutputStream also close the underlying OutputStream?

后端 未结 2 1918
北海茫月
北海茫月 2021-02-19 10:07

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

2条回答
  •  盖世英雄少女心
    2021-02-19 10:34

    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.

提交回复
热议问题