ByteBuffer操作简介

package java.nio;
public abstract class ByteBuffer
    extends Buffer
    implements Comparable<ByteBuffer>
{
    /*申请指定空间的ByteBuffer,DirectByteBuffer与HeapByteBuffer的区别稍后介绍*/
    public static ByteBuffer allocate(int capacity) {
        if (capacity < 0)
            throw new IllegalArgumentException();
        return new HeapByteBuffer(capacity, capacity);
    }
        public static ByteBuffer allocateDirect(int capacity) {
        // Android-changed: Android's DirectByteBuffers carry a MemoryRef.
        // return new DirectByteBuffer(capacity);
        DirectByteBuffer.MemoryRef memoryRef = new         DirectByteBuffer.MemoryRef(capacity);
        return new DirectByteBuffer(capacity, memoryRef);
    }

    /*Byte数组转ByteBuffer*/
    public static ByteBuffer wrap(byte[] array,
                                    int offset, int length)
    {
        try {
            return new HeapByteBuffer(array, offset, length);
        } catch (IllegalArgumentException x) {
            throw new IndexOutOfBoundsException();
        }
    }
    public static ByteBuffer wrap(byte[] array) {
        return wrap(array, 0, array.length);
    }

    /*ByteBuffer转Byte数组*/
    public abstract Object array();

    /*get 方法,会改变position,remain不足会抛BufferUnderflowException异常*/
    public ByteBuffer get(byte[] dst, int offset, int length) {
        checkBounds(offset, length, dst.length);
        if (length > remaining())
            throw new BufferUnderflowException();
        int end = offset + length;
        for (int i = offset; i < end; i++)
            dst[i] = get(); //这个不带参数的get将会调用HeapByteBuffer的get方法,所以他会需要自增position.
        return this;
    }

    public final int remaining() {
        return limit - position;
    }
    
    /*put方法,会改变position,抛出BufferOverflowException异常*/
    public abstract ByteBuffer put(byte b);
    public abstract ByteBuffer put(int index, byte b);
    /*当参数为ByteBuffer时,参数中的src的position也会改变,如果目标buffer剩余位置小于*/
    public ByteBuffer put(ByteBuffer src) {
        if (src == this)
            throw new IllegalArgumentException();
        if (isReadOnly())
            throw new ReadOnlyBufferException();
        int n = src.remaining();
        if (n > remaining())
            throw new BufferOverflowException();
        ......
    }
    public ByteBuffer put(byte[] src, int offset, int length) {
        checkBounds(offset, length, src.length);
        if (length > remaining())
            throw new BufferOverflowException();
        int end = offset + length;
        for (int i = offset; i < end; i++)
            this.put(src[i]);
        return this;
    }

    /*rewind,重置position*/
    public Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }
/*flip方法要注意,他不但会改变position,还会改变limit,所以在put、get、remaining前后用这个要慎重*/
    public Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

    /*position,直接设置position*/
    public Buffer position(int newPosition) {
        if ((newPosition > limit) || (newPosition < 0))
            // Android-changed: Improved error message.
            throw new IllegalArgumentException("Bad position " + newPosition + "/" + limit);
        position = newPosition;
        if (mark > position) mark = -1;
        return this;
    }


}

ByteBuffer有两种实现方式:HeapByteBuffer基于Java堆的实现,而DirectByteBuffer 使用了 unsafed 的API 进行了对外的实现。HeapByteBuffer就是创建效率高,读取和写入的效率低。DirectByteBuffer是创建效率低,读取和写入的效率高。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值