(二)理解Buffer

  1. 内附备注,大概了解一下Buffer类的实现

public abstract class Buffer {

    // Invariants: mark <= position <= limit <= capacity
   /**
   一个备忘位置。调用 mark( )来设定 mark = postion。调用 reset( )设定 position = mark。标记在设定前是未定义的(undefined)。
   **/
    private int mark = -1;
    /**
    下一个要被读或写的元素的索引。Buffer类提供了get( )和 put( )函数 来读取或存入数据,position位置会自动进行相应的更新。
    **/
    private int position = 0;
   /**
   缓冲区的第一个不能被读或写的元素。或者说,缓冲区中现存元素的计数。
   **/ 
    private int limit;
    /**
    *缓冲区能够容纳的数据元素的最大数量,可以理解为数组的长度。 这一容量在缓冲区创建时被设定,并且永远不能被改变。
   **/
    private int capacity;

    /**
     * Returns this buffer's capacity.
     *返回此缓冲区的容量
     * @return  The capacity of this buffer

     */
    public final int capacity() {
        return capacity;
    }

    /**
     * Returns this buffer's position.
     *返回此缓冲区的位置
     * @return  The position of this buffer
     */
    public final int position() {
        return position;
    }

    /**
     * Sets this buffer's position.  If the mark is defined and larger than the
     *设置此缓冲区的位置
 **/
    public final Buffer position(int newPosition) {
        if ((newPosition > limit) || (newPosition < 0))
            throw new IllegalArgumentException();
        position = newPosition;
        if (mark > position) mark = -1;
        return this;
    }

    /**
     * Returns this buffer's limit.
     *返回此缓冲区的限制
     * @return  The limit of this buffer
     */
    public final int limit() {
        return limit;
    }

    /**
     * Sets this buffer's limit.  If the position is larger than the new limit
     *设置此缓冲区的限制
     * @return  This buffer

     */
    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;
    }

    /**
     * Sets this buffer's mark at its position.
     *在此缓冲区的位置设置标记
     * @return  This buffer
     */
    public final Buffer mark() {
        mark = position;
        return this;
    }

    /**
     * Resets this buffer's position to the previously-marked position.
     *将此缓冲区的位置重置为以前标记的位置
     * <p> Invoking this method neither changes nor discards the mark's
     * value. </p>
     *
     * @return  This buffer
     *
     * @throws  InvalidMarkException
     *          If the mark has not been set
     */
    public final Buffer reset() {
        int m = mark;
        if (m < 0)
            throw new InvalidMarkException();
        position = m;
        return this;
    }

    /**
     * Clears this buffer.  The position is set to zero, the limit is set to
     * the capacity, and the mark is discarded.
     *
			清除此缓冲区
     * @return  This buffer
     */
    public final Buffer clear() {
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }

    /**
     * Flips this buffer.  The limit is set to the current position and then
     * the position is set to zero.  If the mark is defined then it is
     * discarded.
		反转此缓冲区
     * @return  This buffer
     */
    public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

    /**
     * Rewinds this buffer.  The position is set to zero and the mark is
     * discarded.
    重绕此缓冲区
     * @return  This buffer
     */
    public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }

    /**
     * Returns the number of elements between the current position and the
     * limit.
     *返回当前位置与限制之间的元素数
     * @return  The number of elements remaining in this buffer
     */
    public final int remaining() {
        return limit - position;
    }

    /**
     * Tells whether there are any elements between the current position and
     * the limit.
     *告知在当前位置和限制之间是否有元素
     * @return  <tt>true</tt> if, and only if, there is at least one element
     *          remaining in this buffer
     */
    public final boolean hasRemaining() {
        return position < limit;
    }

    /**
     * Tells whether or not this buffer is read-only.
     *告知此缓冲区是否为只读缓冲区
     * @return  <tt>true</tt> if, and only if, this buffer is read-only
     */
    public abstract boolean isReadOnly();

    /**
     * Tells whether or not this buffer is backed by an accessible
     * array.
     *告知此缓冲区是否具有可访问的底层实现数组
     * @since 1.6
     */
    public abstract boolean hasArray();

    /**
     * Returns the array that backs this
     * buffer&nbsp;&nbsp;<i>(optional operation
     * 返回此缓冲区的底层实现数组
     * @since 1.6
     */
    public abstract Object array();

    /**
     * Returns the offset within this buffer's backing array of the first
	返回此缓冲区的底层实现数组中第一个缓冲区元素的偏移量
     * @since 1.6
     */
    public abstract int arrayOffset();

    /**
     * Tells whether or not this buffer is
		告知此缓冲区是否为直接缓冲区
     *
     * @since 1.6
     */
    public abstract boolean isDirect();
}
  1. Buffer特定类的实现
    在这里插入图片描述

3.ByteBuffer
其常规方法有:

     ByteBuffer allocate(int capacity) //创建一个指定capacity的ByteBuffer。
     ByteBuffer allocateDirect(int capacity) //创建一个direct的ByteBuffer,这样的ByteBuffer在参与IO操作时性能会更好
     ByteBuffer wrap(byte [] array)
     ByteBuffer wrap(byte [] array, int offset, int length) //把一个byte数组或byte数组的一部分包装成ByteBuffer。
     //get put方法不多说
     byte get(int index)
     ByteBuffer put(byte b)
     int getInt()             //从ByteBuffer中读出一个int值。
     ByteBuffer putInt(int value)  // 写入一个int值到ByteBuffer中。

举个栗子: 创建的容量为 10 的 ByteBuffer

ByteBuffer.allocate(10);

在这里插入图片描述
1) 写入数据到缓冲区

buffer.put((byte)'a').put((byte)'b').put((byte)'c').put((byte)'d').put((byte)'e');

abcde字符串以ASCII码put进buffer
在这里插入图片描述
2)读取缓冲区中数据,需要先进行flip()方法,将postion复位到0,并将limit设为当前postion。
在这里插入图片描述
然后get()方法读取,从buffer里读一个字节,并把postion移动一位。上限是limit,即写入数据的最后位置。
3)clear():将position置为0,并不清除buffer内容。
4) mark() & reset():mark相关的方法主要是mark()(标记)和reset()(回到标记).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值