由於JDK中提供的ByteBuffer無法動態擴容,並且API使用復雜等原因,Netty中提供了ByteBuf。
Bytebuf的API操作更加便捷,可以動態擴容,提供了多種ByteBuf的實現,以及高效的零拷貝機制。
ByteBuf的操作
ByteBuf有三個重要的屬性:capacity容量,readerIndex讀取位置,writerIndex寫入位置
提供了readerIndex和weiterIndex兩個變量指針來支持順序讀和寫操作
下圖顯示了一個緩沖區是如何被兩個指針分割成三個區域的:
代碼示例:
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.Arrays;
public class ByteBufDemo {
public static void main(String[] args) {
// 1.創建一個非池化的ByteBuf,大小為10個字節
ByteBuf buf = Unpooled.buffer(10);
System.out.println("原始ByteBuf為:" + buf.toString());
System.out.println("1.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 2.寫入一段內容
byte[] bytes = {1, 2, 3, 4, 5};
buf.writeBytes(bytes);
System.out.println("寫入的bytes為:" + Arrays.toString(bytes));
System.out.println("寫入一段內容后ByteBuf為:" + buf.toString());
System.out.println("2.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 3. 讀取一段內容
byte b1 = buf.readByte();
byte b2 = buf.readByte();
System.out.println("讀取的bytes為:" + Arrays.toString(new byte[] {b1, b2}));
System.out.println("讀取一段內容后ByteBuf為:" + buf.toString());
System.out.println("3.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 4.將讀取的內容丟棄
buf.discardReadBytes();
System.out.println("將讀取的內容丟棄后ByteBuf為:" + buf.toString());
System.out.println("4.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 5.清空讀寫指針
buf.clear();
System.out.println("清空讀寫指針后ByteBuf為:" + buf.toString());
System.out.println("5.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 6.再次寫入一段內容,比第一段內容少
byte[] bytes2 = {1, 2, 3};
buf.writeBytes(bytes2);
System.out.println("寫入的bytes為:" + Arrays.toString(bytes2));
System.out.println("寫入一段內容后ByteBuf為:" + buf.toString());
System.out.println("6.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 7.將ByteBuf清零
buf.setZero(0, buf.capacity());
System.out.println("清零后ByteBuf為:" + buf.toString());
System.out.println("7.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
// 8.再次寫入一段超過容量的內容
byte[] bytes3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
buf.writeBytes(bytes3);
System.out.println("寫入的bytes為:" + Arrays.toString(bytes));
System.out.println("寫入一段內容后ByteBuf為:" + buf.toString());
System.out.println("8.ByteBuf中的內容為:" + Arrays.toString(buf.array()) + "\n");
}
}
ButeBuf動態擴容
capacity默認值:256字節,最大值:Integer.MAX_VALUE (2G)
writeXXX方法調用時,通過AbstractByteBuf.ensureWritable0()方法進行檢查
容量計算方法:AbstractByteBufAllocator.calculateNewCapacity
根據capacity的最小值要求,對應有兩套計算方法:
沒超過4兆:從64字節開始,每次遞增一倍,直至計算出來的newCapacity滿足新容量最小要求
示例:當前大小256,已寫250,繼續寫10字節的數據,需要的最小容量要求是261,則新容量為64x2x2x2=512
超過4兆:新容量=新容量最小要求/4兆x4兆+4兆
示例:當前大小為3兆,已寫3兆,繼續寫2兆,需要的最小容量大小為5兆,則新容量是8兆(不能超過最大值)
4兆的來源:一個固定的閾值AbstractByteBufAllocator.CALCULATE_THRESHOLD
ByteBuf的實現
在使用中都是通過ByteBufAllocator分配器進行申請,同時具備有內存管理功能
PooledByteBuf對象,內存 復用
PooledThreadCache:PooledByteBufAllocator實例維護的一個線程變量
多種分類的MemoryRegionCache數組用作內存緩存,MemoryRegionCache內部是鏈表,隊列里面存Chuck。PoolChuck里面維護了內存引用,內存復用的做法就是把buf的memory指向chuck的memory
PooledByteBufAllocator.ioBuffer運作過程梳理:
零拷貝機制
Netty的零拷貝機制,是一種應用層的實現,和底層JVM,操作系統內存機制並無過多關聯。
- CompositeByteBuf,將多個ByteBuf合並為一個邏輯上的ByteBuf,避免了各個ByteBuf之間的拷貝
- wrapedBuffer()方法,將byte[]數組包裝成ByteBuf對象
- slice()方法,將一個ByteBuf對象切割成多個ByteBuf對象
代碼示例:
public class ZeroCopyTest {
public static void main(String[] args) {
ByteBuf buffer1 = Unpooled.buffer(7);
buffer1.writeByte(7);
ByteBuf buffer2 = Unpooled.buffer(7);
buffer2.writeByte(13);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
CompositeByteBuf newBuf = compositeByteBuf.addComponents(true, buffer1, buffer2);
System.out.println("CompositeByteBuf:" + newBuf);
byte[] bytes = {1, 2, 3};
ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes);
System.out.println("wrappedBuffer:" + wrappedBuffer.getByte(2));
bytes[2] = 7;
System.out.println("wrappedBuffer:" + wrappedBuffer.getByte(2));
ByteBuf buf = Unpooled.wrappedBuffer("Netty".getBytes());
ByteBuf slice = buf.slice(1, 2);
slice.unwrap();
System.out.println("slice:" + slice);
}
}