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