package com.stu.nio;
import org.junit.Test;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
public class TestPipe {
@Test
public void test() throws IOException {
// 1. 获取管道
Pipe pipe = Pipe.open();
// 2. 将缓存区中的数据写入通道(这部分放到一个线程中)
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
Pipe.SinkChannel sinkChannel = pipe.sink();
byteBuffer.put("通过单向管道发送数据".getBytes());
byteBuffer.flip();
sinkChannel.write(byteBuffer);
// 3. 读取缓存区中的数据(这部分放到一个线程中)
Pipe.SourceChannel sourceChannel = pipe.source();
byteBuffer.flip();
int len = sourceChannel.read(byteBuffer);
System.out.println(new String(byteBuffer.array(),0 ,len));
sourceChannel.close();
sinkChannel.close();
}
}
学自b站尚硅谷