Win a copy of Functional Reactive Programming this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic

OutputStream to InputStream?

 
Josh Shinavier
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Say you want to perform some sort of operation on the data in an InputStream, e.g. the audio data in a WAV file. You cycle through it char by char until you reach the end of the stream, writing to an OutputStream as you go. Now you want to perform a second operation on the modified stream you've just created. Is there a way to then open a new InputStream which reads what you've just written to the OutputStream (without first writing to a file)?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ummm... I hope you're not really treating bytes from an audio stream as "chars"; that's going to be pretty confusing. As to your question - you could achieve this using a PipedOutputStream and PipedInputStream I think. You write to the PipedOutputStream, and create another thread that reads from the attached PipedInputStream and does whatever you want with the data. However it may well be more efficient to implement whatever additional processing you want using a single class that extends FilterOutputStream. Then just chain the FilterInputStream on to whatever other OutputStream you want to write to.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Report post to moderator
Jim, do you know any examples like this? I wanted to make a bunch of filter objects that I could plug together in different combinations kinda like using Unix pipes. I played with it for about ten minutes and decided I wasn't quite ready. Is that what the piped classes are for?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think so. I don't know a good complete example of this off the top of my head, and I've never actually had a problem where I needed the piped streams - I always managed to refactor things so I was looking at one of the streams from the other end, and suddenly things seemed simpler. But if you have a component A which accepts an OutputStream as a parameter, and a component B which accepts an InputStream as a parameter, and A needs to do something before B and those steams are the expected ways to get data in/out of the specified components, and you can't edit either A or B to give you a more intuitive interface ,, then piped streams are probably what you need:

A key point is that the components need to be in separate threads, since whoever has the InputStream will block until the OutputStream is written to. It all seems rather backwards to me most of the time, so if I can find a way to refactor A or B, I usually do so.
A good example of component B might be a SAXParser, which takes an InputStream as a parameter to its parse() method. At the moment I'm blanking on a good example for A that would go with this, but there's probably something out there.
Alternately, if A does something before B, and A takes an InputStream while B takes an OutputStream, it's much simpler:

That's the paradigm I'm more used to operating in, but sometimes that's not what we have to work with.
 
Davide Simonetti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Have a look also to this thread . It discuss the same topic.
Davide.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Davide, have a look at this FAQ entry.
 
    Bookmark Topic Watch Topic
  • New Topic