Java NIO 学习笔记 缓冲区补充

1.缓冲区分配 方法   以 ByteBuffer 为例

(1)使用静态方法

ByteBuffer buffer = ByteBuffer.allocate( 500 );

  allocate() 方法分配一个具有指定大小的底层数组,并将它包装到一个缓冲区对象中

(2)使用wrap方法将数组转成缓冲区

byte bts[] = new byte[1024];
ByteBuffer buffer = ByteBuffer.wrap( bts );

  使用了 wrap() 方法将一个数组包装为缓冲区。一旦完成包装,底层数据就可以通过缓冲区或者直接访问。

2.缓冲区分片

  slice()发放可以根据现有的缓冲区创建一种子缓冲区,新缓冲区与原缓冲区的一部分数据共享

  代码举例:

//创建一个缓冲区并给缓冲区添加数据
        ByteBuffer btBuffer = ByteBuffer.allocate(15);
        for (int i = 0; i < btBuffer.capacity(); i++) {
            btBuffer.put((byte)i);
        }
        //指定子缓冲区的位置和限制  使用slice()方法  将缓冲区分区
        btBuffer.position(5);
        btBuffer.limit(11);
        ByteBuffer subBuffer = btBuffer.slice();
        
        //修改子缓冲区的数据
        for (int i = 0; i < subBuffer.capacity(); i++) {
            byte bt = subBuffer.get(i);
            bt = (byte) (bt*10);
            subBuffer.put(i, bt);
        }
        //将缓冲区的posion和limit设置成原来的大小
        btBuffer.position(0);
        btBuffer.limit(btBuffer.capacity());
        //查看缓冲区数据
        while (btBuffer.remaining()>0) {
            System.out.println(btBuffer.get());
        }
    }

输出结果

 

3.只读缓冲区

  调用asReadOnlyBuffer()的方法将任何一个常规缓冲区转换为只读缓冲区(单向的),此方法返回的缓冲区与原来的缓冲区数据共享且完全相同,但是这个缓冲区是只读的。

  制度缓冲区对于数据保护很有用,例如将缓冲区传递给某个对象的方法时,您无法知道这个方法是否会修改缓冲区中的数据。创建一个只读的缓冲区可以 保证 该缓冲区不会被修改。

  代码展示:

//创建一个缓冲区并给缓冲区添加数据
ByteBuffer btBuffer = ByteBuffer.allocate(15);
        
ByteBuffer copBuffer = btBuffer.asReadOnlyBuffer();
        
copBuffer.put(2, (byte) 10);

  运行上述代码会报错,证明了只读的缓冲区不能被修改

4.直接缓冲区和间接缓冲区

  另一种有用的 ByteBuffer 是直接缓冲区。 直接缓冲区 是为加快 I/O 速度,而以一种特殊的方式分配其内存的缓冲区。

  实际上,直接缓冲区的准确定义是与实现相关的。Sun 的文档是这样描述直接缓冲区的:

    给定一个直接字节缓冲区,Java 虚拟机将尽最大努力直接对它执行本机 I/O 操作。也就是说,它会在每一次调用底层操作系统的本机 I/O 操作之前(或之后),尝试避免将缓冲区的内容拷贝到一个中间缓冲区中(或者从一个中间缓冲区中拷贝数据)。

  代码展示:

FileInputStream fis = new FileInputStream( infile );
    FileOutputStream fos = new FileOutputStream( outfile );

    FileChannel fcin = fis.getChannel();
    FileChannel fcout = fos.getChannel();
ByteBuffer buffer
= ByteBuffer.allocateDirect( 800 ); while (true) { buffer.clear(); int r = fcin.read( buffer ); if (r==-1) { break; } buffer.flip(); fcout.write( buffer ); }
原文地址:https://www.cnblogs.com/kuoAT/p/7065536.html