《Netty权威指南》之NIO ByteBuffer详解

 

Java NIO(New 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视频写的笔记,再此,感谢尚硅谷的免费视频   大爱尚硅谷

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值