Netty:ByteBuf的容量(capacity)

介绍

ByteBuf的capacity()返回该ByteBuf可容纳的字节数。
maxCapacity()返回该ByteBuf可允许的最大容量。
capacity(int newCapacity)对当前容量进行扩展或者缩减。如果新的容量newCapacity小于当前的容量,那么ByteBuf的内容会被截取到newCapacity的长度,截取的时候是将后边的内容去掉。如果新的容量newCapacity大于当前的容量,那么ByteBuf的内容会被扩充到newCapacity长度,从后边进行扩充。

代码示例

创建ByteBuf 时指定了初始容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(2);
		System.out.println(buf.capacity());
		System.out.println(buf.maxCapacity());
	}

}

运行输出:
在这里插入图片描述

创建ByteBuf 时指定了初始容量和最大容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(2, 4);
		System.out.println(buf.capacity());
		System.out.println(buf.maxCapacity());
	}

}

运行输出:
在这里插入图片描述

创建ByteBuf 时没有指定初始容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer();
		System.out.println(buf.capacity());
		System.out.println(buf.maxCapacity());
	}

}

运行输出:
在这里插入图片描述

通过ByteBuf的capacity(int newCapacity) 扩充容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(2);
		buf.capacity(4);
		System.out.println(buf.capacity());
		System.out.println(buf.maxCapacity());
	}

}

运行输出:
在这里插入图片描述

通过ByteBuf的capacity(int newCapacity) 缩减容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(4);
		buf.capacity(2);
		System.out.println(buf.capacity());
		System.out.println(buf.maxCapacity());
	}

}

运行输出:
在这里插入图片描述

ByteBuf容量缩减是将后边的内容去掉

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(4);
		buf.writeByte(0x68);

		// 将buf的容量缩减
		buf.capacity(3);
		// 看看容量缩减以后buf各字节的内容是什么
		for (int i = 0; i < buf.capacity(); i++) {
			System.out.println(buf.getByte(i));
		}
	}

}

运行输出:
在这里插入图片描述

对比ByteBuf容量缩减前后内容的变化

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(3);		
		buf.setByte(2, 0x68);
		System.out.println("容量缩减前各字节的内容:");		
		for (int i = 0; i < buf.capacity(); i++) {
			System.out.println(buf.getByte(i));
		}
		
		// 将buf的容量缩减
		buf.capacity(2);
		System.out.println("容量缩减以后各字节的内容:");
		for (int i = 0; i < buf.capacity(); i++) {
			System.out.println(buf.getByte(i));
		}
	}

}

运行输出:
在这里插入图片描述

根据ByteBuf当前可读字节数缩减容量

package com.thb;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Test {

	public static void main(String[] args) {		
		ByteBuf buf = Unpooled.buffer(5);		
		buf.writeByte(0x68);
		// 查看可读字节数
		System.out.println("当前可读字节数:" + buf.readableBytes());
		// 缩减容量
		buf.capacity(buf.readableBytes());
		
		System.out.println("容量缩减后各字节的内容:");		
		for (int i = 0; i < buf.capacity(); i++) {
			System.out.println(buf.getByte(i));
		}
	}

}

运行输出:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值