Netty中Buffer的基本使用

Netty中Buffer的基本使用

buffer分配

	@Test
    public void testBufferAllocator(){
        /**
         * buffer,默认使用缓冲池,直接分配的方式,默认大小256
         */
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        System.out.println("ByteBufAllocator.DEFAULT.buffer()---" + buf);

        /**
         * directBuffer,默认使用缓冲池,默认大小256
         */
        ByteBuf directBuffer = ByteBufAllocator.DEFAULT.directBuffer();
        System.out.println("ByteBufAllocator.DEFAULT.directBuffer()---" + directBuffer);

        /**
         * 不使用缓冲池的方式
         */
        ByteBuf unPoolHeapBuf = UnpooledByteBufAllocator.DEFAULT.heapBuffer();
        System.out.println("UnpooledByteBufAllocator.DEFAULT.heapBuffer()---" + unPoolHeapBuf);

        /**
         * 使用缓冲池的方式
         */
        ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.heapBuffer();
        System.out.println("PooledByteBufAllocator.DEFAULT.heapBuffer()---" + byteBuf);

        /**
         * 使用缓冲池的方式,设置初始化大小
         */
        ByteBuf byteBufCap = PooledByteBufAllocator.DEFAULT.heapBuffer(8);
        System.out.println("PooledByteBufAllocator.DEFAULT.heapBuffer(8)---" + byteBufCap);

        /**
         * 使用缓冲池的方式,设置初始化大小和最大大小
         */
        ByteBuf byteBufMaxCap = PooledByteBufAllocator.DEFAULT.heapBuffer(8,128);
        System.out.println("PooledByteBufAllocator.DEFAULT.heapBuffer(8,128)---" + byteBufMaxCap);
    }

分配方式默认使用缓冲区堆内直接分配方式,默认初始大小为256个字节。

buffer读写

    @Test
    public void testBufferRW(){
        ByteBuf byteBufMaxCap = PooledByteBufAllocator.DEFAULT.heapBuffer(1,4);

        System.out.println("初始化前----------");
        print(byteBufMaxCap);

        byteBufMaxCap.writeBytes(new byte[]{1});
        System.out.println("写入一个字节:1");
        print(byteBufMaxCap);

        byteBufMaxCap.writeBytes(new byte[]{2});
        System.out.println("写入一个字节:2");
        print(byteBufMaxCap);

        byte b1 = byteBufMaxCap.readByte();
        System.out.println("读取一个字节:" + b1);
        print(byteBufMaxCap);

        byte b2 = byteBufMaxCap.readByte();
        System.out.println("读取一个字节:" + b2);
        print(byteBufMaxCap);
        //数据读过以后,下标并没有变化,只能再写入两个字节。

        byteBufMaxCap.writeBytes(new byte[]{3});
        System.out.println("写入一个字节:3");
        print(byteBufMaxCap);

        byteBufMaxCap.writeBytes(new byte[]{4});
        System.out.println("写入一个字节:4");
        print(byteBufMaxCap);

        //此时如果不清空,再写入的话就会报错。
        byteBufMaxCap.clear();
        System.out.println("调用clear方法之后---");
        print(byteBufMaxCap);

        byteBufMaxCap.writeBytes(new byte[]{5});
        System.out.println("写入一个字节:5");
        print(byteBufMaxCap);
    }


	//观察读写后属性变化。
    public void print(ByteBuf buf) {
        System.out.println("buf.isReadable():" + buf.isReadable());
        System.out.println("buf.readerIndex():" + buf.readerIndex());
        System.out.println("buf.readableBytes():" + buf.readableBytes());
        System.out.println("buf.isWritable():" + buf.isWritable());
        System.out.println("buf.writerIndex():" + buf.writerIndex());
        System.out.println("buf.writableBytes():" + buf.writableBytes());
        System.out.println("buf.capacity():" + buf.capacity());
        System.out.println("buf.maxCapacity():" + buf.maxCapacity());
        System.out.println("----------分隔符----------");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值