Pipe管道 | 张扎瓦的博客

Pipe管道


Pipe管道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class PipeTest {
public static void main(String[] args) throws IOException {
Pipe pipe = Pipe.open();

// 通过管道发送数据
Pipe.SinkChannel sink = pipe.sink();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put("通过管道发送数据".getBytes());
byteBuffer.flip();
sink.write(byteBuffer);
byteBuffer.clear();

// 接受数据
Pipe.SourceChannel source = pipe.source();
int len = source.read(byteBuffer);
byteBuffer.flip();
System.out.println(new String(byteBuffer.array(), 0, len));
byteBuffer.clear();

sink.close();
source.close();
}

}
如果我的文章对您有所帮助,不妨打赏一杯豆浆以资鼓励(○` 3′○)