Last update time: 2014-06-23
A Java NiO pipeline is a one-way data connection between two threads. A pipe has a source channel and a sinking channel. You write data to a sinking channel. This data is then read from the source channel.
Here's a plumbing principle:
Create a pipeline
You can open a pipeline by calling the Pipe.open () method, like this:
Pipe pipe = Pipe.open ();
Write a pipeline
In order to write a pipeline, you need to access this sinking channel, like this:
Pipe.sinkchannel Sinkchannel = Pipe.sink ();
Write a sinkchannel by calling the Write () method, like this:
String NewData = "New String to write to file ..." + System.currenttimemillis (); Bytebuffer buf = bytebuffer.allocate; Buf.clear (); Buf.put (Newdata.getbytes ()); Buf.flip (); while ( Buf.hasremaining ()) { sinkchannel.write (BUF);}
read from a pipe
In order to read data from a pipeline, you need to access this source channel, like this:
Pipe.sourcechannel Sourcechannel = Pipe.source ();
In order to read the data from the source channel, you can call the Read () method, like this:
Bytebuffer buf = bytebuffer.allocate; int bytesread = Inchannel.read (BUF);
The int value returned by the Read method tells you how many bytes were read in buffer.
Translation Address: http://tutorials.jenkov.com/java-nio/pipe.html
Java NiO 13, Java NIO Pipe (pipeline)