java.nio.Buffer源码阅读

Java 自从 JDK1.4 起,对各种 I/O 操作使用了 Buffer 和 Channel 技术。这种更接近于操作系统的的底层操作使得 I/O 操作速度得到大幅度提升,下面引用一段《Java 编程思想》对于 Buffer(缓冲器)和 Channel 的形象化解释。

我们可以将它想象成一个煤矿,Channel(通道)是一个包含煤层(数据)的矿藏,而 Buffer(缓冲器)则是派送到矿藏的卡车。卡车满载煤炭而归,我们再从卡车上获得煤炭。也就是说,我们并没有直接和 Channel 交互;我们只是和 Buffer 交互,并把 Buffer 派送到 Channel。Channel 要么从 Buffer 获得数据,要么向 Buffer 发送数据。

本片博文简单的介绍一下 java.nio 包中的 Buffer 抽象类。

一、类的介绍

(一)概述

首先 Buffer 是一个能存储基本数据类型的容器(除了 Boolean 类型),从 java.nio 包的继承结构就能看出来。

但是它的子类没有 BooleanBuffer 这样一个类。

Buffer 是一个线性、有序的基本类型元素组成的序列。 除了其中存储的元素,Buffer 类还有 4 个重要的成员属性:

  • capacity:它表示一个 Buffer 包含的元素数量,它是非负且恒定不变的。
  • position:它是下一个要读或者写的元素的索引,它是非负的且不会超过 limit 的大小。
  • limit:它是可以读或者写的最后一个元素的索引,它是非负的且不会超过 capacity 的大小。
  • mark:当调用 reset() 方法被调用时,一个 Buffer 的 mark 值会被设定为当前的 position 值的大小。

综合上面,可以得出这样一个永远成立的公式:0 <= mark <= position <= limit <= capacity

通过这 4 个属性,间接的实现了 Buffer 中的读写保护机制。

一个新创建的 Buffer 具有以下几个性质:

  • 它的 position 是 0;
  • mark 没有被定义(实际上是 -1);
  • 而 limit 值可能是 0,也可能是其他值,这取决于这个 Buffer 的类型;
  • Buffer 中每一个元素值都被初始化为 0

(二)数据传输

Buffer 类的每一个子类都有两套 put 和 get 操作,以便向 Buffer 中写数据,或从 Buffer 中读数据。

  • (Relative opetations)相对操作:这种操作会从当前的 position 位置开始读写一个或多个元素,position 会按照读写的元素个数值增加。
  • (Absolute opetations)绝对操作:绝对操作要指明读写的元素索引,读写元素后,当前 Buffer 的 position 值不变。

上面说的是我们直接与 Buffer 打交道的方式。开篇就说过,Channel 也能操作 Buffer 中的数据,要注意的是这个 Channel 是与当前 Buffer 的 position 位置相关联的 Channel。

(三)一些重要的方法

在学习这些方法前先来看一张图:

因为在创建 Buffer 的时候并不会区分创建的是读 Buffer 还是写 Buffer,所以就需要一种机制来区分当前 Buffer 正在被读还是正在被写,即所谓的读模式与写模式。

利用 position 和 limit 这两个属性就能很好的做出这种区分,从上面的图就能看出来,当 limit = capacity 的时候说明可以向当前 Buffer 写入数据;当二者不等时就可以从 Buffer 中读取数据。

每次读写模式的转换 position 都被重置为 0,下面的 3 个方法就是完成读写模式转换的 3 种途径。

  • clear( ) :调用 clear( ) 方法后,我们就可以向 Buffer 里面 put 数据,或者 Channel 能读取 Buffer 里的数据,并且将 limit 设置为 capacity,将 position 设置为 0。 
  • flip( ) :调用 flip( ) 方法后,我们就可以向 Buffer 里面 get 数据,或者 Channel 能向 Buffer 里写数据,并且将 limit 设置为 position,将 position 设置为 0。 
  • rewind( ) :调用 flip( ) 方法后,使 Buffer 里的数据可以被重新读取(无论是我们从 Buffer 读,还是 Channel 从 Buffer 读),此时 limit 不变,将 position 设置为 0。 

(四)Buffer 的读写性

Buffer 分为:① 只读 Buffer    ② 读写 Buffer

每个 Buffer 都是可读的,但是不是每个 Buffer 都是可写的。

只读 Buffer 的内容是不能变化,但是 mark,limit,position 值是可变的。

(五)线程安全性

在多线程操作下 Buffer 是不安全的,所以在多线程操作同一个 Buffer 的时候要使用相应的线程同步操作。

(六)Buffer 的数组形式

@ TODO

 二、类的源代码

public abstract class Buffer {

    /**
     * The characteristics of Spliterators that traverse and split elements
     * maintained in Buffers.(这个属性我目前还没弄懂是干嘛用的) 
     */
    static final int SPLITERATOR_CHARACTERISTICS =
        Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED;

    // 注意只有 mark 和 position 有初始值
    private int mark = -1;
    private int position = 0;
    private int limit;
    private int capacity;

    // 这个属性只有在当前 Buffer 为 Direct Buffer 时才会使用
    long address;
  // 创建一个指定属性值的 Buffer,且参数的大小必须符合上面所讲到的大小顺序规范(会在构造方法里进行检查)
    Buffer(int mark, int pos, int lim, int cap) {       // package-private
        if (cap < 0)
            throw new IllegalArgumentException("Negative capacity: " + cap);
        this.capacity = cap;
        limit(lim);
        position(pos);
        if (mark >= 0) {
            if (mark > pos)
                throw new IllegalArgumentException("mark > position: ("
                                                   + mark + " > " + pos + ")");
            this.mark = mark;
        }
    }

    public final int capacity() {
        return capacity;
    }

    public final int position() {
        return position;
    }

    /**     *  重新设置 Buffer 的 position,并检查其大小是否符合规范
     */
    public final Buffer position(int newPosition) {
        if ((newPosition > limit) || (newPosition < 0))
            throw new IllegalArgumentException();
        position = newPosition;
        if (mark > position) mark = -1;
        return this;
    }

    public final int limit() {
        return limit;
    }

    /**
     *  同 position
     */
    public final Buffer limit(int newLimit) {
        if ((newLimit > capacity) || (newLimit < 0))
            throw new IllegalArgumentException();
        limit = newLimit;
        if (position > limit) position = limit;
        if (mark > limit) mark = -1;
        return this;
    }

    /**
     *  将 mark 设置为当前 position,并返回当前 Buffer*/
    public final Buffer mark() {
        mark = position;
        return this;
    }

    /**
     * 将当前的 position 重新指向 mark 所指的元素,并且 mark 值不变
     */
    public final Buffer reset() {
        int m = mark;
        if (m < 0)
            throw new InvalidMarkException();
        position = m;
        return this;
    }

    /**
     * 这个方法的原理上面已经讲过,下面是它的使用方法
     * buf.clear();     // 
     * in.read(buf);    // in 可以是一个 Channel 及其子类的一个实例,表示从 Channel 中读取数据后写入 Buffer 中
     * 注意这个方法并不会使 Buffer 中的数据清空,只是 Buffer 中的读写保护机制
     * 使这个操作看起来像是将 Buffer 中的内容清空了,实际上只是覆盖了原有的值而已*/
    public final Buffer clear() {
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }

    /**
     * 这个方法的原理上面也说过了
     * 具体可以像下面这样使用
     * buf.put(magic);    // 内存向 Buffer 中存放数据(我们直接打交道的是 Buffer)
     * in.read(buf);      // Channel 也向 Buffer 中存放数据
     * buf.flip();        // Flip buffer,让 Buffer 做好被读取数据的准备
     * out.write(buf);    // Channel 从 Buffer 中读取数据
     */
    public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

    /**
     * 具体使用如下:
     * out.write(buf);    // 将 Buffer 中的数据写入 Channel
     * buf.rewind();      // Rewind buffer
     * buf.get(array);    // 
     */
    public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }

    /**
     * 返回当前 Buffer 还有多少数据可供读写*/
    public final int remaining() {
        return limit - position;
    }

    public final boolean hasRemaining() {
        return position < limit;
    }

    public abstract boolean isReadOnly();

    /**
     * 判断 Buffer 是否是由可获取的数组构成
     * 如果这个方法返回 true,那说明这个 Buffer 是可读写的
     * 且 array() 和 arrayOffset() 方法可以安全的调用
     * @since 1.6
     */
    public abstract boolean hasArray();

    /**
     * 返回这个 Buffer 的数组形式(optional operation)
     * 这个方法是为了让数组 Buffer 更有效的传递给 native code
     * Buffer 的子类会实现这个方法,并返回子类 Buffer 里所保存的基本类型的数组
     *
     * 改变 Buffer 的内容也会造成对数组内容的改变,反之亦然
     *
     * 一般在调用这个方法之前会调用 hasArray() 方法,以确保能获取到数组内容
     *
     * @since 1.6
     */
    public abstract Object array();

    /**
     * Returns the offset within this buffer's backing array of the first
     * element of the buffer(optional operation).
     *
     * If this buffer is backed by an array then buffer position p
     * corresponds to array index p + arrayOffset().
     *
     * <p> Invoke the {@link #hasArray hasArray} method before invoking this
     * method in order to ensure that this buffer has an accessible backing
     * array.  </p>
     *
     * @return  The offset within this buffer's array
     *          of the first element of the buffer
     *
     * @throws  ReadOnlyBufferException
     *          If this buffer is backed by an array but is read-only
     *
     * @throws  UnsupportedOperationException
     *          If this buffer is not backed by an accessible array
     *
     * @since 1.6
     */
    public abstract int arrayOffset();

    /**     * 鉴别 Buffer 是否为 Direct Buffer
     * @since 1.6
     */
    public abstract boolean isDirect();


    // -- 下面是几个包内的私有方法,了解一下即可. --


    final int nextGetIndex() {                          // package-private
        if (position >= limit)
            throw new BufferUnderflowException();
        return position++;
    }

    final int nextGetIndex(int nb) {                    // package-private
        if (limit - position < nb)
            throw new BufferUnderflowException();
        int p = position;
        position += nb;
        return p;
    }

    final int nextPutIndex() {                          // package-private
        if (position >= limit)
            throw new BufferOverflowException();
        return position++;
    }

    final int nextPutIndex(int nb) {                    // package-private
        if (limit - position < nb)
            throw new BufferOverflowException();
        int p = position;
        position += nb;
        return p;
    }

    final int checkIndex(int i) {                       // package-private
        if ((i < 0) || (i >= limit))
            throw new IndexOutOfBoundsException();
        return i;
    }

    final int checkIndex(int i, int nb) {               // package-private
        if ((i < 0) || (nb > limit - i))
            throw new IndexOutOfBoundsException();
        return i;
    }

    final int markValue() {                             // package-private
        return mark;
    }

    final void truncate() {                             // package-private
        mark = -1;
        position = 0;
        limit = 0;
        capacity = 0;
    }

    final void discardMark() {                          // package-private
        mark = -1;
    }

    static void checkBounds(int off, int len, int size) { // package-private
        if ((off | len | (off + len) | (size - (off + len))) < 0)
            throw new IndexOutOfBoundsException();
    }

}

 (完)

转载于:https://www.cnblogs.com/KKSJS/p/9909587.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值