ByteBuffer的使用及实现原理

ByteBuffer的使用及实现原理,第1张

1、介绍

Buffer是nio包的一个抽象类,作为java nio的三大组件(Buffer、Channel,Selector)之一,在java nio网络编程中尤为重要。

Buffer提供了一个字节缓冲区,配合Channel使用,可以从Channel中读取或写入数据。

2、结构 属性介绍

以ByteBuffer为例,其包括5个主要的属性:hb、position、limit、capacity、mark。

hb:ByteBuffer类有一个byte数组变量hb,此数组里面存放的就是实际的字节数据。

capacity:容量大小,其实就是hb字节数组的大小,始终不变。

position:当前读写 *** 作的ByteBuffer对象位置,其实就是hb字节数组下标位置。

limit:读写 *** 作position的大小限制,读写 *** 作时position需要小于limit。

mark:记录当前读写的位置,方便后续使用。

常用API方法
/**
 * 初始化指定大小的ByteBuffer对象返回。
 */
static ByteBuffer allocate(int capacity);
/**
 * 使用指定字节数组初始化ByteBuffer对象返回。
 */
static ByteBuffer wrap(byte[] array)
/**
 * 返回当前position位置的字节数据,position自增1。
 */    
byte get();
/**
 * 返回指定位置的数据。
 */  
byte get(int index);
/**
 * 当前position位置设置为传入字节,position自增1。
 */ 
ByteBuffer put(byte b);
/**
 * 切换到读模式。
 */ 
Buffer flip();
/**
 * 切换到写模式,不保留未读完数据。
 */ 
Buffer clear();
/**
 * 切换到写模式,保留未读完数据。
 */ 
ByteBuffer compact();
图解
  • ①初始化状态。执行**ByteBuffer.allocate(10);**后状态。

  • ②调用put方法插入数据,每往当前position位置插入一个数据,position执行加1 *** 作。插入4个数据后状态。

  • ③调用flip方法,依次设置limit=position、position=0、mark=-1。

  • ④调用get方法读取数据,返回当前position下标对应的值,然后positon执行加1 *** 作。读取三个数据后状态。

  • ⑤调用clear或compact方法,重置position、limit的值。

调用clear后状态(依次执行position = 0、limit = capacity、mark = -1)。

调用compact后状态。

3、读模式和写模式

其实ByteBuffer本身并没有读模式、写模式的概念,为了便于初学者理解网友们强加的概念。

flip、clear、compact等方法只是修改了position、limit、mark等属性的值而已,理解了上面的几个 *** 作图就不需要理解不存在的读模式、写模式,避免混淆理解。

4、使用演示
  • 演示1
/**
 * 代码
 */ 
public class ByteBufferDemo {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put((byte)'a');
        buffer.put((byte)'b');
        buffer.put((byte)'c');
        buffer.put((byte)'d');
        System.out.println(buffer);

        buffer.flip();
        System.out.println(buffer);

        System.out.println(buffer.get());
        System.out.println(buffer.get());
        System.out.println(buffer.get());

        buffer.clear();
        System.out.println(buffer);
        System.out.println(Arrays.toString(buffer.array()));

        System.out.println(buffer.get(2));
    }
}

/**
 * 运行结果
 */
java.nio.HeapByteBuffer[pos=4 lim=10 cap=10]
java.nio.HeapByteBuffer[pos=0 lim=4 cap=10]
97
98
99
java.nio.HeapByteBuffer[pos=0 lim=10 cap=10]
[97, 98, 99, 100, 0, 0, 0, 0, 0, 0]
99
  • 演示二
/**
 * 代码
 */ 
public class ByteBufferDemo {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put((byte)'a');
        buffer.put((byte)'b');
        buffer.put((byte)'c');
        buffer.put((byte)'d');
        System.out.println(buffer);

        buffer.flip();
        System.out.println(buffer);

        System.out.println(buffer.get());
        System.out.println(buffer.get());
        System.out.println(buffer.get());
        
        buffer.compact();
        System.out.println(buffer);
        System.out.println(Arrays.toString(buffer.array()));

        System.out.println(buffer.get(2));
    }
}

/**
 * 运行结果
 */
java.nio.HeapByteBuffer[pos=4 lim=10 cap=10]
java.nio.HeapByteBuffer[pos=0 lim=4 cap=10]
97
98
99
java.nio.HeapByteBuffer[pos=1 lim=10 cap=10]
[100, 98, 99, 100, 0, 0, 0, 0, 0, 0]
99
  • 演示三
/**
 * 代码
 */ 
public class ByteBufferDemo {
    public static void main(String[] args) {
        byte[] bytes = {(byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g'};
        ByteBuffer buffer = ByteBuffer.wrap(bytes);

        System.out.println(buffer);
        System.out.println(Arrays.toString(buffer.array()));

        //直接读取数据
        System.out.println(buffer.get());
        System.out.println(buffer);

        //写入数据
        buffer.put((byte)0x01);
        buffer.put((byte)0x02);
        System.out.println(buffer);
        System.out.println(Arrays.toString(buffer.array()));
    }
}

/**
 * 运行结果
 */
java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]
[97, 98, 99, 100, 101, 102, 103]
97
java.nio.HeapByteBuffer[pos=1 lim=7 cap=7]
java.nio.HeapByteBuffer[pos=3 lim=7 cap=7]
[97, 1, 2, 100, 101, 102, 103]

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/741038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存