NIO之ByteBuffer详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyzx_in_csdn/article/details/80344039
Java NIONew IO) 是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API
NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同, NIO支持面向缓冲区的、基于

通道的IO操作。 NIO将以更加高效的方式进行文件的读写操作。

Java NIO系统的核心在于:通道(Channel)和缓冲区(Buffer)。通道表示打开到 IO 设备(例如:文件、
套接字
)的连接。若需要使用 NIO 系统,需要获取用于连接 IO 设备的通道以及用于容纳数据的缓冲
区。然后操作缓冲区,对数据进行处理


简而言之, Channel 负责传输, Buffer 负责存储


package com.lyzx.concurrent.nio;

import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class BufferTest {

    /**
     * 字节缓冲区ByteBuffer的概念
     * 通过 allocate()/allocateDirect()方法获取一个缓冲区
     * 前者是在虚拟机实例中创建一个缓冲,后者是在实际的操作系统中创建一个缓冲
     *
     * 还可以通过wrap()方法把一个字节数组转换为字节缓冲区
     *
     * isDirect()表示是不是直接缓冲
     */
    @Test
    public void test1(){
        ByteBuffer buff = ByteBuffer.allocate(1024);
        ByteBuffer bufferDirect = ByteBuffer.allocateDirect(1024);
        System.out.println("buff:"+buff.isDirect());
        System.out.println("bufferDiret:"+bufferDirect.isDirect());

        ByteBuffer wrapBuffer1 = ByteBuffer.wrap("你好".getBytes());
        ByteBuffer wrapBuffer2 = ByteBuffer.wrap("你好中国".getBytes(),0,3);

        System.out.println(wrapBuffer1.isDirect() +"   "+wrapBuffer2.isDirect());
    }


    /**
     * 关于ByteBuffer的3个重要到的位置解释
     * 1、capacity  总容量大小
     * 2、position  当前的将要读写位置(可读/可写的位置)
     * 3、limit     限制位置(当前不可读/不可写的位置)
     *
     * @throws UnsupportedEncodingException
     */
    @Test
    public void test2() throws UnsupportedEncodingException {
        ByteBuffer buff = ByteBuffer.allocate(1024);
        System.out.println("================init================");
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        System.out.println("================init================");

        //这里有一个很有趣的现象希望大家注意一下,GBK和UTF-8编码方式中一个汉字使用的字节数不一样
        System.out.println("================put================");
        buff.put("你好".getBytes("UTF-8"));
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        System.out.println("================put================");


        System.out.println("================flip================");
        buff.flip();  //切换模式,默认是写模式
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        System.out.println("================flip================");

        System.out.println("================get================");
        byte b1 = buff.get();
        byte b2 = buff.get();
        byte b3 = buff.get();
        byte[] barray = {b1,b2,b3};
        System.out.println(">>>"+new String(barray));
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        System.out.println("================get================");


        System.out.println("================rewind================");
        //使position回到原位置,这意味着buffer里面的数据可以重读读取
        buff.rewind();
        byte b4 = buff.get();
        byte b5 = buff.get();
        byte b6 = buff.get();
        byte[] barray2 = {b4,b5,b6};
        System.out.println(new String(barray2));
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        System.out.println("================rewind================");

        System.out.println("================clear================");
        buff.clear();
        System.out.println("position:"+buff.position());
        System.out.println("limit:"+buff.limit());
        System.out.println("capacity:"+buff.capacity());
        byte b7 = buff.get();
        byte b8 = buff.get();
        byte b9 = buff.get();
        byte[] barray3 = {b7,b8,b9};
        System.out.println(new String(barray3));

        System.out.println("================clear================");
    }

    @Test
    public void test3() throws UnsupportedEncodingException{
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("你好大中国".getBytes("UTF-8"));

        System.out.println("==========put===============");
        System.out.println("position:"+buffer.position()); //15
        System.out.println("limit:"+buffer.limit());       //1024
        System.out.println("capacity:"+buffer.capacity()); //1024
        System.out.println("==========put===============");

        System.out.println("==========get===============");
        buffer.flip();
        byte[] barray = new byte[3];
        buffer.get(barray);
        System.out.println(new String(barray));
        System.out.println("position:"+buffer.position());  //3
        System.out.println("limit:"+buffer.limit());        //15
        System.out.println("capacity:"+buffer.capacity());  //1024
        System.out.println("==========get===============");

        System.out.println("==========mark=============");
        buffer.mark();//在当前位置做一个标记,即position=3的位置
        buffer.get(barray);
        System.out.println(new String(barray));
        System.out.println("position:"+buffer.position());  //6
        System.out.println("limit:"+buffer.limit());        //15
        System.out.println("capacity:"+buffer.capacity());  //1024
        System.out.println("==========mark=============");

        System.out.println("==========mark=============");
        buffer.reset();
        System.out.println("position:"+buffer.position());  //3
        System.out.println("limit:"+buffer.limit());        //15
        System.out.println("capacity:"+buffer.capacity());  //1024
        System.out.println("==========mark=============");


        //查看此字节数组中是否还有数据  实现如下=>  return position < limit
        while(buffer.hasRemaining()){
            buffer.get(barray);
            System.out.println(">>"+new String(barray));
            System.out.println(buffer.remaining());
        }
    }
}

注:此博客是观看完 尚硅谷 的NIO视频写的笔记,再此,感谢尚硅谷的免费视频   大爱尚硅谷

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页