当前位置:   article > 正文

Java网络编程【2】缓冲区Buffer的数据读取_读取java缓存数据

读取java缓存数据

1.什么是缓冲区

在Java NIO中负责数据的存取,缓冲区的底层其实就是数组。用于存储不同数据类型的数组。除了八大基本数据类型中的boolean类型,其他的类型都有其对应的缓冲区。

  • ByteBuffer
  • CharBuffer
  • IntBuffer
  • ShortBuffer
  • LongBuffer
  • FloatBuffer
  • DoubleBuffer

2.缓存区属性

属性说明
容量(Capacity)缓冲区能够容纳的数据元素的最大数量,缓冲区创建时被设定,永远不能被改变
上界(Limit)下一个要被读或写的元素的索引,位置会自动由相应的get()和put()方法更新
位置(Position)一个备忘位置,调用mark()方法来设定,mark=position,调用reset方法设定position=mark。标记在设定前是未定义的(undefined)
mark <= position <= limit <= capacity
  • 1

3.缓存区读写操作

String bufferStr = "buffer";

        System.out.println("------------分配缓冲区allocate()-------------");
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());

        /*  ------------分配缓冲区allocate()-------------
        0
        1024
        1024*/

        System.out.println("------------缓冲区存数据put()-------------");
        buffer.put(bufferStr.getBytes());
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());

        /*------------缓冲区存数据put()-------------
        6
        1024
        1024*/

        System.out.println("------------缓冲区切换到读数据模式flip()-------------");
        buffer.flip();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        
        /*     ------------缓冲区切换到读数据模式flip()-------------
        0
        6
        1024*/

        System.out.println("------------缓冲区读数据get()-------------");
        byte[] bytes = new byte[buffer.limit()];
        buffer.get(bytes);
        System.out.println("缓存区存入的数据是>>>"+new String(bytes));
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        
        /*  ------------缓冲区读数据get()-------------
         缓存区存入的数据是>>>buffer
        6
        6
        1024*/

        System.out.println("------------重复读rewind()-------------");
        buffer.rewind();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        
        /*  ------------重复读rewind()-------------
        0
        6
        1024*/
        

        System.out.println("------------清除clear():处于被遗忘状态,起始数据还在-------------");
        buffer.clear();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        
        /*------------清除clear()-------------
        0
        1024
        1024*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

具体实现不做赘述。

4.mark()与reset()

mark()用作position定位,与reset()共同使用,可以使在调用reset()方法时让postion返回到mark()方法调用时的postion。

String str = "buffer";
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(str.getBytes());
        buffer.flip();
        byte[] bytes = new byte[buffer.limit()];
        buffer.get(bytes,0,2);
        System.out.println(new String(bytes,0,2));
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        
        /*2
        6
        1024*/
        System.out.println("------------mark()--------------");

        buffer.mark();
        buffer.get(bytes,2,2);
        System.out.println(new String(bytes,2,2));
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        /*------------mark()--------------
        ff
        4
        6
        1024*/
        System.out.println("-------------reset()-------------");
        buffer.reset();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        /*-------------reset()-------------
        2
        6
        1024*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/1005585
推荐阅读
相关标签
  

闽ICP备14008679号