【NIO】ByteBuffer

ByteBuffer的使用

  1. 想ByteBuffer中写入数据,每次bytebuffer大小
  2. bytebuffer开启读模式(默认是写模式)
  3. 按照get(每次1字节)获取数据,知道bytebuffer中数据读取完毕
  4. 开启写模式clear/comact
  5. 再次读取文件信息(实际上每次读文件信息,文件也会有指针进行移动,下次再来读,按照移动后的指针位置开始进行读取)
java 复制代码
{
        try (FileChannel channel = new FileInputStream("aaa.txt").getChannel()) {
            // 缓冲区设置大小为10字节
            ByteBuffer byteBuffer =ByteBuffer.allocate(10);

            while (true) {
                int read = channel.read(byteBuffer);
                System.out.println("read:"+read);

                if (read <=0) {
                    break;
                }
                // 开启读模式
                byteBuffer.flip();
                // 当缓冲区还有剩余未读数据
                while (byteBuffer.hasRemaining()) {
                    // 获取1字节
                    System.out.println((char)byteBuffer.get());
                }

                // 切换成写模式
                byteBuffer.clear();

            }

        } catch (IOException e) {
        }
    }

NIO读写两种模式下,指针位置的变化,clear(写),filp(读),compact(写)

初始状态:ByteBuffer 初始化(容量 10)

  1. position
  2. capacity
  3. limit

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│   │   │   │   │   │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
 ↑                   ↑
Position=0         Limit=10(=Capacity)
  • 刚创建 ByteBuffer,默认是写模式
  • Position=0(下一个写入位置),Limit=10(最大可写入位置)。

步骤 1:写入 4 字节数据(比如 "1234")

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1  │2  │3  │4  │   │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
         ↑           ↑
     Position=4    Limit=10
  • 写入 4 字节后,Position 从 0 移到 4(标记下一个写入位置);
  • Limit 仍 = 10(写模式下 Limit=Capacity)。

步骤 2:切换读模式(flip ())

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1  │2  │3  │4  │   │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
 ↑           ↑
Position=0  Limit=4
  • flip () 会把 Limit 设为当前 Position(4),Position 重置为 0;
  • 现在是读模式:可读取的范围是 Position=0 到 Limit=4。

步骤 3:读取 2 字节数据(比如 "12")

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│1  │2  │3  │4  │   │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
     ↑       ↑
Position=2  Limit=4
  • 读取 2 字节后,Position 从 0 移到 2;
  • Limit 仍 = 4(读模式下 Limit = 已写入的总长度);
  • 此时还剩 2 字节未读("34")

步骤 4:切换写模式(compact ())

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│3  │4  │   │   │   │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
     ↑                   ↑
Position=2             Limit=10
  • compact () 会把未读的 2 字节("34")移动到缓冲区开头
  • 然后 Position 设为 "未读字节的长度"(2),Limit 重置为 Capacity(10);
  • 现在是写模式:新数据会从 Position=2 开始写入(不会覆盖未读的 "34")。

步骤 5:再写入 3 字节数据(比如 "567")

plaintext

复制代码
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│3  │4  │5  │6  │7  │   │   │   │   │   │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
         ↑               ↑
     Position=5         Limit=10
  • 从 Position=2 开始写入 3 字节,Position 移到 5;
  • 此时缓冲区数据是 "34567",后续可继续写入到 Position=10。
相关推荐
做前端的娜娜子3 分钟前
同一链接实现 PC Web 与移动 H5 自适应
前端·掘金·金石计划
小帅不太帅11 分钟前
架构没变、规模没变,DeepSeek V4 Flash 正式版凭什么暴涨 47 分?
前端·aigc·deepseek
jarvisuni36 分钟前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法
卷福同学2 小时前
AI编程出海第二步:验证关键词能否做站
前端·人工智能·后端
赵庆明老师2 小时前
Vben精讲:21-详解web-antd:tsconfig.json
前端·json·vim
wc882 小时前
微软EDGE浏览器功能学习
前端·学习·edge
Csvn3 小时前
🎯 原生 `<dialog>` 元素:终于可以扔掉一半的自定义弹窗组件了?
前端
Csvn3 小时前
🎨 CSS @layer:用「层叠优先级」终结样式打架的世纪难题
前端
濮水大叔4 小时前
不必把 Vue3 写成“麻花”:从状态碎片到对象协作,重新理解 Zova 的前端心智模型
前端·typescript·vue3·ioc·tsx·zova
Vuji5 小时前
MCP: 一条 tool 调用链路的旅程
前端·人工智能