public void test() throws IOException{
//1.获取管道
Pipe pipe = Pipe.open();
//2.将缓冲区的数据写入管道
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
Pipe.SinkChannel channel1 = pipe.sink();
byteBuffer.put("将缓冲区中的数据写入通道中...".getBytes());
byteBuffer.flip();
channel1.write(byteBuffer);
//3. 读取缓冲区中的数据
Pipe.SourceChannel channel2 = pipe.source();
byteBuffer.flip();
int len = channel2.read(byteBuffer);
System.out.println(new String(byteBuffer.array(),0,len));
channel2.close();
channel1.close();
}
使用管道Pipe 完成数据传输
最新推荐文章于 2023-08-03 16:15:38 发布