How to convert OutputStream to InputStream? How to convert OutputStream to InputStream? java java

How to convert OutputStream to InputStream?


There seem to be many links and other such stuff, but no actual code using pipes. The advantage of using java.io.PipedInputStream and java.io.PipedOutputStream is that there is no additional consumption of memory. ByteArrayOutputStream.toByteArray() returns a copy of the original buffer, so that means that whatever you have in memory, you now have two copies of it. Then writing to an InputStream means you now have three copies of the data.

The code:

// take the copy of the stream and re-write it to an InputStreamPipedInputStream in = new PipedInputStream();final PipedOutputStream out = new PipedOutputStream(in);new Thread(new Runnable() {    public void run () {        try {            // write the original OutputStream to the PipedOutputStream            // note that in order for the below method to work, you need            // to ensure that the data has finished writing to the            // ByteArrayOutputStream            originalByteArrayOutputStream.writeTo(out);        }        catch (IOException e) {            // logging and exception handling should go here        }        finally {            // close the PipedOutputStream here because we're done writing data            // once this thread has completed its run            if (out != null) {                // close the PipedOutputStream cleanly                out.close();            }        }       }}).start();

This code assumes that the originalByteArrayOutputStream is a ByteArrayOutputStream as it is usually the only usable output stream, unless you're writing to a file. I hope this helps! The great thing about this is that since it's in a separate thread, it also is working in parallel, so whatever is consuming your input stream will be streaming out of your old output stream too. That is beneficial because the buffer can remain smaller and you'll have less latency and less memory usage.


An OutputStream is one where you write data to. If some module exposes an OutputStream, the expectation is that there is something reading at the other end.

Something that exposes an InputStream, on the other hand, is indicating that you will need to listen to this stream, and there will be data that you can read.

So it is possible to connect an InputStream to an OutputStream

InputStream----read---> intermediateBytes[n] ----write----> OutputStream

As someone metioned, this is what the copy() method from IOUtils lets you do. It does not make sense to go the other way... hopefully this makes some sense

UPDATE:

Of course the more I think of this, the more I can see how this actually would be a requirement. I know some of the comments mentioned Piped input/ouput streams, but there is another possibility.

If the output stream that is exposed is a ByteArrayOutputStream, then you can always get the full contents by calling the toByteArray() method. Then you can create an input stream wrapper by using the ByteArrayInputStream sub-class. These two are pseudo-streams, they both basically just wrap an array of bytes. Using the streams this way, therefore, is technically possible, but to me it is still very strange...


As input and output streams are just start and end point, the solution is to temporary store data in byte array. So you must create intermediate ByteArrayOutputStream, from which you create byte[] that is used as input for new ByteArrayInputStream.

public void doTwoThingsWithStream(InputStream inStream, OutputStream outStream){   //create temporary bayte array output stream  ByteArrayOutputStream baos = new ByteArrayOutputStream();  doFirstThing(inStream, baos);  //create input stream from baos  InputStream isFromFirstData = new ByteArrayInputStream(baos.toByteArray());   doSecondThing(isFromFirstData, outStream);}

Hope it helps.