java NIO初识

import java.nio.ByteBuffer;

/**
 * Created by langz on 2018/01/04 0004.
 * 一、Nio缓冲区,负责数据的存储,底层就是数组,用于存储不同数据类型的数据
 * 根据数据类型不同,提供了相应类型的缓冲区,除了boolean除外:
 * ByteBuffer(最常用)、CharBuffer等基本数据类型的缓冲区
 * 它们的管理方式基本一致,通过allocate()获取缓冲区
 * <p>
 * 二、缓冲区存取数据的两个核心方法:
 * put():
 * get():
 * <p>
 * 三、缓冲区中4个核心属性
 * capacity:容量,表示缓冲区中最大存储数据的容量,一旦声明不能改变
 * limit:限制界限,表示缓冲区中可以操作数据的大小,limit后面的数据不能进行读写
 * position:位置,表示缓冲区中正在操作数据的位置,要满足position <= limit <= capacity
 * <p>
 * mark:标记,表示记录当前position的位置。可以通过reset()回复到mark位置
 * 0 <= mark <= position <= limit <= capacity
 */
public class TestBuffer {

    @org.junit.Test
    public void testByteBuffer() {
        //1、创建一个指定大小的缓冲区
        System.out.println("-----------------allocate---------------");
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //2、存入数据到缓冲区
        System.out.println("-----------------put---------------");
        String s = "abcd";
        byteBuffer.put(s.getBytes());

        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //3.切换到读取数据模式
        System.out.println("-----------------flip---------------");
        byteBuffer.flip();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //4、读取缓存区中的数据
        System.out.println("-----------------get---------------");
        byte[] data = new byte[byteBuffer.limit()];
        byteBuffer.get(data);
        System.out.println(new String(data));
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //5.
        System.out.println("-----------------rewind---------------");
        byteBuffer.rewind(); //rewind后可重复读数据
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());

        //6、清空缓存区
        System.out.println("-----------------clear---------------");
        //clear后,但是缓存区的数据依然存在,单数数据处于被遗忘状态(再读的时候不能正确读取),可以重新写数据,回到最初状态
        byteBuffer.clear();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
    }

    @org.junit.Test
    public void testMark() {
        String s = "abcde";
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(s.getBytes());
        byteBuffer.flip();
        byte[] data = new byte[byteBuffer.limit()];
        byteBuffer.get(data, 0, 2);
        System.out.println(new String(data, 0, 2));
        System.out.println("position = " + byteBuffer.position());

        //mark() : 标记
        byteBuffer.mark();
        byteBuffer.get(data, 2, 2);
        System.out.println(new String(data, 2, 2));
        System.out.println("position = " + byteBuffer.position());

        //reset() 回复到mark的位置
        byteBuffer.reset();
        System.out.println("position = " + byteBuffer.position());
        //判断缓冲区中是否还有剩余数据
        if (byteBuffer.hasRemaining()) {
            //缓冲区中还可以操作的数据量
            int left = byteBuffer.remaining();
            System.out.println("left = " + left);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值