Reputation: 347
I have a servlet that writes bytes to its outputstream. The size of the content might be large. I want to prevent the servlet from doing any buffering. What is the best way to prevent response buffering? I do not want to call flush() or close() on the ServletOutputStream myself as I assume the servlet container should do that for me. In that case, where do the bytes reside if the servlet calls flush() and close() and the httpclient hasn't yet read the bytes.
Upvotes: 2
Views: 1970
Reputation: 10702
Every servlet container provides behind-the-scene implementations of the HttpServletRequest and HttpServletResponse implementations meaning that you have no idea if the backing impl inside of one container buffers the writes and the other doesn't.
That said, the buffering will be limited to a small multi-KB size (if it is even done) so you can simply write() over and over and over again to the output stream without worrying about memory. The impl will stream those bytes to the network connection, over the wire, to the client where it is the job of the network hardware, operating system and client code to buffer and read all the content.
TCP will take care of ACKs and resending any lost data for you automatically.
In short, you don't need to worry about the client "Catching up"; just write out your data and everything on the clients end in combination with TCP will take care of getting the data there.
Upvotes: 3