| Author |
output stream to input stream
|
Prash Negu
Ranch Hand
Joined: Apr 20, 2009
Posts: 45
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
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
Joined: Oct 02, 2003
Posts: 10043
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Prash Negu
Ranch Hand
Joined: Apr 20, 2009
Posts: 45
|
|
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
Joined: Mar 22, 2000
Posts: 12327
|
|
Perhaps you should look into java.io.PipedOutputStream and PipedInputStream.
Bill
|
Java Resources at www.wbrogden.com
|
 |
Prash Negu
Ranch Hand
Joined: Apr 20, 2009
Posts: 45
|
|
|
I think Piped streams are for concurrent operations using threads. But, in my requirement those are one after another.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Prash Negu
Ranch Hand
Joined: Apr 20, 2009
Posts: 45
|
|
|
Thanks, it works. I overlooked piped streams.
|
 |
 |
|
|
subject: output stream to input stream
|
|
|