Java NIO Pipe
2016-03-12 23:12
555 查看
Creating a Pipe
Writing to a Pipe
Reading from a Pipe
A Java NIO Pipe is a one-way data connection between two threads. A
Here is an illustration of the
You open a
To write to a
You write to a
To read from a
To read from the source channel you call its
The
Writing to a Pipe
Reading from a Pipe
Jakob Jenkov Last update: 2014-06-23 |
Pipehas 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
Pipeprinciple:
Java NIO: Pipe Internals |
Creating a Pipe
You open a Pipeby calling the
Pipe.open()method. Here is how that looks:
Pipe pipe = Pipe.open();
Writing to a Pipe
To write to a Pipeyou need to access the sink channel. Here is how that is done:
Pipe.SinkChannel sinkChannel = pipe.sink();
You write to a
SinkChannelby 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 Pipeyou 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
intreturned by the
read()method tells how many bytes were read into the buffer.
相关文章推荐
- Java NIO DatagramChannel
- 20145218 《Java程序设计》第二周学习总结
- java多线程典型例子及分析
- red and black 杭电1312 java深搜
- java的垃圾回收机制浅理解
- mybatis与spring的整合
- 20145330孙文馨 《Java程序设计》第二周学习总结
- Java NIO: Non-blocking Server
- Java基础练习
- Java UDP 编程
- Java NIO ServerSocketChannel
- Spring IOC容器概念
- eclipse下maven项目保持原有目录结构配置resin运行环境
- 20145127《java程序设计》第二周学习总结
- java线程池ThreadPoolExecutor的使用
- Spring MVC中的视图解析ViewResolver
- java语法_1:面向对象:1
- FileReader文件字符输入流
- Java NIO SocketChannel
- Java NIO FileChannel