Netty 4 Bytebuffer WritableBytes 说明
Netty 4 Bytebuffer WritableBytes clarification
我正在尝试了解 ByteBuffer。下面是程序。
ByteBuf heapBuff = Unpooled.buffer(18);
System.out.println("writableBytes " + heapBuff.writableBytes());
heapBuff.writeCharSequence("RAJKUMAR NATARAJAN TESTE", Charset.defaultCharset());
System.out.println("readableBytes " + heapBuff.readableBytes());
System.out.println("writableBytes " + heapBuff.writableBytes());
以上程序的输出是
writableBytes 18
readableBytes 24
writableBytes 104
writableBytes的计算逻辑是什么
缓冲区写入将检查它们是否有足够的可写字节来容纳数据,如果没有,将在后台增加缓冲区的大小。这就是检查是否有足够的可写字节时调用的内容,也是容量增加的原因。如果将 24 加到 104 = 128,即 2 的幂。
AbstractByteBuf.class
final void ensureWritable0(int minWritableBytes) {
ensureAccessible();
if (minWritableBytes <= writableBytes()) {
return;
}
if (minWritableBytes > maxCapacity - writerIndex) {
throw new IndexOutOfBoundsException(String.format(
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
writerIndex, minWritableBytes, maxCapacity, this));
}
// Normalize the current capacity to the power of 2.
int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
// Adjust to the new capacity.
capacity(newCapacity);
}
资源:
我正在尝试了解 ByteBuffer。下面是程序。
ByteBuf heapBuff = Unpooled.buffer(18);
System.out.println("writableBytes " + heapBuff.writableBytes());
heapBuff.writeCharSequence("RAJKUMAR NATARAJAN TESTE", Charset.defaultCharset());
System.out.println("readableBytes " + heapBuff.readableBytes());
System.out.println("writableBytes " + heapBuff.writableBytes());
以上程序的输出是
writableBytes 18
readableBytes 24
writableBytes 104
writableBytes的计算逻辑是什么
缓冲区写入将检查它们是否有足够的可写字节来容纳数据,如果没有,将在后台增加缓冲区的大小。这就是检查是否有足够的可写字节时调用的内容,也是容量增加的原因。如果将 24 加到 104 = 128,即 2 的幂。
AbstractByteBuf.class
final void ensureWritable0(int minWritableBytes) {
ensureAccessible();
if (minWritableBytes <= writableBytes()) {
return;
}
if (minWritableBytes > maxCapacity - writerIndex) {
throw new IndexOutOfBoundsException(String.format(
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
writerIndex, minWritableBytes, maxCapacity, this));
}
// Normalize the current capacity to the power of 2.
int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
// Adjust to the new capacity.
capacity(newCapacity);
}
资源: