• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why PipeReader and PipeWriter

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
My understanding is, Pipes are used to channel the output from one thread into the input of another. why can't we use OutputStream and InputStream ?
What is the advantage of using PipeReader and PipeWriter.

thanks
siva
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't use In/OutputStream for anything because they're abstract. PipedReader/Writer are specialized streams used for writing character data between threads. There's a corresponding PipedInput/OutputStream for writing binary data. Look at the I/O reading and writing chapter of the Java Tutorial for more details.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> We can't use In/OutputStream for anything because they're abstract.
> PipedReader/Writer are specialized streams used for writing character
> data between threads.

Thanks Joe. It makes sense.
However, please read below.

Look at the Section "How to Use Pipe Streams" at URL
http://java.sun.com/docs/books/tutorial/essential/io/pipedstreams.html.

The tutorial reads, "Without pipe streams, the program would have to store the results somewhere (such as in a file or in memory) between each step, as shown here:"

"With pipe streams, the output from one method could be piped into the next, as shown in this figure:"

My point of confusion is, even though, the output from one method could be piped into the next method, still you have keep the result in the buffer or memory, correct ?. I don't get, the real advantage of using pipe stream ?, though, I think, I understand what it means. Meaning pipe streams are special buffer or memory used by the thread to input or output the result.

To re-state my question, I can make the two threads read same buffer, correct ?. What is the advantage I get, by piping the result to another thread ?

When I read this, I get the impression, as if the output or result never stored in the memory, but piped to the next method.

Hope I am making my point. Not sure, Where I am getting disconnected.

thanks for your help.
siva

[ August 09, 2004: Message edited by: Siva kandasamy ]
[ August 09, 2004: Message edited by: Siva kandasamy ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Siva kandasamy:

My point of confusion is, even though, the output from one method could be piped into the next method, still you have keep the result in the buffer or memory, correct ?.


Yes and no. You could be processing a file line by line. Read in a line, pass it to the next processor using the pipe and go on to the next line. The data goes somewhere (pipes are actually a specialized kind of file at the OS level), but we don't need to be concerned about the details. Using pipes saves us the trouble of creating a data structure to synchronize and manange access to a shared buffer or writing to a temporary file, and it would presumably be more efficient than a home grown solution.


To re-state my question, I can make the two threads read same buffer, correct ?. What is the advantage I get, by piping the result to another thread ?


You'd cut your disk reads in half.
Pipes are usually used to move data that is in an intermediate form between threads. Like in the tutorial example, the data is shuffled and sorted until we've massaged it to a "valuable" form that is worth writing out.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
Its all makes sense.
Thank you for your patient and answering me.
thanks
siva
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!