介绍
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));
}
}
}
运行输出: