package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import lzma.sdk.ICodeProgress;
import lzma.sdk.lzma.Encoder;

/* JADX INFO: loaded from: classes.dex */
public class LzmaFrameEncoder extends MessageToByteEncoder<ByteBuf> {
    private static final int DEFAULT_LC = 3;
    private static final int DEFAULT_LP = 0;
    private static final int DEFAULT_MATCH_FINDER = 1;
    private static final int DEFAULT_PB = 2;
    private static final int MAX_FAST_BYTES = 273;
    private static final int MEDIUM_DICTIONARY_SIZE = 65536;
    private static final int MEDIUM_FAST_BYTES = 32;
    private static final int MIN_FAST_BYTES = 5;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) LzmaFrameEncoder.class);
    private static boolean warningLogged;
    private final Encoder encoder;
    private final int littleEndianDictionarySize;
    private final byte properties;

    public LzmaFrameEncoder() {
        this(MEDIUM_DICTIONARY_SIZE);
    }

    private static int maxOutputBufferLength(int i2) {
        return ((int) (((double) i2) * (i2 < 200 ? 1.5d : i2 < 500 ? 1.2d : i2 < 1000 ? 1.1d : i2 < 10000 ? 1.05d : 1.02d))) + 13;
    }

    public LzmaFrameEncoder(int i2, int i3, int i4) {
        this(i2, i3, i4, MEDIUM_DICTIONARY_SIZE);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.handler.codec.MessageToByteEncoder
    public ByteBuf allocateBuffer(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, boolean z) throws Exception {
        return channelHandlerContext.alloc().ioBuffer(maxOutputBufferLength(byteBuf.readableBytes()));
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.handler.codec.MessageToByteEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) throws Exception {
        int i2 = byteBuf.readableBytes();
        ByteBufInputStream byteBufInputStream = new ByteBufInputStream(byteBuf);
        ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(byteBuf2);
        byteBufOutputStream.writeByte(this.properties);
        byteBufOutputStream.writeInt(this.littleEndianDictionarySize);
        byteBufOutputStream.writeLong(Long.reverseBytes(i2));
        this.encoder.code(byteBufInputStream, byteBufOutputStream, -1L, -1L, (ICodeProgress) null);
        byteBufInputStream.close();
        byteBufOutputStream.close();
    }

    public LzmaFrameEncoder(int i2) {
        this(3, 0, 2, i2);
    }

    public LzmaFrameEncoder(int i2, int i3, int i4, int i5) {
        this(i2, i3, i4, i5, false, 32);
    }

    public LzmaFrameEncoder(int i2, int i3, int i4, int i5, boolean z, int i6) {
        if (i2 < 0 || i2 > 8) {
            throw new IllegalArgumentException("lc: " + i2 + " (expected: 0-8)");
        }
        if (i3 < 0 || i3 > 4) {
            throw new IllegalArgumentException("lp: " + i3 + " (expected: 0-4)");
        }
        if (i4 >= 0 && i4 <= 4) {
            if (i2 + i3 > 4 && !warningLogged) {
                logger.warn("The latest versions of LZMA libraries (for example, XZ Utils) has an additional requirement: lc + lp <= 4. Data which don't follow this requirement cannot be decompressed with this libraries.");
                warningLogged = true;
            }
            if (i5 < 0) {
                throw new IllegalArgumentException("dictionarySize: " + i5 + " (expected: 0+)");
            }
            if (i6 >= 5 && i6 <= MAX_FAST_BYTES) {
                Encoder encoder = new Encoder();
                this.encoder = encoder;
                encoder.setDictionarySize(i5);
                this.encoder.setEndMarkerMode(z);
                this.encoder.setMatchFinder(1);
                this.encoder.setNumFastBytes(i6);
                this.encoder.setLcLpPb(i2, i3, i4);
                this.properties = (byte) ((((i4 * 5) + i3) * 9) + i2);
                this.littleEndianDictionarySize = Integer.reverseBytes(i5);
                return;
            }
            throw new IllegalArgumentException(String.format("numFastBytes: %d (expected: %d-%d)", Integer.valueOf(i6), 5, Integer.valueOf(MAX_FAST_BYTES)));
        }
        throw new IllegalArgumentException("pb: " + i4 + " (expected: 0-4)");
    }
}
