Java - converting from OutputStream to InputStream?

An OutputStream is one where you write data to. If some module exposes an OutputStream the expectation is that there is something at the other end that is reading 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---> intermediateBytesn ----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.

An OutputStream is one where you write data to. If some module exposes an OutputStream, the expectation is that there is something at the other end that is reading. 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---> intermediateBytesn ----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...

Copy() do this IS to OS according to API, I need it to do backwards – Waypoint Apr 25 at 13:47 See my edit on the top, it is necesarry for me to make some conversion – Waypoint Apr 25 at 14:02 Make your output a ByteArrayOutputStream, and then make you input in the second case a BAIS. Does this solution not apply? Perhaps I am misunderstanding.

– Java Drinker Apr 25 at 14:07.

Try to go through this link which might help you: coderanch.com/t/275464/Streams/java/Outp....

– GuruKulki Apr 25 at 13:42 That was not me, but there are pipedInputStreams in source code, I have pure inputStream and OutputStream, still I don't see any solution here... – Waypoint Apr 25 at 13:46.

You can't "convert a OutputStream to InputStream" (or viceversa), they are two fundamentally different classes, with different (rather opposite) behaviours. You are surely approaching your problem (which we don't know) from the wrong angle. (The good news is that, once you approach it in the right angle, you'll probably get a better understanding of OOP).

Clearly IOUtils. Copy(inputStream, outputStream) works for going from inputStream to outputStream. There's no reason why the other doesn't work, I am just not thinking of any canned solution at this moment... Maybe a brain freeze on my part.

– MeBigFatGuy Apr 25 at 13:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions