zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Netty】NIO 缓冲区 ( Buffer ) ( 缓冲区读写类型 | 只读缓冲区 | 映射字节缓冲区 )

字节nio映射 类型 读写 Buffer 只读 Netty
2023-06-13 09:17:42 时间

文章目录

I . 缓冲区 ( Buffer ) 存取类型


1 . 缓冲区 ( Buffer ) 数据读写类型 注意点 : 以 字节缓冲区 ( ByteBuffer ) 为例 ;

① 向 字节缓冲区 ( ByteBuffer ) 中放入数据 :

  • 放入 Int 类型数据 : ByteBuffer putInt(int value) ;
  • 放入 Double 类型数据 : ByteBuffer putDouble(double value) ;
  • 放入 Short 类型数据 : ByteBuffer putShort(short value) ;

② 从 字节缓冲区 ( ByteBuffer ) 中读取数据 :

  • 取出 Int 类型数据 : int getInt() ;
  • 取出 Double 类型数据 : double getDouble() ;
  • 取出 Short 类型数据 : short getShort() ;

③ 读取数据注意点 : 读取 字节缓冲区 ( ByteBuffer ) 数据时 , 必须按照放入 字节缓冲区 ( ByteBuffer ) 中的数据进行 , 否则就会读出错误数据 , 或乱码 ;

④ 读取溢出 : 读取 或 写出时 , position 一定不能超过 limit , 否则就会报 BufferUnderFlowException 异常 ;

Exception in thread "main" java.nio.BufferUnderflowException
	at java.nio.Buffer.nextGetIndex(Buffer.java:506)
	at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:361)
	at kim.hsl.nio.BufferDemo2.main(BufferDemo2.java:23)

代码示例 :

package kim.hsl.nio;

import java.nio.ByteBuffer;

public class BufferDemo2 {
    public static void main(String[] args) {
        //1 . 创建一个存储 Int 类型数据的 Buffer , 可以存储 1024 个字节
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //2 . 向缓冲区中放入数据
        buffer.putInt(8888);
        buffer.putDouble(88.888);
        buffer.putShort((short) 888);

        //3 . 写入转读取前先翻转, 将 position 设置为 0
        buffer.flip();

        //4 . 从缓冲区中读取数据
        int intValue = buffer.getInt();
        double doubleValue = buffer.getDouble();
        short shortValue = buffer.getShort();
        //已经读取完了, 在读取就溢出了 java.nio.BufferUnderflowException
        //buffer.getInt();

        //5 . 打印读取的数据信息
        System.out.println(String.format("intValue = %d, doubleValue = %f, shortValue = %d", 
                intValue, doubleValue, shortValue));
    }
}

执行结果 :

intValue = 8888, doubleValue = 88.888000, shortValue = 888

II . 只读缓冲区 ( ReadOnlyBuffer )


1 . 只读 缓冲区 ( ReadOnlyBuffer ) :

① 只读缓冲区 ( ReadOnlyBuffer ) 获取 : 先创建一个 Buffer 对象 , 向其中存储数据 , 调用 asReadOnlyBuffer() 方法 , 可以返回一个只读缓冲区 , 该缓冲区 , 只能读取 , 不能写入 ;

② 实际类型 : 只读缓冲区的类型是 HeapByteBufferR ;

③ 只读缓冲区写入数据异常 : 该 只读缓冲区 ( ReadOnlyBuffer ) 只能读取数据 , 不能向其中写入数据 ; 如果写入数据 , 就会报异常 ;

Exception in thread "main" java.nio.ReadOnlyBufferException
	at java.nio.HeapByteBufferR.putShort(HeapByteBufferR.java:324)
	at kim.hsl.nio.BufferDemo3.main(BufferDemo3.java:28)

2 . 示例代码 :

package kim.hsl.nio;

import java.nio.ByteBuffer;

public class BufferDemo3 {
    public static void main(String[] args) {
        //1 . 创建一个存储 Int 类型数据的 Buffer , 可以存储 1024 个字节
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //2 . 向缓冲区中放入数据
        buffer.putInt(8888);
        buffer.putDouble(88.888);
        buffer.putShort((short) 888);

        //3 . 写入转读取前先翻转, 将 position 设置为 0
        buffer.flip();

        //4 . 将上述缓冲区转为只读缓冲区
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();

        //5 . 从缓冲区中读取数据
        int intValue = readOnlyBuffer.getInt();
        double doubleValue = readOnlyBuffer.getDouble();
        short shortValue = readOnlyBuffer.getShort();
        //已经读取完了, 在读取就溢出了 java.nio.BufferUnderflowException
        //buffer.getInt();
        //向只读缓冲区中存放数据抛 java.nio.ReadOnlyBufferException 异常
        //readOnlyBuffer.putShort((short) 888);

        //5 . 打印读取的数据信息
        System.out.println(String.format("intValue = %d, doubleValue = %f, shortValue = %d",
                intValue, doubleValue, shortValue));
    }
}

执行结果 :

intValue = 8888, doubleValue = 88.888000, shortValue = 888

III . 映射字节缓冲区 ( MappedByteBuffer )


1 . 映射字节缓冲区 ( MappedByteBuffer ) : 在内存中修改文件 , 不需要将文件中的内容拷贝到内存中 , 再修改后 , 写回到文件 , 其性能提高了很多 ;

① 内存说明 : 修改文件的内存并不是堆内存 , 而是在堆外内存中 ;

② MappedByteBuffer 类结构 :

  • MappedByteBuffer 继承 ByteBuffer 抽象类 ;
  • MappedByteBuffer 本身也是抽象类 , 其有两个子类 , 分别是 DirectByteBuffer , DirectByteBufferR ;

③ 可操作区域 : fc.map(FileChannel.MapMode.READ_WRITE, 0, 10); 的 MappedByteBuffer 只能操作 从 0 索引开始的 10 个字节 , 即从 0 到 9 索引代表的字节 , 其中的 10 代表可操作性的字节个数 , 并不是索引值 ;

2 . 代码示例 :

package kim.hsl.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class MappedByteBufferDemo {
    public static void main(String[] args) {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile("file.txt", "rw");
            FileChannel fc = randomAccessFile.getChannel();

            //FileChannel.MapMode.READ_WRITE : 指的是读写模式
            //0 : 将文件从 0 位置开始映射到内存中
            //10 : 将文件从 0 位置开始映射到内存中的大小
            //即 将 file.txt 文件从 0 开始的 10 字节映射到内存中
            MappedByteBuffer mappedByteBuffer = fc.map(FileChannel.MapMode.READ_WRITE, 0, 10);

            mappedByteBuffer.put(0, (byte) 'N');
            mappedByteBuffer.put(1, (byte) 'N');
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(randomAccessFile != null)
                    randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

执行结果 : 在 IntelliJ IDEA 环境中打开没有刷新 , 在文件浏览器中打开 , “Hello World” 的前两位变成了 “NN” ;