Reads a sequence of bytes from this channel into a subsequence of the given buffers. This operation, sometimes called a
scattering read , is often useful when implementing network protocols that group data into segments consisting of one or more fixed-length headers followed by a variable-length body. The
handler
parameter is a completion handler that is invoked when the read operation completes (or fails). The result passed to the completion handler is the number of bytes read or
-1
if no bytes could be read because the channel has reached end-of-stream.
This method initiates a read of up to r bytes from this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,
dsts[offset].remaining()
+ dsts[offset+1].remaining()
+ ... + dsts[offset+length-1].remaining()
at the moment that the read is attempted.
Suppose that a byte sequence of length n is read, where 0
<
n <=
r. Up to the first dsts[offset].remaining()
bytes of this sequence are transferred into buffer dsts[offset]
, up to the next dsts[offset+1].remaining()
bytes are transferred into buffer dsts[offset+1]
, and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit. The underlying operating system may impose a limit on the number of buffers that may be used in an I/O operation. Where the number of buffers (with bytes remaining), exceeds this limit, then the I/O operation is performed with the maximum number of buffers allowed by the operating system.
If a timeout is specified and the timeout elapses before the operation completes then it completes with the exception InterruptedByTimeoutException
. Where a timeout occurs, and the implementation cannot guarantee that bytes have not been read, or will not be read from the channel into the given buffers, then further attempts to read from the channel will cause an unspecific runtime exception to be thrown.