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

最新文章

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

热门文章

  1. VIM中使用S查找并替换
  2. PocketMoney
  3. AngularJs指令配置参数scope详解
  4. Java变量的默认值和初始化
  5. makefile 必知必会以及Makefile是怎样炼成的
  6. springMvc Velocity tool 源码分析
  7. General PE format layout
  8. NXP LPC-Link LPC3154
  9. Context Switching on the Cortex-M3
  10. [Winform]默认以管理员身份运行程序