• Post Reply Bookmark Topic Watch Topic
  • New Topic

PipeReader and PipeWriter

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Although I read lots of tutprials about PipeReader and PipeWriter, I could not exactly understand its purpose and especially usage.
Would you explain it and send sample explanatory code snippet for this ??

Thanks
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pipes are used to channel the output from one thread into the input of another

The Java Tutorial: IO: How to Use Pipe Streams
There's an example on that page as well. Does that all make sense?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you want to read in a InputStream (or Reader) every byte (or character) that is being written to an OutputStream (or Writer)?

That's what pipes are good for. You have one thread procesing information, and writing that information into a PipeOutputStream (PipedWriter). At the other side of the pipe there is another thread, reading the information through a PipedInputStream (PipedReader).

For instance, you have this thread, that simply receives a list of
Strings, reverse them and then prints them on the screen.



Now, instead of printing them in the screen, we are going to send them on pipe
to other thread

You do that like this:



But, how do we connect the pipes?. That's to say, how do we make another thread to
receive the reversed strings. Well, take a look at this example. In this case, the
thread that connects to the other end of the pipe is the main thread.



I hope this helps!
[ April 27, 2006: Message edited by: Edwin Dalorzo ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!