java.nio.ByteBuffer方法练习

3.2 详解NIO Buffer类及其属性

在这里插入图片描述

[Q&A] capacity属性一旦初始化,就不能再改变。原因是什么呢?

ByteBuffer子类就拥有一个byte[]类型的数组成员final byte[] hb,可以作为自己的读写缓冲区,在数组内存分配好之后,它的大小就不能改变了。

3.3 详解NIO Buffer类的重要方法

使用Java NIO Buffer类的基本步骤如下:

1・使用创建子类实例对象的 allocate() 方法创建一个Buffer类的实例对象。
2・调用 put() 方法将数据写入缓冲区中。
3・写入完成后,在开始读取数据前调用 Buffer.flip() 方法,将缓冲区转换为读模式。
4・调用 get() 方法,可以从缓冲区中读取数据。
5・读取完成后,调用 Buffer.clear() 方法或 Buffer.compact() 方法,将缓冲区转换为写模式,可以继续写入。

3.3.1 allocate()

# allocate() 获取Buffer子类的实例对象,并且分配内存空间。

ByteBuffer byteBuffer = ByteBuffer.allocate(8);

System.out.println(byteBuffer); 			// java.nio.HeapByteBuffer[pos=0 lim=8 cap=8]
System.out.println(byteBuffer.position());  // 0
System.out.println(byteBuffer.limit()); 	// 8
System.out.println(byteBuffer.capacity());  // 8

3.3.2 put()

# 把对象写入缓冲区,下标从0开始,每当一个数据写到缓冲区之后,position会向后移动到下一个可写的位置
IntBuffer intBuffer= IntBuffer.allocate(8);
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=8 cap=8]

byteBuffer.put(1);
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=1 lim=8 cap=8]

byteBuffer.put(2);
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=2 lim=8 cap=8]

3.3.3 flip():缓冲区切换成读模式

# 缓冲区切换成读模式
public Buffer flip() {
    limit = position;  设置可读上限(已写数据下标的右边界)
    position = 0;      读的起始位置
    mark = -1;         清除之前的mark标记
    return this;
}

IntBuffer intBuffer = IntBuffer.allocate(8);
System.out.println("写入数据之前 : " + intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=8 cap=8]

intBuffer.put(new int[]{3, 2, 1});
System.out.println("写入数据之后 : " + intBuffer); //  java.nio.HeapIntBuffer[pos=3 lim=8 cap=8]

intBuffer.flip();
System.out.println("重置游标之后 : " + intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=8]

3.3.4 get()

IntBuffer intBuffer = IntBuffer.allocate(8);
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=8 cap=8]

intBuffer.put(new int[] {3, 2, 1});

intBuffer.flip();
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=8]
for (int i = 0; i < 3; i++) {
    System.out.println(intBuffer.get() + " " + intBuffer);
}
// 3 java.nio.HeapIntBuffer[pos=1 lim=3 cap=8]
// 2 java.nio.HeapIntBuffer[pos=2 lim=3 cap=8]
// 1 java.nio.HeapIntBuffer[pos=3 lim=3 cap=8]

3.3.5 rewind():再读一遍

# 已经读完的数据,如果需要再读一遍
public Buffer rewind() {
    position = 0;   重置为0,可以重新读
    mark = -1;      清除之前的mark标记
    return this;
}

IntBuffer intBuffer = IntBuffer.allocate(8);
intBuffer.put(new int[]{3, 2, 1});
intBuffer.flip();
for (int i = 0; i < 3; i++) {
    System.out.println(intBuffer.get());
}
// 3
// 2
// 1

intBuffer.rewind();
for (int i = 0; i < 3; i++) {
    System.out.println(intBuffer.get());
}
// 3
// 2
// 1

3.3.6 mark()和reset():飞雷神

Buffer.mark()方法将当前position的值保存起来放在mark属性中,让mark属性记住这个临时位置;然后可以调用Buffer.reset()方法将mark的值恢复到position中。

3.3.7 clear():切换为写模式

将缓冲区切换为写模式

public Buffer clear() {
    position = 0;       将position清零
    limit = capacity;   limit设置为capacity最大容量值,可以一直写入,直到缓冲区写满
    mark = -1;          清除之前的mark标记
    return this;
}
IntBuffer intBuffer = IntBuffer.allocate(8);
intBuffer.put(new int[]{3, 2, 1});

intBuffer.flip();
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=3 cap=8]

intBuffer.clear();
System.out.println(intBuffer); // java.nio.HeapIntBuffer[pos=0 lim=8 cap=8]

-----------------------------------------------------------------------------摘自《Java高并发核心编程. 卷1, NIO、Netty、Redis、ZooKeeper》(尼恩 编著)

hasRemaining()

判断缓冲区中是否还有剩余的元素可以读取或者写入。

// 方法定义
public final boolean hasRemaining() {
    return position < limit;
}

// 打印缓冲区内容
while (buffer.hasRemaining()) {
    System.out.print(buffer.get() + " ");
}

remaining()

返回缓冲区中剩余可读或可写元素的数量

// 方法定义
public final int remaining() {
    int rem = limit - position;
    return rem > 0 ? rem : 0;
}

IntBuffer intBuffer = IntBuffer.allocate(8);
intBuffer.put(new int[]{3, 2, 1});
intBuffer.flip();

System.out.println(intBuffer.remaining()); // 3
System.out.println(intBuffer);             // java.nio.HeapIntBuffer[pos=0 lim=3 cap=8]

intBuffer.get();

System.out.println(intBuffer.remaining()); // 2
System.out.println(intBuffer);             // java.nio.HeapIntBuffer[pos=1 lim=3 cap=8]

compact():重新整理数据

1・将缓冲区中所有未读的数据(position~limit)移到缓冲区的起始位置,并保持其顺序。这实际上重叠并覆盖了已读过的数据部分。
2・如果先将positon设置到limit,再compact,那么相当于clear()
参考:https://blog.csdn.net/u012345283/article/details/38357851

参考

ByteBuffer的allocate()方法详解
JAVA NIO缓冲区(Buffer)------ByteBuffer常用方法详解
NIO学习系列:缓冲区内部实现机制
java.nio.ByteBuffer用法小结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值