Reprinted from Concurrent Programming Network –ifeve.com This article link address: Java NIO Series Tutorial (11) Pipe
The Java NIO pipe is a one-way data connection between 2 threads. Pipe has a source channel and a sink channel. The data is written to the sink channel and is read from the source channel.
Here is the diagram of the pipe principle:
Creating Pipelines
Open the pipe through the Pipe.open () method. For example:
1 |
Pipe Pipe = Pipe.open (); |
write data to a pipe
To write data to a pipe, you need to access the sink channel. Like this:
1 |
Pipe.sinkchannel Sinkchannel = Pipe.sink (); |
Write the data to Sinkchannel, like this by calling the Sinkchannel write () method:
01 |
String NewData = "New String to write to file ..." + System.currenttimemillis (); |
02 |
Bytebuffer buf = Bytebuffer.allocate (48); |
04 |
Buf.put (Newdata.getbytes ()); |
08 |
while (Buf.hasremaining ()) { |