package io.netty.handler.codec.compression;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/* JADX INFO: loaded from: classes.dex */
public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {
    private static final int MIN_COMPRESSIBLE_LENGTH = 18;
    private static final byte[] STREAM_START = {-1, 6, 0, 0, 115, 78, 97, 80, 112, 89};
    private final Snappy snappy = new Snappy();
    private boolean started;

    private static void calculateAndWriteChecksum(ByteBuf byteBuf, ByteBuf byteBuf2) {
        byteBuf2.writeIntLE(Snappy.calculateChecksum(byteBuf));
    }

    private static void setChunkLength(ByteBuf byteBuf, int i2) {
        int iWriterIndex = (byteBuf.writerIndex() - i2) - 3;
        if ((iWriterIndex >>> 24) != 0) {
            throw new CompressionException(a.l("compressed data too large: ", iWriterIndex));
        }
        byteBuf.setMediumLE(i2, iWriterIndex);
    }

    private static void writeChunkLength(ByteBuf byteBuf, int i2) {
        byteBuf.writeMediumLE(i2);
    }

    private static void writeUnencodedChunk(ByteBuf byteBuf, ByteBuf byteBuf2, int i2) {
        byteBuf2.writeByte(1);
        writeChunkLength(byteBuf2, i2 + 4);
        calculateAndWriteChecksum(byteBuf, byteBuf2);
        byteBuf2.writeBytes(byteBuf, i2);
    }

    @Override // io.netty.handler.codec.MessageToByteEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, ByteBuf byteBuf2) {
        if (byteBuf.isReadable()) {
            if (!this.started) {
                this.started = true;
                byteBuf2.writeBytes(STREAM_START);
            }
            int i2 = byteBuf.readableBytes();
            if (i2 > 18) {
                while (true) {
                    int iWriterIndex = byteBuf2.writerIndex() + 1;
                    if (i2 < 18) {
                        byteBuf = byteBuf.readSlice(i2);
                        break;
                    }
                    byteBuf2.writeInt(0);
                    if (i2 <= 32767) {
                        ByteBuf slice = byteBuf.readSlice(i2);
                        calculateAndWriteChecksum(slice, byteBuf2);
                        this.snappy.encode(slice, byteBuf2, i2);
                        setChunkLength(byteBuf2, iWriterIndex);
                        return;
                    }
                    ByteBuf slice2 = byteBuf.readSlice(32767);
                    calculateAndWriteChecksum(slice2, byteBuf2);
                    this.snappy.encode(slice2, byteBuf2, 32767);
                    setChunkLength(byteBuf2, iWriterIndex);
                    i2 -= 32767;
                }
            }
            writeUnencodedChunk(byteBuf, byteBuf2, i2);
        }
    }
}
