java nio ReadableByteChannel,WritableByteChannel,ByteChannel源码分析

目录

ReadableByteChannel

WritableByteChannel

ByteChannel


ReadableByteChannel


package java.nio.channels;

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


/**
 * 可以读取字节的通道。
 *
 * <p> 在任何给定的时间内,可读通道上只能进行一个读操作。
 * 如果一个线程在一个通道上启动一个读操作,
 * 那么任何其他尝试启动另一个读操作的线程都会阻塞,直到第一个操作完成。
 * 其他类型的I/O操作是否可以与读取操作同时进行取决于通道的类型。</p>
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface ReadableByteChannel extends Channel {

    /**
     * 从这个通道读取一个字节序列到给定的缓冲区。
     *
     * <p> 尝试从通道中读取r字节,其中r是在调用此方法时缓冲区中剩余的字节数,即dst.remaining()。
     *
     * <p> 假设读取一个长度为n的字节序列, 并且 0 < = n < = r。
     * 字节序列将被转移到缓冲区,序列中的第一个字节对应缓冲区的索引为p,
     * 最后一个字节是指数p + n - 1, p是方法调用时,缓冲区的位置。
     * 返回后,缓冲区的位置将等于p + n;它的极限不会改变。
     *
     * <p> 读操作可能不会填充缓冲区,实际上它可能根本不会读取任何字节。
     * 它是否这样做取决于通道的性质和状态。
     * 一个套接字通道在非阻塞模式,例如,不能读取任何字节而不是立刻从套接字的输入缓冲区读取;
     * 类似地,文件通道只能读取文件中保留的字节。
     * 但是,可以保证的是,如果一个通道处于阻塞模式,并且缓冲区中至少有一个字节,
     * 那么这个方法将阻塞,直到至少一个字节被读取。
     *
     * <p> 此方法可以在任何时候调用。
     * 但是,如果另一个线程已经在这个通道上启动了读操作,那么对这个方法的调用将阻塞,直到第一个操作完成。</p>
     *
     * @param  dst
     *         The buffer 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 int read(ByteBuffer dst) throws IOException;

}

WritableByteChannel


package java.nio.channels;

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


/**
 * 一个可以写入字节的通道。
 *
 * <p> 在任何给定的时间内,在可写通道上只能进行一个写操作。
 * 如果一个线程在一个通道上启动了一个写操作,那么任何其他尝试启动另一个写操作的线程都会阻塞,直到第一个操作完成。
 * 其他类型的I/O操作是否可以与写操作同时进行,取决于通道的类型。</p>
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface WritableByteChannel
    extends Channel
{

    /**
     * 将一个字节序列从给定的缓冲区写入此通道。
     *
     * <p> 尝试将最多r字节写入到通道,其中r是在调用此方法时缓冲区中剩余的字节数,即src.remaining()。
     *
     * <p> 假设写入了一个长度为n的字节序列,其中0 <= n <= r。
     * 这个字节序列将从索引p开始的缓冲区中传输,其中p是调用此方法时缓冲区的位置;
     * 最后一个字节的索引将是p + n - 1。
     * 返回后,缓冲区的位置将等于p + n;它的极限不会改变。
     *
     * <p> 除非另有规定,写操作只会在写完所有r请求的字节后返回。
     * 某些类型的通道,取决于它们的状态,可能只写一些字节,也可能根本不写。
     * 例如,在非阻塞模式下的套接字通道不能发送比套接字输出缓冲区中空闲的字节更多的字节。
     *
     * <p> 此方法可以在任何时候调用。
     * 但是,如果另一个线程已经在这个通道上启动了写操作,那么对这个方法的调用将阻塞,直到第一个操作完成。</p>
     *
     * @param  src
     *         The buffer 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 int write(ByteBuffer src) throws IOException;

}

ByteChannel

package java.nio.channels;

import java.io.IOException;


/**
 * 一个可以读写字节的通道。
 * 该接口简单地统一了ReadableByteChannel和WritableByteChannel;
 * 它没有指定任何新的操作。
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface ByteChannel
    extends ReadableByteChannel, WritableByteChannel
{

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值