The Java NIO pipeline is a one-way data connection between 2 threads. Pipe
there is a source channel and a sink channel. The data is written to the sink channel, which is read from the source channel.
Here is the diagram of the pipe principle:
Creating pipelines
Pipe.open()
Open the pipeline by method. For example:
Pipe pipe = Pipe.open ();
Write data to the pipeline
To write data to the pipeline, you need access to the sink channel. Like this:
Pipe.sinkchannel Sinkchannel = Pipe.sink ();
By calling Sinkchannel's write()
method, the data is written SinkChannel
, like this:
String NewData = "New string to write to file ..." += bytebuffer.allocate ($); Buf.clear (); Buf.put ( Newdata.getbytes ()); Buf.flip (); while (Buf.hasremaining ()) { sinkchannel.write (BUF);}
Reading data from a pipeline
From reading the data of the pipeline, you need to access the source channel, like this:
Pipe.sourcechannel Sourcechannel = Pipe.source ();
Call the source channel's read()
method to read the data, like this:
Bytebuffer buf = bytebuffer.allocate (); int bytesread = Sourcechannel.read (BUF);
read()
The int value returned by the method tells us how many bytes were read into the buffer.
Java NiO Series Tutorials (11) Pipe