Java NIO Scatter / Gather

本文详细介绍了Java NIO中散发读取和聚集写入的概念,这两种操作允许从单个通道读取到多个缓冲区或将多个缓冲区写入到单个通道,非常适合处理各种部分的数据,如消息头和正文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java NIO comes with built-in scatter / gather(分散/聚集) support. Scatter / gather are concepts(概念) used in reading from, and writing to channels.(通过buffer在数组中出现的顺序来实现的)

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 Scatter principle:

Here is a code example that shows how to perform a scattering read:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body   = ByteBuffer.allocate(1024);

ByteBuffer[] bufferArray = { header, body };

channel.read(bufferArray);

Notice how the buffers are first inserted into an array, then the array passed as parameter to thechannel.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:

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 buffers

ByteBuffer[] bufferArray = { header, body };

channel.write(bufferArray);

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.(对于动态数据也适合)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值