• Post Reply Bookmark Topic Watch Topic
  • New Topic

output stream to input stream  RSS feed

 
Prash Negu
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement to convert output stream to input stream in the code. Below is how I am doing it. Is there any better way to do this in 1.6? Does below approach has any disadvantages?



Please suggest. Thanks in advacnce.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has the disadvantage that it doesn't compile.

However I can't suggest any better way to do it (i.e. something which would at least compile) because I'm not clear on what it means to "convert" an output stream to an input stream. Could you explain that a bit?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
converting an output stream to an input stream is like saying "I want to convert the exhaust tailpipe on my car to be where I pump gas into the fuel tank".

It makes no sense.
 
Prash Negu
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends on requirement. Here is my business case..

I am working with Apache FOP where JAXB annoted java beans are used to generate PDF documents. Here are two steps.
1) JAXB marchaller converts bean to XML output stream. I dont want to write to a file.
2) Above output stream needs to go in as inout to FOP transformer. So I need to convert above output stream to input stream.

Above code is working fine for me. Just wanted to know is there any better way to that.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you should look into java.io.PipedOutputStream and PipedInputStream.

Bill
 
Prash Negu
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Piped streams are for concurrent operations using threads. But, in my requirement those are one after another.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're saying you need to write everything to the OutputStream first, and only then use the InputStream? Why? This requires a buffer to store everything that's written - something your current ByteArrayOutputStream will do for you. If there's a lot of data, this buffer will become very large.

If you use PipedInputStream and PipedOutputStream there is one thing you need to be very aware of - both streams need to be open until the other is done. If you close the PipedOutputStream (for example by letting its thread end), reading from the PipedInputStream will cause exceptions. That's mentioned in the Javadoc of PipedInputStream and PipedOutputStream:
A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.

The pipe is said to be broken if a thread that was reading data bytes from the connected piped input stream is no longer alive.
 
Prash Negu
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works. I overlooked piped streams.
 
Davide Simonetti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Me too i had this kind of problem: a library want to write its data to an OutputStream and later another library wanted to read the same data from an InputStream.

There are 3 possible approaches:
* the one you were doing: write the data to a ByteArrayOutputStream and read it back from a ByteArrayInputStream. The advantages are: it's easy to implement, and fast if the data fit into memory. Depending on the size of the data it may stress your server or send it OutOfMemory.
* Save the data to a temporary file, and read it back. It's slow, you must be careful in closing the streams.
* use pipes. Pipes are a good compromise between speed and memory usage. They are difficult to implement (you have to instantiate a new Thread in order to use them).

A code sample on how to use pipes:


I wrote a tutorial where I explain tese concepts in a longer way, with code samples.
The sample code above is over simplified, if the intenral function launches an exception it is completely lost, and the outer stream is truncated. To deal with these problems i also wrote an Open Source library (Easystream) to ease the task of using pipes. Have a look if you're still interested.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!