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 writers?
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 cleanup. 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)
Up to IO