简体   繁体   English

关闭BufferedOutputStream是否也会关闭底层的OutputStream?

[英]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. 我通过调用response.getOutputStream()将二进制数据(从数据库中提取的CSV文件作为Clob)传输到浏览器,并且通常在复制数据时将OutputStream包装在BufferedOutputStream中。

Should I close the BufferedOutputStream or will this also close the underlying OutputStream? 我应该关闭BufferedOutputStream还是关闭底层的OutputStream?

[Extra question: Do I need to use a BufferedOutputStream in this case or is the response already buffered?] [额外问题:在这种情况下我是否需要使用BufferedOutputStream或响应是否已经缓冲?]

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 . 该行为实际上是从FilterOutputStream继承的。 The Javadocs for for FilterOutputStream.close state: FilterOutputStream.close状态的Javadocs:

The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream. FilterOutputStream的close方法调用其flush方法,然后调用其底层输出流的close方法。

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). 它可能隐藏在某个地方的servlet规范中 - 它甚至可以配置(有时你真的不想缓冲,但如果你可以缓冲整个响应,这意味着你可以提供一个更好的错误页面,如果你出错后出错)我开始写作)。

Closing the BufferedOutputStream will also close the underlying OutputStream. 关闭BufferedOutputStream也将关闭底层的OutputStream。 You should close the BufferedOutputStream so that it flushes its contents before closing the underlying stream. 应该关闭BufferedOutputStream,以便在关闭基础流之前刷新其内容。 See the implementation of FilterOutputStream.close() (from which BufferedOutputStream extends) to convince yourself. 请参阅FilterOutputStream.close()的实现(BufferedOutputStream从中扩展)以说服自己。

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. 我想无论是否缓冲给你的servlet的响应流取决于你的Servlet容器的实现。 FWIW I know that Tomcat does buffer its servlet response streams by default, in order to attempt to set the content-length HTTP header. FWIW我知道默认情况下Tomcat确实缓冲了它的servlet响应流,以便尝试设置内容长度的HTTP头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以关闭基础输出流并让装饰器BufferedOutputStream处于未关闭状态吗 - Can I close underlying outputstream and let the decorator BufferedOutputStream unclosed 关闭InputStreamReader还关闭底层的InputStream吗? - Do closing InputStreamReader also close underlying InputStream? 从getInput / OutputStream()获得的输入/输出流的关闭是否会影响底层Socket的重新创建? - Does closing of input/output stream gotten from getInput/OutputStream() affects recreation of underlying Socket? 套接字:BufferedOutputStream还是只是OutputStream? - Sockets: BufferedOutputStream or just OutputStream? 为什么Outputstream =不缓冲新的BufferedOutputStream? - Why Outputstream = new BufferedOutputStream not buffered? 关闭套接字的输入流是否也会关闭套接字连接? - Does closing the inputstream of a socket also close the socket connection? 关于关闭BufferedOutputStream - about closing BufferedOutputStream 关闭 DataInputStream 也会关闭 FileInputStream 吗? - Will closing a DataInputStream also close the FileInputStream? 有没有办法在不关闭底层流的情况下关闭Writer? - Is there a way to close a Writer without closing the underlying stream? BufferedInputStream和BufferedOutputstream是否不同于InputStream和Outputstream - Is BufferedInputStream and BufferedOutputstream different from InputStream and Outputstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM