package io.netty.buffer;

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

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

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

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

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

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

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

    @Override // io.netty.buffer.ByteBuf
    public final ByteBuf capacity(int i2) {
        checkNewCapacity(i2);
        PoolChunk<T> poolChunk = this.chunk;
        if (!poolChunk.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;
        }
        poolChunk.arena.reallocate(this, i2, true);
        return this;
    }

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

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

    public void init(PoolChunk<T> poolChunk, long j2, int i2, int i3, int i4, PoolThreadCache poolThreadCache) {
        init0(poolChunk, j2, i2, i3, i4, poolThreadCache);
    }

    public void initUnpooled(PoolChunk<T> poolChunk, int i2) {
        init0(poolChunk, 0L, poolChunk.offset, i2, i2, null);
    }

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

    public abstract ByteBuffer newInternalNioBuffer(T t2);

    @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, this, readerIndex(), writerIndex());
    }

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

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

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

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