Java網絡編程 -- Netty中的ByteBuf

因爲JDK中提供的ByteBuffer沒法動態擴容,而且API使用複雜等緣由,Netty中提供了ByteBuf。
Bytebuf的API操做更加便捷,能夠動態擴容,提供了多種ByteBuf的實現,以及高效的零拷貝機制。vue

ByteBuf的操做

ByteBuf有三個重要的屬性:capacity容量,readerIndex讀取位置,writerIndex寫入位置
提供了readerIndex和weiterIndex兩個變量指針來支持順序讀和寫操做java

下圖顯示了一個緩衝區是如何被兩個指針分割成三個區域的:數組

img

代碼示例:緩存

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)markdown

writeXXX方法調用時,經過AbstractByteBuf.ensureWritable0()方法進行檢查
容量計算方法:AbstractByteBufAllocator.calculateNewCapacityapp

根據capacity的最小值要求,對應有兩套計算方法:
沒超過4兆:從64字節開始,每次遞增一倍,直至計算出來的newCapacity知足新容量最小要求
示例:當前大小256,已寫250,繼續寫10字節的數據,須要的最小容量要求是261,則新容量爲64x2x2x2=512ui

超過4兆:新容量=新容量最小要求/4兆x4兆+4兆
示例:當前大小爲3兆,已寫3兆,繼續寫2兆,須要的最小容量大小爲5兆,則新容量是8兆(不能超過最大值)url

4兆的來源:一個固定的閾值AbstractByteBufAllocator.CALCULATE_THRESHOLDspa

ByteBuf的實現

img

在使用中都是經過ByteBufAllocator分配器進行申請,同時具有有內存管理功能操作系統

PooledByteBuf對象,內存 複用

PooledThreadCache:PooledByteBufAllocator實例維護的一個線程變量
多種分類的MemoryRegionCache數組用做內存緩存,MemoryRegionCache內部是鏈表,隊列裏面存Chuck。PoolChuck裏面維護了內存引用,內存複用的作法就是把buf的memory指向chuck的memory
PooledByteBufAllocator.ioBuffer運做過程梳理:

img

零拷貝機制

Netty的零拷貝機制,是一種應用層的實現,和底層JVM,操做系統內存機制並沒有過多關聯。

  1. CompositeByteBuf,將多個ByteBuf合併爲一個邏輯上的ByteBuf,避免了各個ByteBuf之間的拷貝

img

  1. wrapedBuffer()方法,將byte[]數組包裝成ByteBuf對象

img

  1. slice()方法,將一個ByteBuf對象切割成多個ByteBuf對象

img

代碼示例:

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);
  }
}

相關文章
相關標籤/搜索