A Java NIO Pipe is a one-way data connection between and threads. A Pipe has a source channel and a sink channel. You write data to the sink channel. This data can and is read from the source channel.
Here's an illustration of the Pipe principle:
Creating a Pipe
You open a Pipe by calling the Pipe.open () method. Here's how this looks:
Pipe pipe = Pipe.open ();
Writing to a Pipe
To write to a Pipe you need to access the sink channel. Here's how it's done:
Pipe.sinkchannel Sinkchannel = Pipe.sink ();
You write to a sinkchannel by calling it's write () method, 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 from a Pipe
To read from a Pipe you need to access the source channel. Here's how it's done:
Pipe.sourcechannel Sourcechannel = Pipe.source ();
To read from the source channel, its read () method, like this:
Bytebuffer buf = bytebuffer.allocate (); int bytesread = Inchannel.read (BUF);
The int returned by the read () method tells how many bytes were read into the buffer.
Java NIO Pipe