程序员Java学习笔记

Java NIO(五):Pipe

2018-03-05  本文已影响96人  聪明的奇瑞

Pipe

Pipe

Pipe使用

Pipe pipe = Pipe.open();
Pipe.SinkChannel sink = pipe.sink();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.put("新的一天".getBytes("UTF-8"));
buf.flip();
while(buf.hasRemaining()) {
    sink.write(buf);
}   
Pipe.SourceChannel source = pipe.source();
ByteBuffer byteBuffer = ByteBuffer.allocate(48);
int length = source.read(byteBuffer);
System.out.println(new String(byteBuffer.array(),0,length,"UTF-8"));
上一篇 下一篇

猜你喜欢

热点阅读