Java NIO Pipe
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.
Ref:
http://tutorials.jenkov.com/java-nio/pipe.html
最新文章
- IOS开发基础知识--碎片7
- 虚基类&;虚继承
- MemSQL start[c]up Round 2 - online version C. More Reclamation(博弈)
- ActiveReports 9 新功能:创新的报表分层设计理念
- Scrapy安装介绍
- OpenCV源码阅读(2)---matx.h---函数的内联实现
- Linux的启动流程以及GRUB详解
- Android通过HTTP POST带參訪问asp.net网页
- ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3
- 彩色图像--色彩空间 HSI(HSL)、HSV(HSB)
- 基于Azure blob storage T级别HBase表恢复
- HttpWebRequest 模拟网站登录获取数据
- Java内存回收机制基础[转]
- 关于reverse_iterator
- Java 面向对象 知识点基础浅谈
- 关于JDBC和连接池我学到的(转载保存)
- PHPstorm配置SVN的问题
- springcloud Eureka学习笔记
- AXI Quad SPI
- 根据23423条件,截取字段‘abdecsdsadsadsad’,以ab/dec/sdsa/ds/ads 输出