Channels and Streams essentially do the same thing, but they are different ways of doing it. They are both sources of data.
A stream is a source/sink where you read or write bytes to or from. Channels work a bit differently. Instead of reading directly from a channel, you pass a ByteBuffer to a Channel, and the Channel will either fill or drain the buffer.
I believe channels are a little bit faster than streams when it comes to reading devices or drives and such, because they are designed to read in bulk. There are also some other methods you can find in FileChannel for fast operations on files.
Personally I prefer using streams most of the time, because writing code for them is a lot less awkward than it is for channels.
File mapping is a nice feature provided by FileChannel. It maps a file to memory, so you can directly edit the file as if you were editing an array of bytes.
A pipe is simply a way of transporting data from one part of your program to another part. Pipes can be implemented with channels, streams, etc.
Let's compare a pipe to, say, a socket connection.
A socket represents the connection between two processes. You can put data in on one side, and it will come out the other side. Pipes work exactly the same way, except they represent a connection between two different parts of the same process, not different processes. For instance, you can have one thread that investigates something, and it will write its findings into one end of a pipe, and another thread will read from the other end of the pipe and process the data.
Joined: Sep 06, 2007
Very Very Good!Thank you!
Joined: Sep 06, 2007
Instead of streaming data to a file, a thread can stream it through a pipe to another thread. The first thread writes to the
pipe, and the second thread reads from the pipe. A pipe is neither a file nor a network connection, but a structure in memory that holds the data that is written until it is read.
I search forum about pipe, and find that. As I see,Pipe is used for connection between tow programming language in order to transport data, Like JAVA and C, May you explain baout the mechanism of this transportation?
I also seached in internet and see pipe is used in netwrok programming,Is it the pipe use in network programming is same pipe used in thread communication?
Stephan van Hulst
Joined: Sep 20, 2010
Well, I suppose that's one of the applications for a pipe.
Let's say you have a hardware device that contains sensors (for instance, for light, temperature, acceleration, etc). Its driver may be written in a language like C. It might be set up in a way that it sends all its sensor data through a pipe. Your program that uses the hardware might then be written in Java, which reads the data from the pipe and processes it.
Regarding network programming, I'm not sure what they are referring to. It sounds like a simple socket connection, which in a broad abstract sense you could understand as a pipe, but I prefer to use the word pipe for a connection within the same process.
Do you have a link to where you read these things?
The topics you linked to refer to a special mechanism most operating systems provide. This mechanism "pipes" the standard output of one process to the standard input of another process. In most operating systems this is done using the pipe symbol: |
Here is an example in Unix:
If I'm not mistaken, anything you write to System.out in FirstMain, will be available to System.in in SecondMain.