Java.nio.channels.Pipe 提供一种机制,可以按照写入的顺序读取数据。 Pipe.SinkChannel用来向管道写入数据,而Pipe.SourceChannel用来从管道读取数据:
PipeTest.java
package cn.outofmemory.nio.channels; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Pipe; public class PipeTest { public static void main(String[] args) throws IOException { //初始化Pipe实例 Pipe pipe = Pipe.open(); // 获取写通道 Pipe.SinkChannel skChannel = pipe.sink(); String testData = "Test Data to Check java NIO Channels Pipe."; ByteBuffer buffer = ByteBuffer.allocate(512); buffer.clear(); buffer.put(testData.getBytes()); buffer.flip(); //向写通道写入数据 while(buffer.hasRemaining()) { skChannel.write(buffer); } //获得读取数据通道 Pipe.SourceChannel sourceChannel = pipe.source(); buffer = ByteBuffer.allocate(512); //将读取数据写到控制台 while(sourceChannel.read(buffer) > 0){ //flip方法将当前读取位置设置为0, limit设置为写入数据的size buffer.flip(); while(buffer.hasRemaining()){ char ch = (char) buffer.get(); System.out.print(ch); } //clear方法将buffer的limit设置为其容量capacity, 将position设置为0 buffer.clear(); } } }
输出如下:
Test Data to Check java NIO Channels Pipe.
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10464.html