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
Java NIO Pipe的更多相关文章
- Java NIO学习笔记八 Pipe
Java NIO Pipe Java NIO管道是两个线程之间的单向数据连接.Pipe 具有源信道和接受通道.您将数据写入sink通道.然后可以从源通道读取该数据. 这是一个原理的Pipe流程图: J ...
- Java NIO 完全学习笔记(转)
本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...
- Java NIO 学习总结 学习手册
原文 并发编程网(翻译):http://ifeve.com/java-nio-all/ 源自 http://tutorials.jenkov.com/java-nio/index.html Java ...
- 海纳百川而来的一篇相当全面的Java NIO教程
目录 零.NIO包 一.Java NIO Channel通道 Channel的实现(Channel Implementations) Channel的基础示例(Basic Channel Exampl ...
- 转:Java NIO系列教程(九) Pipe
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 创建管道 通过Pi ...
- Java NIO (五) 管道 (Pipe)
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 如下图: 向管道写数据: 从管道读数据: 1. ...
- Java NIO -- 管道 (Pipe)
Java NIO 管道是2个线程之间的单向数据连接. Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 举个例子: package com.soyo ...
- Java NIO系列教程(十一) Pipe
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 创建管道 通过Pi ...
- Java NIO 学习笔记(五)----路径、文件和管道 Path/Files/Pipe
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
随机推荐
- cv2 与 matplotlib 的 Bug 记录
cv2 的 imread 无法读取中文路径 解决方案: img = cv2.imdecode(np.fromfile(image_path,dtype=np.uint8),cv2.IMREAD_COL ...
- odoo权限管理(二.记录管理)
规则保存在ir.rule模型表里,需要设置关联某个模型,关联很多组,访问权限控制和domian. 通过domain_force过滤出的一些记录来执行约束. 例子:经理只能删除状态为'cancel'的客 ...
- 关于XML解析中的CDATA的简单介绍
所有 XML 文档中的文本均会被解析器解析. 只有 CDATA 区段(CDATA section)中的文本会被解析器忽略. PCDATA PCDATA 指的是被解析的字符数据(Parsed Chara ...
- [JZOJ4786]小a的强迫症
[JZOJ4786]小a的强迫症 题目大意: 有\(n(n\le10^5)\)种颜色的珠子,第\(i\)种颜色有\(num[i]\)个.你要把这些珠子排成一排,使得第\(i\)种颜色的最后一个珠子一定 ...
- AMD K7以来核心架构一览表
转载或拿走使用请注明出处,谢谢! 注:K10以前AMD的CPU型号主要用PR值标称,故此表中未表示其准确型号
- Loj10154 选课
试题描述: 大学实行学分制.每门课程都有一定的学分,学生只要选修了这门课并通过考核就能获得相应学分.学生最后的学分是他选修各门课的学分总和.每个学生都要选择规定数量的课程.其中有些课程可以直接选修,有 ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- 51Nod 1092 回文字符串(LCS + dp)
51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...
- Java_正确理解ThreadLocal
首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的.各 ...
- 区别ES3ES5和ES6this的指向问题。区分普通函数和箭头函数中this的指向问题
ES3 ES5this的指向问题 this指的是该函数被调用的对象 var foo = function () { this.a = 'a', this.b = 'b', this.c = { a: ...
