ByteBuffer的实验

て烟熏妆下的殇ゞ 提交于 2020-03-11 10:17:48
//代码参考 https://blog.csdn.net/coding_1994/article/details/87902828
        ByteBuffer buff = ByteBuffer.allocate(1024);
        String str = "helloWorld";
        buff.put(str.getBytes());
        System.out.println(new String(buff.array()));
        System.out.println("position:" + buff.position() + "\t limit:"
                + buff.limit() + "\t capacity:" + buff.capacity());
        // 读取两个字节byte[] abytes = new byte[1];
        byte[] abytes = new byte[1];
        buff.get(abytes);//其实是byte 0值
        System.out.println("get one byte to string:" + new String(abytes));
        // Reads the byte at this buffer's current position, and then increments
        // the position.
        buff.get();//其实也是byte 0值
        System.out.println("获取两个字节(两次get()方法调用)后");
        System.out.println("position:" + buff.position() + "\t limit:"
                + buff.limit());
        // Sets this buffer's mark at its position. like
        // ByteBuffer.this.mark=position
        buff.mark();
        System.out.println("mark()...");
        System.out.println("position:" + buff.position() + "\t limit:"
                + buff.limit());

        // 当读取到码流后,进行解码。首先对ByteBuffer进行flip操作,
        // 它的作用是将缓冲区当前的limit设置为position,position设置为0
        // flip方法将Buffer从写模式切换到读模式。调用flip()方法会将position设回0,并将limit设置成之前position的值。
        buff.flip();
        System.out.println("flip()...");
        System.out.println("position:" + buff.position() + "\t limit:"
                + buff.limit() + "\t capacity:" + buff.capacity());

        byte[] tbyte = new byte[buff.limit()-3];
        buff.get(tbyte);
        System.out.println("get limit byte to string:" + new String(tbyte));
        System.out.println("position:" + buff.position() + "\t limit:"
                + buff.limit());
        if (buff.hasRemaining()) {
            System.out.println("hasRemaining");
            buff.compact();//压缩之后,因为本来还有3个数据没有取出,get取出数据之后的limit本来就是12,
            // 所以压缩之后position=3,关键是limit还是12(为什么limit还是12呢?如果再次flip之后的数据????
            //第二次实验时,此时limit怎么又是1024了,感觉电脑再次结果不正常,是我的眼睛的问题吗?我的理解:应该是1024才正确
            System.out.println("compact position="+buff.position()+" limit="+buff.limit());
            buff.flip();
            System.out.println("compact flip position="+buff.position()+" limit="+buff.limit()); //flip之后此时position=0,limit=3
            byte[] tbyte2 = new byte[buff.limit()];
            buff.get(tbyte2);
            System.out.println("get limit byte to string:" + new String(tbyte2));
            System.out.println("position:" + buff.position() + "\t limit:"+ buff.limit()); //都是3,当然无数据了
        } else {
            buff.clear();
            System.out.println("clear position="+buff.position()+" limit="+buff.limit());
        }

helloWorld   
position:10     limit:1024     capacity:1024
get one byte to string: 
获取两个字节(两次get()方法调用)后
position:12     limit:1024
mark()...
position:12     limit:1024
flip()...
position:0     limit:12     capacity:1024
get limit byte to string:helloWorl
position:9     limit:12
hasRemaining
compact position=3 limit=1024
compact flip position=0 limit=3
get limit byte to string:d  
position:3     limit:3

 

byte[] tbyte = new byte[buff.limit()-3];

这一句不要减3的话,则代码进入buff.clear()。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!