java nio ScatteringByteChannel,GatheringByteChannel源码分析

 ScatteringByteChannel


package java.nio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;


/**
 * 一种能将字节读入缓冲区序列的通道。
 *
 * <p> 分散读操作在一次调用中将一个字节集合读入一个或多个给定的缓冲区序列中。
 * 分散读取在实现网络协议或文件格式时通常是有用的,
 * 例如,将数据分组成段,由一个或多个固定长度的报头和可变长度的主体组成。
 * 在GatheringByteChannel接口中定义了类似的收集写操作。</p>
 * 
 * 
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface ScatteringByteChannel
    extends ReadableByteChannel
{

    /**
     * 从这个通道读取一个字节序列到给定缓冲区的子序列中。
     *
     * <p> 这个方法的调用尝试从这个通道读取最多r字节,
     * 其中r是保留给定缓冲区数组的指定子序列的总字节数,也就是说,
     *
     * <blockquote><pre>
     * dsts[offset].remaining()
     *     + dsts[offset+1].remaining()
     *     + ... + dsts[offset+length-1].remaining()</pre></blockquote>
     *
     * 在调用此方法时。
     *
     * <p> 假设一个字节序列长度为n被读取,0 < = n < = r。
     * 最多dsts[offset].remaining()个字节序列,被转入缓冲dsts[offset],
     * 最多dsts[offset+1].remaining()个字节序列,被转入缓冲dsts[offset+1],
     * 等等,直到整个字节数列转移到给定的缓冲区。
     * 将尽可能多的字节被转移到每个缓冲区,因此,除了最后更新的缓冲区外,
     * 每个被更新的缓冲的最终位置都保证等于该缓冲区的限制。
     *
     * <p> 此方法可以在任何时候调用。但是,如果另一个线程已经在这个通道上启动了读操作,那么对这个方法的调用将阻塞,直到第一个操作完成。</p>
     *
     * @param  dsts
     *         The buffers into which bytes are to be transferred
     *
     * @param  offset
     *         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 <tt>dsts.length</tt>
     *
     * @param  length
     *         The maximum number of buffers to be accessed; must be
     *         non-negative and no larger than
     *         <tt>dsts.length</tt>&nbsp;-&nbsp;<tt>offset</tt>
     *
     * @return The number of bytes read, possibly zero,
     *         or <tt>-1</tt> if the channel has reached end-of-stream
     *
     * @throws  IndexOutOfBoundsException
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
     *          parameters do not hold
     *
     * @throws  NonReadableChannelException
     *          If this channel was not opened for reading
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  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
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long read(ByteBuffer[] dsts, int offset, int length)
        throws IOException;

    /**
     * 从这个通道读取一个字节序列到给定的缓冲区。
     *
     * <p> 调用这种形式的c.read(dsts)方法的行为方式与调用完全相同
     *
     * <blockquote><pre>
     * c.read(dsts, 0, dsts.length);</pre></blockquote>
     *
     * @param  dsts
     *         The buffers into which bytes are to be transferred
     *
     * @return The number of bytes read, possibly zero,
     *         or <tt>-1</tt> if the channel has reached end-of-stream
     *
     * @throws  NonReadableChannelException
     *          If this channel was not opened for reading
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the read operation is in progress
     *
     * @throws  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
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long read(ByteBuffer[] dsts) throws IOException;

}

GatheringByteChannel


package java.nio.channels;

import java.io.IOException;
import java.nio.ByteBuffer;


/**
 * 一种可以从缓冲区序列中写入字节的通道。
 *
 * <p> 在一次调用中,收集写操作从一个或多个给定的缓冲区序列中写入字节。
 * 在实现网络协议或文件格式时,收集写操作通常很有用,
 * 例如,将数据分组到由一个或多个固定长度的头和可变长度的主体组成的段中。
 * 在ScatteringByteChannel接口中定义了类似的散射读取操作。</p>
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface GatheringByteChannel
    extends WritableByteChannel
{

    /**
     * 从给定缓冲区的子序列中写入一个字节序列到该通道。
     *
     * <p> 尝试向该通道写入最多r个字节,其中r是给定缓冲区数组的指定子序列中剩余的总字节数,即,
     *
     * <blockquote><pre>
     * srcs[offset].remaining()
     *     + srcs[offset+1].remaining()
     *     + ... + srcs[offset+length-1].remaining()</pre></blockquote>
     *
     * at the moment that this method is invoked.
     *
     * <p> 假设一个字节序列长度n是写,其中0 < = n < = r 。
     * 从srcs[offset],最多写入srcs[offset].remaining()字节缓冲区的字节序列,
     * 从srcs[offset+1],最多写入srcs[offset+1].remaining()字节缓冲区的字节序列,等等,直到整个字节序列经。
     * 从每个缓冲区写入尽可能多的字节,因此,每个更新缓冲区的最终位置,除了最后一个更新缓冲区,保证等于该缓冲区的限制。
     *
     * <p> 除非另有规定,写操作只会在写完所有r个请求的字节后返回。
     * 某些类型的通道,取决于它们的状态,可能只写一些字节,也可能根本不写。
     * 例如,在非阻塞模式下的套接字通道不能发送比套接字输出缓冲区中空闲的字节更多的字节。
     *
     * <p> 此方法可以在任何时候调用。
     * 但是,如果另一个线程已经在这个通道上启动了写操作,那么对这个方法的调用将阻塞,直到第一个操作完成。 </p>
     *
     * @param  srcs
     *         The buffers from which bytes are to be retrieved
     *
     * @param  offset
     *         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 <tt>srcs.length</tt>
     *
     * @param  length
     *         The maximum number of buffers to be accessed; must be
     *         non-negative and no larger than
     *         <tt>srcs.length</tt>&nbsp;-&nbsp;<tt>offset</tt>
     *
     * @return  The number of bytes written, possibly zero
     *
     * @throws  IndexOutOfBoundsException
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
     *          parameters do not hold
     *
     * @throws  NonWritableChannelException
     *          If this channel was not opened for writing
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the write operation is in progress
     *
     * @throws  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
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long write(ByteBuffer[] srcs, int offset, int length)
        throws IOException;


    /**
     * 从给定的缓冲区写入一个字节序列到此通道。
     *
     * <p> 调用这种形式的c.write(srcs)方法的行为方式与调用完全相同
     *
     * <blockquote><pre>
     * c.write(srcs, 0, srcs.length);</pre></blockquote>
     *
     * @param  srcs
     *         The buffers from which bytes are to be retrieved
     *
     * @return  The number of bytes written, possibly zero
     *
     * @throws  NonWritableChannelException
     *          If this channel was not opened for writing
     *
     * @throws  ClosedChannelException
     *          If this channel is closed
     *
     * @throws  AsynchronousCloseException
     *          If another thread closes this channel
     *          while the write operation is in progress
     *
     * @throws  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
     *
     * @throws  IOException
     *          If some other I/O error occurs
     */
    public long write(ByteBuffer[] srcs) throws IOException;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值