Most visited

Recently visited

Added in API level 1

GatheringByteChannel

public interface GatheringByteChannel
implements WritableByteChannel

java.nio.channels.GatheringByteChannel
Known Indirect Subclasses


可以从一系列缓冲区写入字节的通道。

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

Summary

Public methods

abstract long write(ByteBuffer[] srcs)

从给定的缓冲区中写入一个字节序列到这个通道。

abstract long write(ByteBuffer[] srcs, int offset, int length)

从给定缓冲区的子序列向此通道写入一个字节序列。

Inherited methods

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

Public methods

write

Added in API level 1
long write (ByteBuffer[] srcs)

从给定的缓冲区中写入一个字节序列到这个通道。

表单 c.write(srcs)的这种方法的调用的行为方式与调用完全相同

 c.write(srcs, 0, srcs.length);

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
Returns
long The number of bytes written, possibly zero
Throws
NonWritableChannelException If this channel was not opened for writing
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the write operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException If some other I/O error occurs

write

Added in API level 1
long write (ByteBuffer[] srcs, 
                int offset, 
                int length)

从给定缓冲区的子序列向此通道写入一个字节序列。

试图向该通道写入多达 r个字节,其中 r是给定缓冲器阵列的指定子序列中剩余的字节总数,也就是说,

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

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

除非另有说明,否则只有在写入所有r请求的字节后才会返回写入操作。 根据其状态,某些类型的通道可能只写入一些字节或可能根本没有。 例如,非阻塞模式下的套接字通道不能写入比套接字输出缓冲区中的空闲字节更多的字节。

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

Parameters
srcs ByteBuffer: The buffers from which bytes are to be retrieved
offset int: The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
length int: The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns
long The number of bytes written, possibly zero
Throws
IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold
NonWritableChannelException If this channel was not opened for writing
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the write operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the write 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!