java res.flushBuffer() vs res.getOutputStream().flush();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13318024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
res.flushBuffer() vs res.getOutputStream().flush();
提问by GionJh
What's the difference between calling :
打电话有什么区别:
res.flushBuffer();
versus
相对
res.getOutputStream().flush();
Do these methods flush the same buffer ?
这些方法是否刷新相同的缓冲区?
If so, can you give me a clue on how this buffer is managed by the servlet container?
如果是这样,你能给我一个关于 servlet 容器如何管理这个缓冲区的线索吗?
采纳答案by Dave L.
They would flush the same buffer if you have been using getOutputStream
to write to the body. The other alternative is getWriter
for non-binary data. If you had been using that, then calling res.getOutputStream().flush();
probably wouldn't work.
如果您一直使用getOutputStream
写入正文,它们将刷新相同的缓冲区。另一种选择是getWriter
用于非二进制数据。如果您一直在使用它,那么调用res.getOutputStream().flush();
可能不起作用。
The way the buffer is managed is implementation-specific but take one of the Tomcat implementations for example. You can see that there are some fields like this:
缓冲区的管理方式是特定于实现的,但以Tomcat 实现之一为例。可以看到有一些字段是这样的:
/**
* The associated output buffer.
*/
protected OutputBuffer outputBuffer;
/**
* The associated output stream.
*/
protected CoyoteOutputStream outputStream;
/**
* The associated writer.
*/
protected CoyoteWriter writer;
Calling getOutputStream()
creates a CoyoteOutputStream
that uses the outputBuffer
field that is shown there and likewise for getWriter()
. So they both would use that outputBuffer
depending on which you use. flushBuffer
simply does this:
调用getOutputStream()
创建一个CoyoteOutputStream
使用outputBuffer
显示在那里的字段,同样为getWriter()
. 因此,他们都会outputBuffer
根据您使用的内容来使用它。flushBuffer
只是这样做:
@Override
public void flushBuffer()
throws IOException {
outputBuffer.flush();
}
回答by Stephen C
What's the difference between calling ...
打电话有什么区别...
The only significant difference is that the first version will work whether you are writing / going to write the body in text or binary mode, whereas the second version only works with binary mode output.
唯一的显着区别是第一个版本适用于您是否以文本或二进制模式编写/准备编写正文,而第二个版本仅适用于二进制模式输出。
Do these methods flush the same buffer ?
这些方法是否刷新相同的缓冲区?
Since the javadocs don't give an explicit answer, technically it is implementation dependent. However, in practice the answer is probably "YES" for most implementations, because it is hard to conceive of it making sense to have separate buffers.
由于 javadocs 没有给出明确的答案,因此从技术上讲,它取决于实现。然而,在实践中,大多数实现的答案可能是“是”,因为很难想象拥有单独的缓冲区是有意义的。
There is some indirect evidence for this in the javadoc:
在 javadoc 中有一些间接证据:
The javadoc for
setBufferSize(int)
says: "Sets the preferred buffer size for the body of the response." The implication is that this buffer is the same "the buffer" referred to in the javadoc forflushBuffer()
.The javadoc for
flushBuffer()
says: "A call to this method automatically commits the response, meaning the status code and headers will be written." ... which is consistent with a model where there is effectively one buffer for everything.
javadoc for
setBufferSize(int)
说:“设置响应主体的首选缓冲区大小。” 这意味着该缓冲区与 javadoc for 中引用的“缓冲区”相同flushBuffer()
。javadoc for
flushBuffer()
说:“调用此方法会自动提交响应,这意味着将写入状态代码和标头。” ...这与模型一致,在该模型中,所有东西都有效地有一个缓冲区。
The other thing to note that the response object that a servlet sees could actuallybe an application specific wrapper that was inserted further up the filter chain. It is possible for such a wrapper to behave in a way that is inconsistent with what the javadoc (and the rest of the servlet spec) says.
另一件要注意的事情是,servlet 看到的响应对象实际上可能是一个特定于应用程序的包装器,它被插入到过滤器链的更上游。这种包装器的行为方式可能与 javadoc(以及 servlet 规范的其余部分)所说的不一致。
If so, can you give me a clue on how this buffer is managed by the servlet container?
如果是这样,你能给我一个关于 servlet 容器如何管理这个缓冲区的线索吗?
Your best bet is to look at the source code of the container.
最好的办法是查看容器的源代码。