"Java" "NiO" 6, Java NIO Channel to channel transfers

Source: Internet
Author: User

In Java NiO, if one of the channel is FileChannel, you can pass data directly from one channel to another channel. The FileChannel class has a transferto and a transferfrom method that can be done.

Transferfrom ()

This method passes the data from one source channel to the FileChannel.
Instance:

The parameters position and count, indicating where in the destination file The write begins, and the maximum number of bytes (count) of data transferred. If the data in the source channel is less than the count byte, the transmitted data will be reduced accordingly.

In addition, some socketchannel implementations transmit only those data that is in Socketchannel's internal buffer at this moment, even though Socketchannel will have more data later. Therefore, it does not transfer the entire request data (count) from Socketchannel to FileChannel.

TransferTo ()

This method transmits data from FileChannel to other channel.
Instance:

Note that the above example is similar to the previous example. The only difference is that the FileChannel method is called, and the others are the same.

The question about Socketchannel also exists for Transferto. The implementation of Socketchannel only passes data from FileChannel, knowing that the sent buffer is full before it stops.

"Java" "NiO" 6, Java NIO Channel to channel transfers

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.