package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.ChannelPromiseNotifier;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.util.concurrent.TimeUnit;
import java.util.zip.Checksum;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Exception;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.xxhash.XXHashFactory;

/* JADX INFO: loaded from: classes.dex */
public class Lz4FrameEncoder extends MessageToByteEncoder<ByteBuf> {
    private byte[] buffer;
    private Checksum checksum;
    private final int compressedBlockSize;
    private final int compressionLevel;
    private LZ4Compressor compressor;
    private volatile ChannelHandlerContext ctx;
    private int currentBlockLength;
    private volatile boolean finished;

    public Lz4FrameEncoder() {
        this(false);
    }

    private static int compressionLevel(int i2) {
        if (i2 < 64 || i2 > 33554432) {
            throw new IllegalArgumentException(String.format("blockSize: %d (expected: %d-%d)", Integer.valueOf(i2), 64, 33554432));
        }
        return Math.max(0, (32 - Integer.numberOfLeadingZeros(i2 - 1)) - 10);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public ChannelHandlerContext ctx() {
        ChannelHandlerContext channelHandlerContext = this.ctx;
        if (channelHandlerContext != null) {
            return channelHandlerContext;
        }
        throw new IllegalStateException("not added to a pipeline");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public ChannelFuture finishEncode(ChannelHandlerContext channelHandlerContext, ChannelPromise channelPromise) {
        if (this.finished) {
            channelPromise.setSuccess();
            return channelPromise;
        }
        this.finished = true;
        ByteBuf byteBufHeapBuffer = channelHandlerContext.alloc().heapBuffer(this.compressor.maxCompressedLength(this.currentBlockLength) + 21);
        flushBufferedData(byteBufHeapBuffer);
        int iWriterIndex = byteBufHeapBuffer.writerIndex();
        byte[] bArrArray = byteBufHeapBuffer.array();
        int iArrayOffset = byteBufHeapBuffer.arrayOffset() + iWriterIndex;
        byteBufHeapBuffer.setLong(iWriterIndex, 5501767354678207339L);
        bArrArray[iArrayOffset + 8] = (byte) (this.compressionLevel | 16);
        writeIntLE(0, bArrArray, iArrayOffset + 9);
        writeIntLE(0, bArrArray, iArrayOffset + 13);
        writeIntLE(0, bArrArray, iArrayOffset + 17);
        byteBufHeapBuffer.writerIndex(iWriterIndex + 21);
        this.compressor = null;
        this.checksum = null;
        this.buffer = null;
        return channelHandlerContext.writeAndFlush(byteBufHeapBuffer, channelPromise);
    }

    private void flushBufferedData(ByteBuf byteBuf) {
        int i2;
        int i3;
        int i4 = this.currentBlockLength;
        if (i4 == 0) {
            return;
        }
        this.checksum.reset();
        this.checksum.update(this.buffer, 0, i4);
        int value = (int) this.checksum.getValue();
        byteBuf.ensureWritable(this.compressedBlockSize);
        int iWriterIndex = byteBuf.writerIndex();
        byte[] bArrArray = byteBuf.array();
        int iArrayOffset = byteBuf.arrayOffset() + iWriterIndex;
        try {
            int i5 = iArrayOffset + 21;
            int iCompress = this.compressor.compress(this.buffer, 0, i4, bArrArray, i5);
            if (iCompress >= i4) {
                i3 = 16;
                System.arraycopy(this.buffer, 0, bArrArray, i5, i4);
                i2 = i4;
            } else {
                i2 = iCompress;
                i3 = 32;
            }
            byteBuf.setLong(iWriterIndex, 5501767354678207339L);
            bArrArray[iArrayOffset + 8] = (byte) (i3 | this.compressionLevel);
            writeIntLE(i2, bArrArray, iArrayOffset + 9);
            writeIntLE(i4, bArrArray, iArrayOffset + 13);
            writeIntLE(value, bArrArray, iArrayOffset + 17);
            byteBuf.writerIndex(iWriterIndex + 21 + i2);
            this.currentBlockLength = 0;
        } catch (LZ4Exception e2) {
            throw new CompressionException((Throwable) e2);
        }
    }

    private static void writeIntLE(int i2, byte[] bArr, int i3) {
        int i4 = i3 + 1;
        bArr[i3] = (byte) i2;
        int i5 = i4 + 1;
        bArr[i4] = (byte) (i2 >>> 8);
        bArr[i5] = (byte) (i2 >>> 16);
        bArr[i5 + 1] = (byte) (i2 >>> 24);
    }

    public ChannelFuture close() {
        return close(ctx().newPromise());
    }

    @Override // io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {
        this.ctx = channelHandlerContext;
    }

    public boolean isClosed() {
        return this.finished;
    }

    public Lz4FrameEncoder(boolean z) {
        this(LZ4Factory.fastestInstance(), z, 65536, XXHashFactory.fastestInstance().newStreamingHash32(-1756908916).asChecksum());
    }

    public ChannelFuture close(final ChannelPromise channelPromise) {
        ChannelHandlerContext channelHandlerContextCtx = ctx();
        EventExecutor eventExecutorExecutor = channelHandlerContextCtx.executor();
        if (eventExecutorExecutor.inEventLoop()) {
            return finishEncode(channelHandlerContextCtx, channelPromise);
        }
        eventExecutorExecutor.execute(new Runnable() { // from class: io.netty.handler.codec.compression.Lz4FrameEncoder.1
            @Override // java.lang.Runnable
            public void run() {
                Lz4FrameEncoder lz4FrameEncoder = Lz4FrameEncoder.this;
                lz4FrameEncoder.finishEncode(lz4FrameEncoder.ctx(), channelPromise).addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelPromiseNotifier(channelPromise));
            }
        });
        return channelPromise;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.handler.codec.MessageToByteEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) throws Exception {
        if (this.finished) {
            byteBuf2.writeBytes(byteBuf);
            return;
        }
        int i2 = byteBuf.readableBytes();
        byte[] bArr = this.buffer;
        int length = bArr.length;
        while (true) {
            int i3 = this.currentBlockLength;
            if (i3 + i2 < length) {
                byteBuf.readBytes(bArr, i3, i2);
                this.currentBlockLength += i2;
                return;
            }
            int i4 = length - i3;
            byteBuf.getBytes(byteBuf.readerIndex(), bArr, this.currentBlockLength, i4);
            this.currentBlockLength = length;
            flushBufferedData(byteBuf2);
            byteBuf.skipBytes(i4);
            i2 -= i4;
        }
    }

    public Lz4FrameEncoder(LZ4Factory lZ4Factory, boolean z, int i2, Checksum checksum) {
        super(false);
        if (lZ4Factory == null) {
            throw new NullPointerException("factory");
        }
        if (checksum != null) {
            this.compressor = z ? lZ4Factory.highCompressor() : lZ4Factory.fastCompressor();
            this.checksum = checksum;
            this.compressionLevel = compressionLevel(i2);
            this.buffer = new byte[i2];
            this.currentBlockLength = 0;
            this.compressedBlockSize = this.compressor.maxCompressedLength(i2) + 21;
            this.finished = false;
            return;
        }
        throw new NullPointerException("checksum");
    }

    @Override // io.netty.channel.ChannelOutboundHandlerAdapter, io.netty.channel.ChannelOutboundHandler
    public void close(final ChannelHandlerContext channelHandlerContext, final ChannelPromise channelPromise) throws Exception {
        ChannelFuture channelFutureFinishEncode = finishEncode(channelHandlerContext, channelHandlerContext.newPromise());
        channelFutureFinishEncode.addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.handler.codec.compression.Lz4FrameEncoder.2
            @Override // io.netty.util.concurrent.GenericFutureListener
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                channelHandlerContext.close(channelPromise);
            }
        });
        if (channelFutureFinishEncode.isDone()) {
            return;
        }
        channelHandlerContext.executor().schedule(new Runnable() { // from class: io.netty.handler.codec.compression.Lz4FrameEncoder.3
            @Override // java.lang.Runnable
            public void run() {
                channelHandlerContext.close(channelPromise);
            }
        }, 10L, TimeUnit.SECONDS);
    }
}
