说明
- 通过io.netty.buffer.ByteBuf的maxFastWritableBytes()函数可以得到最大快速可写入字节数。最大快速可写入字节数表示没有内部再分配内存的情况下肯定可以写入的最大字节数。
- maxFastWritableBytes() = capacity(容量) - writerIndex
- writableBytes() <= maxFastWritableBytes() <= maxWritableBytes()。
示例
package com.thb;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class Test {
public static void main(String[] args) {
// 创建一个ByteBuf,没有明确指明初始容量、最大容量
ByteBuf buf = Unpooled.buffer();
// 写入1个字节
buf.writeByte(0x68);
// 当前的writerIndex
System.out.println("buf.writerIndex: " + buf.writerIndex());
// 当前的容量
System.out.println("buf.capacity: " + buf.capacity());
// 最大容量
System.out.println("buf.maxCapacity: " + buf.maxCapacity());
// 可写入字节数
System.out.println("buf.writableBytes: " + buf.writableBytes());
// 最大可写入字节数
System.out.println("buf.maxWritableBytes: " + buf.maxWritableBytes());
// 最大快速可写入字节数
System.out.println("buf.maxFastWritableBytes: " + buf.maxFastWritableBytes());
System.out.println("------------------------");
// 创建一个ByteBuf,明确指明初始容量是3、最大容量是10
ByteBuf buf2 = Unpooled.buffer(3, 10);
// 写入1个字节
buf2.writeByte(0x68);
System.out.println("buf2.writerIndex: " + buf2.writerIndex());
System.out.println("buf2.capacity: " + buf2.capacity());
System.out.println("buf2.maxCapacity: " + buf2.maxCapacity());
System.out.println("buf2.writableBytes: " + buf2.writableBytes());
System.out.println("buf2.maxWritableBytes: " + buf2.maxWritableBytes());
System.out.println("buf2.maxFastWritableBytes: " + buf2.maxFastWritableBytes());
System.out.println("------------------------");
// 创建一个ByteBuf,明确指明初始容量是3、最大容量是10
ByteBuf buf3 = Unpooled.buffer(3, 10);
// 写入3个字节,刚好把初始容量用完了
for (int i = 0; i < 3; i++) {
buf3.writeByte(0x68);
}
System.out.println("buf3.writerIndex: " + buf3.writerIndex());
System.out.println("buf3.capacity: " + buf3.capacity());
System.out.println("buf3.maxCapacity: " + buf3.maxCapacity());
System.out.println("buf3.writableBytes: " + buf3.writableBytes());
System.out.println("buf3.maxWritableBytes: " + buf3.maxWritableBytes());
System.out.println("buf3.maxFastWritableBytes: " + buf3.maxFastWritableBytes());
System.out.println("------------------------");
// 创建一个ByteBuf,明确指明初始容量是3、最大容量是10
ByteBuf buf4 = Unpooled.buffer(3, 10);
// 写入4个字节。在写入第4个字节的时候,ByteBuf自动扩展了一次
for (int i = 0; i < 4; i++) {
buf4.writeByte(0x68);
}
System.out.println("buf4.writerIndex: " + buf4.writerIndex());
System.out.println("buf4.capacity: " + buf4.capacity());
System.out.println("buf4.maxCapacity: " + buf4.maxCapacity());
System.out.println("buf4.writableBytes: " + buf4.writableBytes());
System.out.println("buf4.maxWritableBytes: " + buf4.maxWritableBytes());
System.out.println("buf4.maxFastWritableBytes: " + buf4.maxFastWritableBytes());
}
}
运行输出:
buf.writerIndex: 1
buf.capacity: 256
buf.maxCapacity: 2147483647
buf.writableBytes: 255
buf.maxWritableBytes: 2147483646
buf.maxFastWritableBytes: 255
------------------------
buf2.writerIndex: 1
buf2.capacity: 3
buf2.maxCapacity: 10
buf2.writableBytes: 2
buf2.maxWritableBytes: 9
buf2.maxFastWritableBytes: 2
------------------------
buf3.writerIndex: 3
buf3.capacity: 3
buf3.maxCapacity: 10
buf3.writableBytes: 0
buf3.maxWritableBytes: 7
buf3.maxFastWritableBytes: 0
------------------------
buf4.writerIndex: 4
buf4.capacity: 10
buf4.maxCapacity: 10
buf4.writableBytes: 6
buf4.maxWritableBytes: 6
buf4.maxFastWritableBytes: 6