Java NIO Scatter / Gather
2016-06-27 10:40
459 查看
Java NIO comes with built-in scatter / gather support. Scatter / gather are concepts used in reading from, and writing to channels.
A scattering read from a channel is a read operation that reads data into more than one buffer. Thus, the channel "scatters" the data from the channel into multiple buffers.
A gathering write to a channel is a write operation that writes data from more than one buffer into a single channel. Thus, the channel "gathers" the data from multiple buffers into one channel.
Scatter / gather can be really useful in situations where you need to work with various parts of the transmitted data separately. For instance, if a message consists of a header and a body, you might keep the header and body
in separate buffers. Doing so may make it easier for you to work with header and body separately.
Here is an illustration of the
Here is a code example that shows how to perform a scattering read:
Notice how the buffers are first inserted into an array, then the array passed as parameter to the
is full, the channel moves on to fill the next buffer.
The fact that scattering reads fill up one buffer before moving on to the next, means that it is not suited for dynamically sized message parts. In other words, if you have a header and a body, and the header is fixed size (e.g.
128 bytes), then a scattering read works fine.
Here is a code example that shows how to perform a gathering write:
is written. Thus, if a buffer has a capacity of 128 bytes, but only contains 58 bytes, only 58 bytes are written from that buffer to the channel. Thus, a gathering write works fine with dynamically sized message parts, in contrast to scattering reads.
link from : http://tutorials.jenkov.com/java-nio/scatter-gather.html
A scattering read from a channel is a read operation that reads data into more than one buffer. Thus, the channel "scatters" the data from the channel into multiple buffers.
A gathering write to a channel is a write operation that writes data from more than one buffer into a single channel. Thus, the channel "gathers" the data from multiple buffers into one channel.
Scatter / gather can be really useful in situations where you need to work with various parts of the transmitted data separately. For instance, if a message consists of a header and a body, you might keep the header and body
in separate buffers. Doing so may make it easier for you to work with header and body separately.
Scattering Reads
A "scattering read" reads data from a single channel into multiple buffers. Here is an illustration of that principle:Here is an illustration of the
Scatterprinciple:
Java NIO: Scattering Read |
RandomAccessFile aFile = new RandomAccessFile("C:/Users/Jack/Desktop/2.txt", "rw"); FileChannel inChannel = aFile.getChannel(); ByteBuffer headerbuffer = ByteBuffer.allocate(22); ByteBuffer bodyBuffer = ByteBuffer.allocate(15); ByteBuffer[] bufferArray = { headerbuffer, bodyBuffer}; long bytesRead = inChannel.read(bufferArray); while (bytesRead != -1) { bufferArray[0].flip(); bufferArray[1].flip(); while(bufferArray[0].hasRemaining()){ System.out.print((char) bufferArray[0].get()); } while(bufferArray[1].hasRemaining()){ System.out.print((char) bufferArray[1].get()); } bufferArray[0].clear(); bufferArray[1].clear(); bytesRead = inChannel.read(bufferArray); } aFile.close();
Notice how the buffers are first inserted into an array, then the array passed as parameter to the
channel.read()method. The
read()method then writes data from the channel in the sequence the buffers occur in the array. Once a buffer
is full, the channel moves on to fill the next buffer.
The fact that scattering reads fill up one buffer before moving on to the next, means that it is not suited for dynamically sized message parts. In other words, if you have a header and a body, and the header is fixed size (e.g.
128 bytes), then a scattering read works fine.
Gathering Writes
A "gathering write" writes data from multiple buffers into a single channel. Here is an illustration of that principle:Java NIO: Gathering Write |
ByteBuffer headerbuffer = ByteBuffer.allocate(22); headerbuffer.put("header:abcdefghijklmny".getBytes()); ByteBuffer bodyBuffer = ByteBuffer.allocate(15); bodyBuffer.put("body:mmkkllopyu".getBytes()); ByteBuffer[] bufferArray = { headerbuffer, bodyBuffer}; RandomAccessFile aFile = new RandomAccessFile("C:/Users/Jack/Desktop/3.txt", "rw"); FileChannel inChannel = aFile.getChannel(); headerbuffer.flip(); bodyBuffer.flip(); inChannel.write(bufferArray); aFile.close();The array of buffers are passed into the
write()method, which writes the content of the buffers in the sequence they are encountered in the array. Only the data between position and limit of the buffers
is written. Thus, if a buffer has a capacity of 128 bytes, but only contains 58 bytes, only 58 bytes are written from that buffer to the channel. Thus, a gathering write works fine with dynamically sized message parts, in contrast to scattering reads.
link from : http://tutorials.jenkov.com/java-nio/scatter-gather.html
相关文章推荐
- Java中如何遍历Map对象的4种方法
- myeclipse如何添加源码反编译工具插件
- 在Eclipse中生成接口的JUnit测试类
- springMVC的拦截器
- Java UDP 多用户 组播 聊天程序
- java根据word模板导出word文件
- Spring 的Scope
- JAVA基础
- java异常
- Java Spring @Scheduled tasks executing twice
- Java UDP 组播实现
- 11. ubuntu 16.04 LTS 安装Eclipse
- 10. ubuntu 16.04 LTS 安装Jdk
- 使用eclipse远程调试weblogic
- FreeMarker与Spring框架及SSM的整合过程
- JAVA String字符串倒序
- Spring的注入方式
- Java之HashCode值改变引起Hashset造成的内存泄露
- java排序算法
- FreeMarker与Spring框架及SSM的整合过程