美文网首页Nio
NIO之四--Scatter / Gather

NIO之四--Scatter / Gather

作者: AlanKim | 来源:发表于2019-02-27 07:49 被阅读2次

    Java NIO Scatter / Gather

    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.

    scatter 从一个channel读取到多个buffer,传入read的是是一个数组即可,会自动按照数组中的顺序写入(一个写满,另一个才会继续)

    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.

    gather 多个buffer写入到一个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:

    scatter.png

    Java NIO: Scattering Read

    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.

    如果头部不满128 bytes,需要补位,不然会有部分body的数据写入到header中,造成数据错乱

    Gathering Writes

    A "gathering write" writes data from multiple buffers into a single channel. Here is an illustration of that principle:

    gather.png

    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 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.

    将多个buffer中的数据按buffer顺序写入到channel中。

    Scatter和Gather 都已经在channel的read、write方法中内建支持,不需要额外的设置及处理。

    相关文章

      网友评论

        本文标题:NIO之四--Scatter / Gather

        本文链接:https://www.haomeiwen.com/subject/gjqaeqtx.html