Daimler Chrysler and the Java Crisis
According to statcounter, someone at Daimler Chrysler (who was using Windows XP and a screen resolution of 1024x768 ... eek) needs help "converting" an OutputStream to an InputStream. The exact question was phrased, convert outputstream to inputstream. I think that there could be at least a couple fairly legitimate interpretations of this question, but I'm going with: I need to be able to write to an OutputStream and then read that same data via an InputStream. A FIFO buffer. I choose this interpretation, because I already have an adequate solution. I call it, GinormousBuffer (don't forget Random; GinormousBuffer needs it.).
GinormousBuffer basically provides a single interface for both RAM and disk based FIFO buffers. Based on the settings, it will store the buffer in RAM until a specific threshhold is exceeded, and then page it out to disk. However, through all of this, the interface remains consistent, and the "user" never knows.
Creating and configuring a new GinormousBuffer is very simple. Let's make one with an internal (ie RAM) capacity of ~100K and an external (ie disk) capacity of ~2MB.
GinormousBuffer gb = new GinormousBuffer();
gb = new GinormousBuffer();
gb.setMaxInternalBufferSize(100 * 1000);
gb.setMaxExternalBufferSize(2 * 1000 * 1000);
A series of helpful append() methods exist for appending all manner of data. However, most people will probably be best off using the getOutputStream() method. And after one finishes buffering all of one's data, the getInputStream() method will provide access to that data.
The InputStream instances returned from getInputStream() work independently of each other, and you can therefore have any number of InputStreams reading from the same GinormousBuffer, simultaneously. This is, of course, not the case with the OutputStream.
And if all of this is over-kill, and you just want a simple FIFO buffer class with an InputStream and OutputStream interface built in, try FIFOBuffer.