io-1-buffer

buffer基础

        1.容量(Capacity):缓冲区能够容纳的数据元素的最大数量,这一容量在缓冲区创建时被设定,并且永远不能被改变。
        2.上界(Limit):缓冲区的第一个不能被读或被写的元素,或者说,缓冲区现存元素的计数。
        3.位置(Position):下一个要读或要写的元素的索引。
        4.标记(Mark):一个备忘位置,调用mark mark = position,调用reset position = mark。
        //大小排序
    0  mark  postion  limit  capacity   
可读
        //isReadOnly  是否只可读
        ByteBuffer byteBuffer=ByteBuffer.allocate(100);
        System.out.println(byteBuffer.isReadOnly()); //输出 false
存取
    put:将元素放入缓存中,position后移
    get:获取当前position的元素,position后移
put
    //源码: put(byte x) 方法源码
    //根据断点,默认是走HeapByteBuffer 
     public ByteBuffer put(byte x) {
        hb[ix(nextPutIndex())] = x;   //按position位置放入缓存数据
        return this;
    }

    public ByteBuffer put(int i, byte x) {
       hb[ix(checkIndex(i))] = x;   //在指定位置放入缓存数据
       return this;
    }

     //父类buffer方法
     final int nextPutIndex() { 
        if (position >= limit)
            throw new BufferOverflowException();
        return position++;   //下一个位置后移 
    }

    final int checkIndex(int i) {             
        if ((i < 0) || (i >= limit))
            throw new IndexOutOfBoundsException();
        return i;       //在指定位置设置元素
    }
get
    //获取当前位置的buffer数据
    public byte get() { 
        return hb[ix(nextGetIndex())];  //位置仍将后移一位
    }
    //获取指定位置的缓存元素
    public byte get(int i) {
        return hb[ix(checkIndex(i))];   //位置不变
    }
test
        System.out.println("postion : " + buffer.position());
        buffer.put((byte) 'a');
        System.out.println("postion : " + buffer.position());
        buffer.put((byte) 23);
        System.out.println("postion : " + buffer.position());
        buffer.put((byte) 189);
        System.out.println("postion : " + buffer.position());
        System.out.println(buffer.get());
        buffer.put(4, (byte)12);
        System.out.println("postion : " + buffer.position());
        System.out.println(buffer.get());
        System.out.println("postion : " + buffer.position());
        System.out.println(buffer.get(1));
        //输出
        postion : 0
        postion : 1
        postion : 2
        postion : 3
        0
        postion : 4
        12
        postion : 5
        23
翻转
    flip:将一个能够继续添加数据元素的填充状态的缓冲区翻转成一个准备读出元素的释放状态
    rewind:重读已经被翻转的缓冲区中的数据
flip
public final Buffer flip() {
        limit = position;    //将上限置为当前position
        position = 0;        //将position置为0
        mark = -1;           //标记置为-1
        return this;
    }
  flip 等效于 buffer.limit(buffer.position()).position(0);
rewind
 public final Buffer rewind() {
       position = 0;        //将position置为0
        mark = -1;           //标记置为-1
       return this;
    }
    rewind 等效于 buffer.position(0);
test
        ByteBuffer buffer=ByteBuffer.allocate(100);
        System.out.println("postion : " + buffer.position());
        buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o') ;
        System.out.println("postion : " + buffer.position());
        System.out.println(buffer.get());
        buffer.flip();  //翻转  // flip = buffer.limit(buffer.position()).position(0);
        System.out.print((char)buffer.get());
        System.out.print((char)buffer.get());
        System.out.print((char)buffer.get());
        System.out.print((char)buffer.get());
        System.out.println((char)buffer.get());
        System.out.println("postion : " + buffer.position());
        buffer.rewind();     //重读
        System.out.println("postion : " + buffer.position());

//输出
postion : 0
postion : 5
0
hello
postion : 5
postion : 0
释放
hasRemaining
 public final boolean hasRemaining() {
        return position < limit;    //当前位置小于limit
    }
remaining
public final int remaining() {
        return limit - position;    //上限 - 位置
    }
test
        ByteBuffer buffer=ByteBuffer.allocate(100);
        buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o') ;
        System.out.println("remaining :" +buffer.remaining());  //limit - position = 100 - 5
        buffer.flip();   //limit = 5 position = 0
        System.out.println("remaining :" +buffer.remaining());   //5 - 0
        while (buffer.hasRemaining()) {
            System.out.print((char)buffer.get());
        }
        buffer.rewind();
        System.out.println();
        while (buffer.hasRemaining()) {
            System.out.print((char)buffer.get());
        }
        //输出
        remaining :95
        remaining :5
        hello
        hello
压缩
    compact:丢弃已经释放的数据,保留未释放的数据,并使缓冲区对重新填充容量准备就绪。
compact
    public ByteBuffer compact() {

        System.arraycopy(hb, ix(position()), hb, ix(0), remaining());  //copy数组,消除已读元素
        position(remaining());   //将位置置为未读元素末
        limit(capacity());       //将上限置为capacity  可以继续扩充元素
        discardMark();
        return this;
    }
test
        ByteBuffer buffer=ByteBuffer.allocate(100);
        buffer.put((byte) 'h').put((byte) 'e').put((byte) 'l').put((byte) 'l').put((byte) 'o') ;
        buffer.flip();
        System.out.println((char)buffer.get());
        System.out.println((char)buffer.get());
        buffer.compact();   // position = remaining  limit = capacity mark = -1
        buffer.put((byte) '&');   // 继续在扩充缓冲元素
        buffer.flip();
        while (buffer.hasRemaining()) {
            System.out.print((char)buffer.get());
        }
        //输出
        h
        e
        llo&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值