The Java NIO pipeline is a one-way data connection between 2 threads.
The pipe has a source channel and a sink channel. The data is written to the sink channel, which is read from the source channel.
As an example:
PackageCom.soyoungboy.nio;Importjava.io.IOException;ImportJava.nio.ByteBuffer;ImportJava.nio.channels.Pipe;Importorg.junit.Test; Public classTestpipe {@Test Public voidTest1 ()throwsioexception{//1. Get the pipelinePipe pipe =Pipe.open (); //2. Write data in the buffer to the pipelineBytebuffer buf = bytebuffer.allocate (1024); Pipe.sinkchannel Sinkchannel=Pipe.sink (); Buf.put ("Send data through a one-way pipeline". GetBytes ()); Buf.flip (); Sinkchannel.write (BUF); //3. Reading data from a bufferPipe.sourcechannel Sourcechannel =Pipe.source (); Buf.flip (); intLen =Sourcechannel.read (BUF); System.out.println (NewString (Buf.array (), 0, Len)); Sourcechannel.close (); Sinkchannel.close (); } }
Java NIO-Pipelines (pipe)