Home / Class/ PooledByteBuf Class — netty Architecture

PooledByteBuf Class — netty Architecture

Architecture documentation for the PooledByteBuf class in PooledByteBuf.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  49d00ede_f018_5e8f_42f9_08fdd974fe00["PooledByteBuf"]
  46fccbd1_da6c_61e7_16e3_fc98fb06546d["PooledByteBuf.java"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|defined in| 46fccbd1_da6c_61e7_16e3_fc98fb06546d
  d29dedd9_6aac_58d4_bd66_1cc3715b155c["PooledByteBuf()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| d29dedd9_6aac_58d4_bd66_1cc3715b155c
  b0cb1927_72e0_dbc7_08bf_a442b6deb2c2["init()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| b0cb1927_72e0_dbc7_08bf_a442b6deb2c2
  1f76ef4f_13e3_eba8_3ea1_e84fbc436c5a["initUnpooled()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 1f76ef4f_13e3_eba8_3ea1_e84fbc436c5a
  8f9a28df_5a10_351a_dcf4_0576a7ebc853["init0()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 8f9a28df_5a10_351a_dcf4_0576a7ebc853
  45b9bfbb_4348_547f_8ce7_b3d65b2ff972["reuse()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 45b9bfbb_4348_547f_8ce7_b3d65b2ff972
  e4b2bb2e_61b6_e965_f924_7b6426462c6f["capacity()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| e4b2bb2e_61b6_e965_f924_7b6426462c6f
  4617b06f_4fac_2581_7e7c_b20d59b8fbdb["maxFastWritableBytes()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 4617b06f_4fac_2581_7e7c_b20d59b8fbdb
  3d31b84b_29ad_88f5_6a79_fe73056f1dc5["ByteBuf()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 3d31b84b_29ad_88f5_6a79_fe73056f1dc5
  2d62449f_24eb_4bef_c247_3f4acedd8720["ByteBufAllocator()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 2d62449f_24eb_4bef_c247_3f4acedd8720
  76a572af_ae5c_bd49_fe58_90885a7304d9["ByteOrder()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 76a572af_ae5c_bd49_fe58_90885a7304d9
  7f6ae8d1_3700_4104_01e5_120c6b2183ce["ByteBuffer()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 7f6ae8d1_3700_4104_01e5_120c6b2183ce
  85bb3ea8_6fe7_8b0e_da8f_203ff3df3fac["deallocate()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| 85bb3ea8_6fe7_8b0e_da8f_203ff3df3fac
  f69585e0_78e6_49f6_f61c_921d795c9149["idx()"]
  49d00ede_f018_5e8f_42f9_08fdd974fe00 -->|method| f69585e0_78e6_49f6_f61c_921d795c9149

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/PooledByteBuf.java lines 30–273

abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {

    private final EnhancedHandle<PooledByteBuf<T>> recyclerHandle;

    protected PoolChunk<T> chunk;
    protected long handle;
    protected T memory;
    protected int offset;
    protected int length;
    int maxLength;
    PoolThreadCache cache;
    ByteBuffer tmpNioBuf;
    private ByteBufAllocator allocator;

    @SuppressWarnings("unchecked")
    protected PooledByteBuf(Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity) {
        super(maxCapacity);
        this.recyclerHandle = (EnhancedHandle<PooledByteBuf<T>>) recyclerHandle;
    }

    void init(PoolChunk<T> chunk, ByteBuffer nioBuffer,
              long handle, int offset, int length, int maxLength, PoolThreadCache cache, boolean threadLocal) {
        init0(chunk, nioBuffer, handle, offset, length, maxLength, cache, true, threadLocal);
    }

    void initUnpooled(PoolChunk<T> chunk, int length) {
        init0(chunk, null, 0, 0, length, length, null, false,
                false /* unpooled buffers are never allocated out of the thread-local cache */);
    }

    private void init0(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength,
                       PoolThreadCache cache, boolean pooled, boolean threadLocal) {
        assert handle >= 0;
        assert chunk != null;
        assert !PoolChunk.isSubpage(handle) ||
                chunk.arena.sizeClass.size2SizeIdx(maxLength) <= chunk.arena.sizeClass.smallMaxSizeIdx:
                "Allocated small sub-page handle for a buffer size that isn't \"small.\"";

        chunk.incrementPinnedMemory(maxLength);
        this.chunk = chunk;
        memory = chunk.memory;
        tmpNioBuf = nioBuffer;
        allocator = chunk.arena.parent;
        this.cache = cache;
        this.handle = handle;
        this.offset = offset;
        this.length = length;
        this.maxLength = maxLength;
        PooledByteBufAllocator.onAllocateBuffer(this, pooled, threadLocal);
    }

    /**
     * Method must be called before reuse this {@link PooledByteBufAllocator}
     */
    final void reuse(int maxCapacity) {
        maxCapacity(maxCapacity);
        resetRefCnt();
        setIndex0(0, 0);
        discardMarks();
    }

    @Override
    public final int capacity() {
        return length;
    }

    @Override
    public int maxFastWritableBytes() {
        return Math.min(maxLength, maxCapacity()) - writerIndex;
    }

    @Override
    public final ByteBuf capacity(int newCapacity) {
        if (newCapacity == length) {
            ensureAccessible();
            return this;
        }
        checkNewCapacity(newCapacity);
        if (!chunk.unpooled) {
            // If the request capacity does not require reallocation, just update the length of the memory.
            if (newCapacity > length) {

Frequently Asked Questions

What is the PooledByteBuf class?
PooledByteBuf is a class in the netty codebase, defined in buffer/src/main/java/io/netty/buffer/PooledByteBuf.java.
Where is PooledByteBuf defined?
PooledByteBuf is defined in buffer/src/main/java/io/netty/buffer/PooledByteBuf.java at line 30.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free