Java NIO Scatter / Gather
来源:互联网 发布:java搭建游戏服务器 编辑:程序博客网 时间:2024/06/11 14:41
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.
Scattering Reads
A "scattering read" reads data from a single channel into multiple buffers. Here is an illustration of that principle:ByteBuffer header = ByteBuffer.allocate(128);ByteBuffer body = ByteBuffer.allocate(1024);ByteBuffer[] buffers = { header, body };channel.read(buffers);
Notice how the buffers are first inserted into an array, then the array passed as parameter to the
channel.read()
method. Theread()
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
Here is a code example that shows how to perform a gathering write:
ByteBuffer header = ByteBuffer.allocate(128);ByteBuffer body = ByteBuffer.allocate(1024);//write data into buffersByteBuffer[] buffers = { header, body };channel.write(buffers);
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. 本文转自:http://tutorials.jenkov.com/java-nio/scatter-gather.html
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO(5-Scatter / Gather)
- Java NIO (四) Scatter/Gather
- 【JAVA】【NIO】5、Java NIO Scatter / Gather
- Java NIO教程(四) Scatter & Gather
- java NIO教程(四)--Scatter/Gather
- Java NIO 之 Scatter 和gather
- Java的NIO之Scatter和Gather
- JAVA NIO Scatter/Gather(矢量IO)
- Java NIO的Scatter与Gather
- NIO - Scatter/Gather
- NIO - Scatter/Gather
- oracle数据库复习资料
- Latex 去掉段首的空格 下标
- Web 开源库
- 单播、多播(组播)和广播的区别
- 内核编程与应用编程对比
- Java NIO Scatter / Gather
- 分数化小数--gyy修改+补充+转载
- java的String的使用
- VS2010-使用“预先生成事件命令行”和“后期生成事件命令行”功能
- xz后缀名文件解压方法
- NMS:Non-maximum Suppression学习笔记
- SQL Server选择题40个
- 简单十步让你全面理解SQL
- 高性能、高流量互联网应用架构设计实战原则