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?
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?
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.
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.
There is some indirect evidence for this in the 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.The other thing to note that the response object that a servlet sees could actually be 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.
Your best bet is to look at the source code of the container.
They would flush the same buffer if you have been using
getOutputStream
to write to the body. The other alternative isgetWriter
for non-binary data. If you had been using that, then callingres.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:
Calling
getOutputStream()
creates aCoyoteOutputStream
that uses theoutputBuffer
field that is shown there and likewise forgetWriter()
. So they both would use thatoutputBuffer
depending on which you use.flushBuffer
simply does this: