Alex, I'm not sure what you are talking about with a BufferedReader filling up.  It doesn't make sense in context, this class is about an output stream.

Assuming you meant the BufferedOutputStream we are working with here, its not important that the stream is flushed when this method is run.  During reachedInCoreLimit(), we don't care that the underlying stream gets a flush, we just need to start spooling this stuff out of memory before we explode the heap.  So when that method calls switchToOverflow() invoking flush isn't worth it, it could cause a partial block (what is left in the last buffer) to be written to the underlying stream, which may be a wasted system call that could have easily been aggregated with subsequent writes to this stream.

During doFlush() above, we switch to the overflow stream, then flush it.  So the BufferedOutputStream gets flushed, and the underlying OutputStream that is created by our subclass is also flushed.

Yes, the subclass can implement overflow() to return a BufferedOutputStream.  That means we might double-buffer here.  However, its not worth checking to see if the stream is a BufferedOutputStream and special case around it.  BufferedOutputStream stacks well, so long as the wrapping BOS uses a buffer size that is >= the buffer size of the inner BOS, the inner BOS will become a no-op and simply pass through the wrapper's byte array to the underlying stream.  The default buffer size in the Sun JDK is 8192 bytes, which is the same size as our block size here.

However, the intent is that the subclass' overflow() method is a raw stream, without buffering, and that we supply buffering since our class is called TemporaryBuffer.  For example our File subclass directly returns the FileOutputStream.  Where it gets more complex is in UploadPackServlet, where the overflow stream is the ServletOutputStream.  Buffering on that is undefined, and it most certainly does not extend BufferedOutputStream.  Some servlet containers buffer that in 1 MiB chunks.  Some don't buffer it at all.  Its hard to say what will happen.