package io.netty.buffer;

import io.netty.util.Recycler;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/* JADX INFO: loaded from: classes.dex */
abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    PoolThreadCache cache;
    protected PoolChunk<T> chunk;
    protected long handle;
    protected int length;
    int maxLength;
    protected T memory;
    protected int offset;
    private final Recycler.Handle<PooledByteBuf<T>> recyclerHandle;
    private ByteBuffer tmpNioBuf;

    /* JADX WARN: Multi-variable type inference failed */
    protected PooledByteBuf(Recycler.Handle<? extends PooledByteBuf<T>> handle, int i2) {
        super(i2);
        this.recyclerHandle = handle;
    }

    private void recycle() {
        this.recyclerHandle.recycle(this);
    }

    @Override // io.netty.buffer.ByteBuf
    public final ByteBufAllocator alloc() {
        return this.chunk.arena.parent;
    }

    @Override // io.netty.buffer.ByteBuf
    public final int capacity() {
        return this.length;
    }

    @Override // io.netty.buffer.AbstractReferenceCountedByteBuf
    protected final void deallocate() {
        long j2 = this.handle;
        if (j2 >= 0) {
            this.handle = -1L;
            this.memory = null;
            PoolChunk<T> poolChunk = this.chunk;
            poolChunk.arena.free(poolChunk, j2, this.maxLength, this.cache);
            recycle();
        }
    }

    protected final int idx(int i2) {
        return this.offset + i2;
    }

    void init(PoolChunk<T> poolChunk, long j2, int i2, int i3, int i4, PoolThreadCache poolThreadCache) {
        this.chunk = poolChunk;
        this.handle = j2;
        this.memory = poolChunk.memory;
        this.offset = i2;
        this.length = i3;
        this.maxLength = i4;
        this.tmpNioBuf = null;
        this.cache = poolThreadCache;
    }

    void initUnpooled(PoolChunk<T> poolChunk, int i2) {
        this.chunk = poolChunk;
        this.handle = 0L;
        this.memory = poolChunk.memory;
        this.offset = 0;
        this.maxLength = i2;
        this.length = i2;
        this.tmpNioBuf = null;
        this.cache = null;
    }

    protected final ByteBuffer internalNioBuffer() {
        ByteBuffer byteBuffer = this.tmpNioBuf;
        if (byteBuffer != null) {
            return byteBuffer;
        }
        ByteBuffer byteBufferNewInternalNioBuffer = newInternalNioBuffer(this.memory);
        this.tmpNioBuf = byteBufferNewInternalNioBuffer;
        return byteBufferNewInternalNioBuffer;
    }

    protected abstract ByteBuffer newInternalNioBuffer(T t);

    @Override // io.netty.buffer.ByteBuf
    public final ByteOrder order() {
        return ByteOrder.BIG_ENDIAN;
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public final ByteBuf retainedDuplicate() {
        return PooledDuplicatedByteBuf.newInstance(this, readerIndex(), writerIndex());
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public final ByteBuf retainedSlice() {
        int i2 = readerIndex();
        return retainedSlice(i2, writerIndex() - i2);
    }

    final void reuse(int i2) {
        maxCapacity(i2);
        setRefCnt(1);
        setIndex0(0, 0);
        discardMarks();
    }

    @Override // io.netty.buffer.ByteBuf
    public final ByteBuf unwrap() {
        return null;
    }

    @Override // io.netty.buffer.ByteBuf
    public final ByteBuf capacity(int i2) {
        ensureAccessible();
        if (!this.chunk.unpooled) {
            int i3 = this.length;
            if (i2 <= i3) {
                if (i2 < i3) {
                    int i4 = this.maxLength;
                    if (i2 > (i4 >>> 1)) {
                        if (i4 > 512) {
                            this.length = i2;
                            setIndex(Math.min(readerIndex(), i2), Math.min(writerIndex(), i2));
                            return this;
                        }
                        if (i2 > i4 - 16) {
                            this.length = i2;
                            setIndex(Math.min(readerIndex(), i2), Math.min(writerIndex(), i2));
                            return this;
                        }
                    }
                }
                return this;
            }
            if (i2 <= this.maxLength) {
                this.length = i2;
                return this;
            }
        } else if (i2 == this.length) {
            return this;
        }
        this.chunk.arena.reallocate(this, i2, true);
        return this;
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public final ByteBuf retainedSlice(int i2, int i3) {
        return PooledSlicedByteBuf.newInstance(this, i2, i3, i2);
    }
}
