commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [Jakarta Commons Wiki] New: IO/FrequentlyAskedQuestions
Date Sat, 29 May 2004 08:20:34 GMT
   Date: 2004-05-29T01:20:34
   Editor: RobertBurrellDonkin <>
   Wiki: Jakarta Commons Wiki
   Page: IO/FrequentlyAskedQuestions

   Added martin's explaination of DeferredOutputStream

New Page:

= Commons IO FrequentlyAskedQuestions =

 * What Use is DeferredOutputStream?

Details below:

== What Use is DeferredOutputStream? ==

''What use is DeferredOutputStream when the JDK already contains buffered output streams and

Here's the scenario it serves. Suppose you have some data that you'd
prefer to keep in memory, but you don't know ahead of time how much data
there is going to be. If there's too much, you want to write it to disk
instead of hogging memory, but you don't want to write to disk until you
need to, because disk is slow and is a resource that needs tracking for

So you create a temporary buffer and start writing to that. If / when you
reach the threshold for what you want to keep in memory, you'll need to
create a file, write out what's in the buffer to that file, and write all
subsequent data to the file instead of the buffer.

That's what DeferredOutputStream does for you. It hides all the messing
around at the point of switch-over. All you need to do is create the
deferred stream in the first place, configure the threshold, and then just
write away to your heart's content.

To be very specific, this is something that I needed for FileUpload, along
with its base class (ThresholdingOutputStream). After I created both of
these, I realised that there is nothing FileUpload-specific about them, so
I offerred, and subsequently added, them to IO.

If there is a straightforward way to accomplish the same task without a
class like this, then I'd be happy to be educated. This just seemed like a
good general purpose solution to the specific problem I had at the time.


'''Martin Cooper''' ([
on list])

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message