博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12, Pipe
阅读量:6818 次
发布时间:2019-06-26

本文共 1267 字,大约阅读时间需要 4 分钟。

hot3.png

A Java NIO Pipe is a one-way data connection between two threads. A Pipe has a source channel and a sink channel. You write data to the sink channel. This data can then be read from the source channel.

Here is an illustration of the Pipe principle:

Java NIO: Pipe Internals

Creating a Pipe

You open a Pipe by calling the Pipe.open() method. Here is how that looks:

Pipe pipe = Pipe.open();

Writing to a Pipe

To write to a Pipe you need to access the sink channel. Here is how that is done:

Pipe.SinkChannel sinkChannel = pipe.sink();

You write to a SinkChannel by calling it's write() method, like this:

String newData = "New String to write to file..." + System.currentTimeMillis();ByteBuffer buf = ByteBuffer.allocate(48);buf.clear();buf.put(newData.getBytes());buf.flip();while(buf.hasRemaining()) {    sinkChannel.write(buf);}

Reading from a Pipe

To read from a Pipe you need to access the source channel. Here is how that is done:

Pipe.SourceChannel sourceChannel = pipe.source();

To read from the source channel you call its read() method like this:

ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buf);

The int returned by the read() method tells how many bytes were read into the buffer.

转载于:https://my.oschina.net/u/255456/blog/372886

你可能感兴趣的文章
.NET平台开源项目速览(5)深入使用与扩展SharpConfig组件
查看>>
u-boot-1.3.4 移植到S3C2440
查看>>
HotSpot运行时概览#2
查看>>
Go结构体标签表达式v1.0发布,参数校验杀手锏
查看>>
对react中setState的总结
查看>>
[回炉计划]-实现一个图片预加载
查看>>
正则表达式
查看>>
360前端星计划学习-html
查看>>
专注dApp高效执行和高并发的下一代公有链
查看>>
ONE-sys 整合前后端脚手架 koa2 + pm2 + vue-cli3.0 + element
查看>>
携带更方便功能全 iPone与Apple Watch球形尿袋
查看>>
行为型模式:策略模式
查看>>
实现批量数据增强 | keras ImageDataGenerator使用
查看>>
太忙女友消息未及时回复,分手吗?python微信自动消息帮你谈恋爱
查看>>
Java 多线程NIO学习
查看>>
命名实体识别
查看>>
动态切换的动态代理
查看>>
电商项目(下)
查看>>
vue 数字滚动递增效果
查看>>
vue2.0中父子,兄弟组件的传值2
查看>>