Streams are designed for reading and writing small chunks of data at a time, but computers tend to perform better when larger chunks of bytes are transferred at a time (eg. 2k).  Consequently some output streams can buffer writes so that data is only written when there is enough to write something significant.  A OutputStream flush() method call means “write whatever data is in the buffer immediately, no matter how small”.

Tip: A flush() is done automatically when a close() occurs, but there may be unwritten data if the server or client crashes before a flush(), much like removing a USB stick without ejecting it.

blog comments powered by Disqus