package io.netty.buffer;

import io.netty.util.Recycler;
import io.netty.util.internal.PlatformDependent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;

/* JADX INFO: loaded from: classes.dex */
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
    private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() { // from class: io.netty.buffer.PooledUnsafeDirectByteBuf.1
        /* JADX INFO: Access modifiers changed from: protected */
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // io.netty.util.Recycler
        /* JADX INFO: renamed from: newObject */
        public PooledUnsafeDirectByteBuf newObject2(Recycler.Handle<PooledUnsafeDirectByteBuf> handle) {
            return new PooledUnsafeDirectByteBuf(handle, 0);
        }
    };
    private long memoryAddress;

    private long addr(int i2) {
        return this.memoryAddress + ((long) i2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void initMemoryAddress() {
        this.memoryAddress = PlatformDependent.directBufferAddress((ByteBuffer) this.memory) + ((long) this.offset);
    }

    static PooledUnsafeDirectByteBuf newInstance(int i2) {
        PooledUnsafeDirectByteBuf pooledUnsafeDirectByteBuf = RECYCLER.get();
        pooledUnsafeDirectByteBuf.reuse(i2);
        return pooledUnsafeDirectByteBuf;
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected byte _getByte(int i2) {
        return UnsafeByteBufUtil.getByte(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected int _getInt(int i2) {
        return UnsafeByteBufUtil.getInt(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected int _getIntLE(int i2) {
        return UnsafeByteBufUtil.getIntLE(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected long _getLong(int i2) {
        return UnsafeByteBufUtil.getLong(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected long _getLongLE(int i2) {
        return UnsafeByteBufUtil.getLongLE(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected short _getShort(int i2) {
        return UnsafeByteBufUtil.getShort(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected short _getShortLE(int i2) {
        return UnsafeByteBufUtil.getShortLE(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected int _getUnsignedMedium(int i2) {
        return UnsafeByteBufUtil.getUnsignedMedium(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected int _getUnsignedMediumLE(int i2) {
        return UnsafeByteBufUtil.getUnsignedMediumLE(addr(i2));
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setByte(int i2, int i3) {
        UnsafeByteBufUtil.setByte(addr(i2), (byte) i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setInt(int i2, int i3) {
        UnsafeByteBufUtil.setInt(addr(i2), i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setIntLE(int i2, int i3) {
        UnsafeByteBufUtil.setIntLE(addr(i2), i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setLong(int i2, long j2) {
        UnsafeByteBufUtil.setLong(addr(i2), j2);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setLongLE(int i2, long j2) {
        UnsafeByteBufUtil.setLongLE(addr(i2), j2);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setMedium(int i2, int i3) {
        UnsafeByteBufUtil.setMedium(addr(i2), i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setMediumLE(int i2, int i3) {
        UnsafeByteBufUtil.setMediumLE(addr(i2), i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setShort(int i2, int i3) {
        UnsafeByteBufUtil.setShort(addr(i2), i3);
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected void _setShortLE(int i2, int i3) {
        UnsafeByteBufUtil.setShortLE(addr(i2), i3);
    }

    @Override // io.netty.buffer.ByteBuf
    public byte[] array() {
        throw new UnsupportedOperationException("direct buffer");
    }

    @Override // io.netty.buffer.ByteBuf
    public int arrayOffset() {
        throw new UnsupportedOperationException("direct buffer");
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf copy(int i2, int i3) {
        return UnsafeByteBufUtil.copy(this, addr(i2), i2, i3);
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf getBytes(int i2, ByteBuf byteBuf, int i3, int i4) {
        UnsafeByteBufUtil.getBytes(this, addr(i2), i2, byteBuf, i3, i4);
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public boolean hasArray() {
        return false;
    }

    @Override // io.netty.buffer.ByteBuf
    public boolean hasMemoryAddress() {
        return true;
    }

    @Override // io.netty.buffer.PooledByteBuf
    void init(PoolChunk<ByteBuffer> poolChunk, long j2, int i2, int i3, int i4, PoolThreadCache poolThreadCache) {
        super.init(poolChunk, j2, i2, i3, i4, poolThreadCache);
        initMemoryAddress();
    }

    @Override // io.netty.buffer.PooledByteBuf
    void initUnpooled(PoolChunk<ByteBuffer> poolChunk, int i2) {
        super.initUnpooled(poolChunk, i2);
        initMemoryAddress();
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuffer internalNioBuffer(int i2, int i3) {
        checkIndex(i2, i3);
        int iIdx = idx(i2);
        return (ByteBuffer) internalNioBuffer().clear().position(iIdx).limit(iIdx + i3);
    }

    @Override // io.netty.buffer.ByteBuf
    public boolean isDirect() {
        return true;
    }

    @Override // io.netty.buffer.ByteBuf
    public long memoryAddress() {
        ensureAccessible();
        return this.memoryAddress;
    }

    @Override // io.netty.buffer.AbstractByteBuf
    protected SwappedByteBuf newSwappedByteBuf() {
        return PlatformDependent.isUnaligned() ? new UnsafeDirectSwappedByteBuf(this) : super.newSwappedByteBuf();
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // io.netty.buffer.ByteBuf
    public ByteBuffer nioBuffer(int i2, int i3) {
        checkIndex(i2, i3);
        int iIdx = idx(i2);
        return ((ByteBuffer) ((ByteBuffer) this.memory).duplicate().position(iIdx).limit(iIdx + i3)).slice();
    }

    @Override // io.netty.buffer.ByteBuf
    public int nioBufferCount() {
        return 1;
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuffer[] nioBuffers(int i2, int i3) {
        return new ByteBuffer[]{nioBuffer(i2, i3)};
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public ByteBuf readBytes(ByteBuffer byteBuffer) {
        int iRemaining = byteBuffer.remaining();
        checkReadableBytes(iRemaining);
        getBytes(this.readerIndex, byteBuffer);
        this.readerIndex += iRemaining;
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf setBytes(int i2, ByteBuf byteBuf, int i3, int i4) {
        UnsafeByteBufUtil.setBytes(this, addr(i2), i2, byteBuf, i3, i4);
        return this;
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public ByteBuf setZero(int i2, int i3) {
        UnsafeByteBufUtil.setZero(this, addr(i2), i2, i3);
        return this;
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public ByteBuf writeZero(int i2) {
        ensureWritable(i2);
        int i3 = this.writerIndex;
        setZero(i3, i2);
        this.writerIndex = i3 + i2;
        return this;
    }

    private PooledUnsafeDirectByteBuf(Recycler.Handle<PooledUnsafeDirectByteBuf> handle, int i2) {
        super(handle, i2);
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf getBytes(int i2, byte[] bArr, int i3, int i4) {
        UnsafeByteBufUtil.getBytes(this, addr(i2), i2, bArr, i3, i4);
        return this;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.buffer.PooledByteBuf
    public ByteBuffer newInternalNioBuffer(ByteBuffer byteBuffer) {
        return byteBuffer.duplicate();
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf setBytes(int i2, byte[] bArr, int i3, int i4) {
        UnsafeByteBufUtil.setBytes(this, addr(i2), i2, bArr, i3, i4);
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf getBytes(int i2, ByteBuffer byteBuffer) {
        UnsafeByteBufUtil.getBytes(this, addr(i2), i2, byteBuffer);
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf setBytes(int i2, ByteBuffer byteBuffer) {
        UnsafeByteBufUtil.setBytes(this, addr(i2), i2, byteBuffer);
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public ByteBuf getBytes(int i2, OutputStream outputStream, int i3) throws IOException {
        UnsafeByteBufUtil.getBytes(this, addr(i2), i2, outputStream, i3);
        return this;
    }

    @Override // io.netty.buffer.ByteBuf
    public int setBytes(int i2, InputStream inputStream, int i3) throws IOException {
        return UnsafeByteBufUtil.setBytes(this, addr(i2), i2, inputStream, i3);
    }

    @Override // io.netty.buffer.ByteBuf
    public int getBytes(int i2, GatheringByteChannel gatheringByteChannel, int i3) throws IOException {
        return getBytes(i2, gatheringByteChannel, i3, false);
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public int readBytes(GatheringByteChannel gatheringByteChannel, int i2) throws IOException {
        checkReadableBytes(i2);
        int bytes = getBytes(this.readerIndex, gatheringByteChannel, i2, true);
        this.readerIndex += bytes;
        return bytes;
    }

    @Override // io.netty.buffer.ByteBuf
    public int setBytes(int i2, ScatteringByteChannel scatteringByteChannel, int i3) throws IOException {
        checkIndex(i2, i3);
        ByteBuffer byteBufferInternalNioBuffer = internalNioBuffer();
        int iIdx = idx(i2);
        byteBufferInternalNioBuffer.clear().position(iIdx).limit(iIdx + i3);
        try {
            return scatteringByteChannel.read(byteBufferInternalNioBuffer);
        } catch (ClosedChannelException unused) {
            return -1;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private int getBytes(int i2, GatheringByteChannel gatheringByteChannel, int i3, boolean z) throws IOException {
        ByteBuffer byteBufferDuplicate;
        checkIndex(i2, i3);
        if (i3 == 0) {
            return 0;
        }
        if (z) {
            byteBufferDuplicate = internalNioBuffer();
        } else {
            byteBufferDuplicate = ((ByteBuffer) this.memory).duplicate();
        }
        int iIdx = idx(i2);
        byteBufferDuplicate.clear().position(iIdx).limit(iIdx + i3);
        return gatheringByteChannel.write(byteBufferDuplicate);
    }

    @Override // io.netty.buffer.AbstractByteBuf, io.netty.buffer.ByteBuf
    public int readBytes(FileChannel fileChannel, long j2, int i2) throws IOException {
        checkReadableBytes(i2);
        int bytes = getBytes(this.readerIndex, fileChannel, j2, i2, true);
        this.readerIndex += bytes;
        return bytes;
    }

    @Override // io.netty.buffer.ByteBuf
    public int setBytes(int i2, FileChannel fileChannel, long j2, int i3) throws IOException {
        checkIndex(i2, i3);
        ByteBuffer byteBufferInternalNioBuffer = internalNioBuffer();
        int iIdx = idx(i2);
        byteBufferInternalNioBuffer.clear().position(iIdx).limit(iIdx + i3);
        try {
            return fileChannel.read(byteBufferInternalNioBuffer, j2);
        } catch (ClosedChannelException unused) {
            return -1;
        }
    }

    @Override // io.netty.buffer.ByteBuf
    public int getBytes(int i2, FileChannel fileChannel, long j2, int i3) throws IOException {
        return getBytes(i2, fileChannel, j2, i3, false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private int getBytes(int i2, FileChannel fileChannel, long j2, int i3, boolean z) throws IOException {
        checkIndex(i2, i3);
        if (i3 == 0) {
            return 0;
        }
        ByteBuffer byteBufferInternalNioBuffer = z ? internalNioBuffer() : ((ByteBuffer) this.memory).duplicate();
        int iIdx = idx(i2);
        byteBufferInternalNioBuffer.clear().position(iIdx).limit(iIdx + i3);
        return fileChannel.write(byteBufferInternalNioBuffer, j2);
    }
}
