2

Possible Duplicate:
Connecting an input stream to an outputstream

If I have a method that expects an OutputStream (from a third-party library), and another method that expects an InputStream (again from a third-party library). What's the best practice for bridging the two together? Basically I need some sort of object that will provide both an InputStream and OutputStream that are connected.

I thought of an approach using SynchronousQueue, but having to convert and un-convert each primitive byte into an Object Byte doesn't sound like a very good idea.

I would prefer not to use temp files, and buffering the entire InputStream is out of the question, as the data can be quite large.

Edit: Also, I would like to keep this single-threaded if possible.

What's the best practice here?

Edit: I need to clarify: I do not have an InputStream OR an OutputStream, just two methods. One expecting an OutputStream to write data to, and another expecting an InputStream to read data from.

Community
  • 1
  • 1
Shane
  • 23
  • 1
  • 5
  • 1
    Why cant you read bytes form InputStream and write it to OutputStream on the fly? – Sap Oct 12 '12 at 20:44
  • I don't have an InputStream or an OutputStream, I am expected to provide an OutputStream for method 1 and an InputStream for method 2. – Shane Oct 12 '12 at 20:48
  • Your question is not clear. If the method expects an InputStream, you need to provide one. Same for the OutputStream. Where would you like these methods to read from and write to? – JB Nizet Oct 12 '12 at 20:58
  • 1
    I think you may have conflicting requirements here: "I would prefer not to use temp files, and buffering the entire InputStream is out of the question, as the data can be quite large." and "Edit: Also, I would like to keep this single-threaded if possible.". The first statement effectivly means the one of the methods will need to be consumming the data as the other method is generating it; otherwise the generated data will have to sit somewhere (memeory or disc) in it's entirity waiting to be fed to the second method. – Kiersten Arnold Oct 12 '12 at 21:00
  • Method 1 expects to be given an OutputStream to which it will write the data. Method 2 expects to be given an InputStream from which it will read the data. I need to provide both streams, without buffering all of the data (buffering a little is ok), and preferably without using temp files and preferably staying single-threaded. – Shane Oct 12 '12 at 21:11

2 Answers2

2

If data is not huge

ByteArrayOutputStream -> byte[] -> ByteArrayInputStream

otherwise

PipedInputStream + PipedOutputStream

which needs 2 threads, since java doesn't support "coroutine"

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • Data is to large to buffer completely. Also, I'd prefer to stay single threaded, but will give in if I don't find a better solution. – Shane Oct 12 '12 at 21:03
  • 1
    it can't be single threaded. when method1() writes to the outputstream, the data must either be consumed by method2() in another thread, or the data must be buffered up. – irreputable Oct 12 '12 at 21:05
  • You're right, this can't be single threaded. It's been a long day and my brain hurts. – Shane Oct 12 '12 at 21:21
0

You can use one of the copy methods of commons IOUtils, namely

static int  copy(InputStream input, OutputStream output) 

          Copy bytes from an InputStream to an OutputStream.

ali köksal
  • 1,217
  • 1
  • 12
  • 19