Java NiO Learning Note Three scatter/collect and channel conversions

Source: Internet
Author: User

Java NiO scatter/collect

Java NiO comes with built-in scatter/gather support. Scatter/collect is a concept used in the process of reading and writing channels.

Scattering from a channel reads a read operation that reads data into multiple buffers. Therefore, the data can be "scattered" from the channel into multiple buffers.

A write to a channel is a write operation that writes data from multiple buffers to a single channel. As a result, data can be collected from multiple buffers into one channel.

scatter/collect can be very useful in situations where it is necessary to separate parts of the transmitted data. For example, if a message consists of a header and body, you can save the header and body in a separate buffer. This makes it easier for you to use the title and body individually.

Scatter Read

Scatter read reads data from a single channel into multiple buffers.

This is a flowchart of the principle Scatter :

Java NIO: Scatter read

This is a code example that shows how to perform scatter reads:

Bytebuffer Header = bytebuffer.allocate (+ = bytebuffer.allocate (1024x768= {header,body}; Channel.read (Bufferarray);

  First, you insert the buffer into the array, and then you pass the array as an argument to the channel.read() method. The read() method then writes data from the channel in the order in which the buffers are in the array. Once the buffer is full, the channel continues to run to populate the next buffer.

Scattering reads fill a buffer before moving to the next, which means that it is not suitable for dynamic-sized message parts. In other words, if you have a title and a subject, and the caption is a fixed size (for example: 128 bytes), then scatter reads work fine.

Collect Write

"Capture write" writes data from multiple buffers to a single channel.

The flowchart is as follows:

Java NIO: Collect write

This is a code example that shows how to perform a collect write:

Bytebuffer Header = bytebuffer.allocate (n= bytebuffer.allocate (1024x768); // write data to buffer  = {header,body};channel.write (bufferarray);

  An array of buffers is passed to the write() method, which writes the contents of the buffer in order, in sequence, in the array. Writes only data between the location of the buffer and the limit. Therefore, if the buffer has a capacity of 128 bytes, but contains only 58 bytes, then only 58 bytes are written to the channel from that buffer. Therefore, the data collected for dynamic size can work exactly as compared to scatter reads.

Java NIO Channel to channel transfer

In Java NIO, you can transfer data directly from one channel to another. If one of the channels is FileChannel通道 . This FileChannel class has a transferTo() and a transferFrom() method that can do the work.

Transferfrom () method

  FileChannel的transferFrom()method to transfer data from the source channel to the FileChannel .

Here are some simple examples:

New=New= tofile.getchannel ();
Long position= 0;
Long count =
Tochannel.transferfrom (Fromchannel,position,count);

The parameter position and quantity tells the destination file where to start writing ( position ), and count how many bytes to transmit (). If the source channel has count fewer bytes than the transmitted channel.

In addition, some SocketChannel implementations can only transmit SocketChannel data here and now that the internal buffers are ready-even though SocketChannel more data may be available later. Therefore, it may not pass in the entire data that is requested ( count ) SocketChannel FileChannel .

TransferTo () method

The transferTo() method is transmitted from one FileChannel other channel.

A simple example:

New=New= tofile.getchannel (); long position= 0; long count = fromchannel.size (); Fromchannel.transferto (Position,count,tochannel);

The example is similar to the previous one. The only real difference is the FileChannel object that invokes the method. The rest is the same.

SocketChannelThere are also problems with this approach transferTo() . The SocketChannel implementation may only be transferred from bytes FileChannel until the send buffer is full and then stopped.

Java NiO Learning Note Three scatter/collect and channel conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.