Java NIO Pipe
来源:互联网 发布:郑州明德软件开发 编辑:程序博客网 时间:2024/05/16 04:49
- Creating a Pipe
- Writing to a Pipe
- Reading from a Pipe
Jakob Jenkov
Last update: 2014-06-23
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:
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.
- Java NIO Pipe
- Java NIO(12-Pipe)
- Java NIO Pipe
- Java NIO (十一) Pipe
- Java NIO Pipe
- Java NIO(5)----Pipe
- Java NIO Pipe
- Java NIO Pipe
- 【JAVA】【NIO】12、Java NIO Pipe
- Java nio 之 管道pipe
- java NIO教程(十一)--Pipe
- Java NIO Pipe使用示例
- JAVA NIO中Pipe的通信能力
- Java NIO系列教程(十一) Pipe
- Java NIO系列教程(十一) Pipe
- Java NIO系列教程(十一) Pipe
- Java NIO系列教程(十一) Pipe
- Java NIO系列教程(十一) Pipe
- 约瑟夫问题与变形
- 优雅地调查删除你微信的人
- banq 质疑Lambda架构
- PHP笔记
- python tkinter界面中添加按钮的方法
- Java NIO Pipe
- C++ const用法 尽可能使用const
- C++处理异常技巧-try,catch,throw,finally
- Head First设计模式之观察者模式(Observer Pattern)
- SD/eMMC寄存器介绍
- 两个栈实现队列及两个队列实现栈
- 手机APP测试流程
- 习题2-5 分数化小数(decimal)
- [leetcode] Symmetric Tree--二叉树遍历的应用