Most visited

Recently visited

Added in API level 1

ScatteringByteChannel

public interface ScatteringByteChannel
implements ReadableByteChannel

java.nio.channels.ScatteringByteChannel
Known Indirect Subclasses


可以将字节读入缓冲区序列的通道。

散列读操作在单次调用中将一系列字节读入一个或多个给定的缓冲区序列中。 在实现网络协议或文件格式时,散列读取通常很有用,例如,将数据分组为包含一个或多个固定长度标头,后跟可变长度主体的段。 类似的采集写入操作在GatheringByteChannel界面中定义。

Summary

Public methods

abstract long read(ByteBuffer[] dsts, int offset, int length)

从该通道读取一系列字节到给定缓冲区的子序列中。

abstract long read(ByteBuffer[] dsts)

从该通道读取一系列字节到给定的缓冲区中。

Inherited methods

From interface java.nio.channels.ReadableByteChannel
From interface java.nio.channels.Channel
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public methods

read

Added in API level 1
long read (ByteBuffer[] dsts, 
                int offset, 
                int length)

从该通道读取一系列字节到给定缓冲区的子序列中。

此方法的调用尝试从该通道读取多达 r个字节,其中 r是给定缓冲区数组中指定子序列剩余的字节总数,也就是说,

 dsts[offset].remaining()
     + dsts[offset+1].remaining()
     + ... + dsts[offset+length-1].remaining()
at the moment that this method is invoked.

假设读取长度为n的字节序列,其中0 <= n <= r 直到该序列的第一个dsts[offset].remaining()字节被传送到缓冲区dsts[offset] ,直到下一个dsts[offset+1].remaining()字节被传送到缓冲区dsts[offset+1]等等,直到整个字节序列被传送到给定缓冲区。 尽可能多的字节传输到每个缓冲区,因此每个更新缓冲区的最终位置(除了最后更新的缓冲区)都保证等于该缓冲区的限制。

这个方法可以在任何时候调用。 然而,如果另一个线程已经在这个通道上启动了一个读操作,那么这个方法的调用将会阻塞,直到第一个操作完成。

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
offset int: The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold
NonReadableChannelException If this channel was not opened for reading
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the read operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException If some other I/O error occurs

read

Added in API level 1
long read (ByteBuffer[] dsts)

从该通道读取一系列字节到给定的缓冲区中。

调用表单 c.read(dsts)的此方法的行为与调用完全相同

 c.read(dsts, 0, dsts.length);

Parameters
dsts ByteBuffer: The buffers into which bytes are to be transferred
Returns
long The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws
NonReadableChannelException If this channel was not opened for reading
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the read operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException If some other I/O error occurs

Hooray!