my dog learned polymorphism*
The moose likes Beginning Java and the fly likes pipe, Channel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pipe, Channel" Watch "pipe, Channel" New topic
Author

pipe, Channel

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
What is pipe in java?What is channel? What is difference between channel and streams?

I checked JAVA doc but it is unclear for me.
A channel for reading, writing, mapping, and manipulating a file.

A pipe consists of a pair of channels: A writable sink channel and a readable source channel.




What does mapping mean here?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Very Very Good!Thank you!
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
http://www.coderanch.com/t/522616/perl/Incubator/Running-perl-java#2369187
http://www.coderanch.com/forums/posts/preList/469172/2103031
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
May someone answer me please?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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.
 
wood burning stoves
 
subject: pipe, Channel
 
Similar Threads
Regular Expressions in String.split()
Creating a nonblocking stream
pipe
PipeReader and PipeWriter
Aliter for string formatting - Which is best approach?