本文介绍了res.flushBuffer()vs res.getOutputStream()。flush();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致电之间有什么区别:

res.flushBuffer();

res.getOutputStream().flush();

这些方法是否刷新相同的缓冲区?

Do these methods flush the same buffer ?

如果是这样,你能给我一个关于servlet容器如何管理这个缓冲区的线索吗?

If so, can you give me a clue on how this buffer is managed by the servlet container?

推荐答案

如果你一直在使用 getOutputStream 他们会刷同一个缓冲区写到身体。对于非二进制数据,另一种替代方法是 getWriter 。如果您一直在使用它,那么调用 res.getOutputStream()。flush(); 可能无效。

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.

管理缓冲区的方式是特定于实现的,但需要。您可以看到有这样的字段:

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:

/**
 * The associated output buffer.
 */
protected OutputBuffer outputBuffer;
/**
 * The associated output stream.
 */
protected CoyoteOutputStream outputStream;
/**
 * The associated writer.
 */
protected CoyoteWriter writer;

调用 getOutputStream()创建 CoyoteOutputStream 使用 outputBuffer 字段显示在那里,同样用于 getWriter()。所以他们都会使用 outputBuffer ,具体取决于你使用的是什么。 flushBuffer 只需这样做:

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:

@Override
public void flushBuffer()
    throws IOException {
    outputBuffer.flush();
}

这篇关于res.flushBuffer()vs res.getOutputStream()。flush();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 23:23