Java NIO Pipe
来源:互联网 发布:mvod.php怎么播放 编辑:程序博客网 时间:2024/05/16 01:31
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.
Pipe pipe = Pipe.open();SinkChannel sinkChannel = pipe.sink();ByteBuffer byteBuffer = ByteBuffer.allocate(50);byteBuffer.clear();byteBuffer.put("hello world".getBytes());byteBuffer.flip();while(byteBuffer.hasRemaining()){sinkChannel.write(byteBuffer);}SourceChannel source = pipe.source();ByteBuffer byteBufferRead = ByteBuffer.allocate(50);byteBufferRead.clear();int read = source.read(byteBufferRead);while (read != -1) {byteBufferRead.flip();while(byteBufferRead.hasRemaining()){System.out.print((char)byteBufferRead.get());}byteBufferRead.clear();source.read(byteBufferRead);}
link from : http://tutorials.jenkov.com/java-nio/pipe.html
0 0
- 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
- App Store审核被拒的23个理由
- Leap Motion 之Unity 开发实战(一. 制作手的HandController)
- android gps hal
- 谷歌官方的下拉刷新控件SwipeRefreshLayout(带自主刷新)
- Linux/ubuntu下安装QQ与卸载QQ
- Java NIO Pipe
- afafaf
- shell 初识
- php连接ORACLE数据库问题解决办法
- 测试某一端口是否开启
- ReentrantLock(重入锁)以及公平性
- 二分法和牛顿迭代法
- 利用KVC写的一个分类实现自动生成模型属性代码
- Java中Runnable和Thread的区别