package io.netty.handler.codec.spdy;

import com.jcraft.jzlib.Deflater;
import com.jcraft.jzlib.JZlib;
import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.compression.CompressionException;

/* JADX INFO: loaded from: classes.dex */
public class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
    private boolean finished;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private final Deflater f5812z;

    public SpdyHeaderBlockJZlibEncoder(SpdyVersion spdyVersion, int i2, int i3, int i4) {
        super(spdyVersion);
        Deflater deflater = new Deflater();
        this.f5812z = deflater;
        if (i2 < 0 || i2 > 9) {
            throw new IllegalArgumentException(a.n("compressionLevel: ", i2, " (expected: 0-9)"));
        }
        if (i3 < 9 || i3 > 15) {
            throw new IllegalArgumentException(a.n("windowBits: ", i3, " (expected: 9-15)"));
        }
        if (i4 < 1 || i4 > 9) {
            throw new IllegalArgumentException(a.n("memLevel: ", i4, " (expected: 1-9)"));
        }
        int iDeflateInit = deflater.deflateInit(i2, i3, i4, JZlib.W_ZLIB);
        if (iDeflateInit != 0) {
            throw new CompressionException(a.l("failed to initialize an SPDY header block deflater: ", iDeflateInit));
        }
        byte[] bArr = SpdyCodecUtil.SPDY_DICT;
        int iDeflateSetDictionary = deflater.deflateSetDictionary(bArr, bArr.length);
        if (iDeflateSetDictionary != 0) {
            throw new CompressionException(a.l("failed to set the SPDY dictionary: ", iDeflateSetDictionary));
        }
    }

    private ByteBuf encode(ByteBufAllocator byteBufAllocator) throws Throwable {
        ByteBuf byteBufHeapBuffer;
        try {
            int i2 = this.f5812z.next_in_index;
            int i3 = this.f5812z.next_out_index;
            int iCeil = ((int) Math.ceil(((double) this.f5812z.next_in.length) * 1.001d)) + 12;
            byteBufHeapBuffer = byteBufAllocator.heapBuffer(iCeil);
            try {
                this.f5812z.next_out = byteBufHeapBuffer.array();
                this.f5812z.next_out_index = byteBufHeapBuffer.arrayOffset() + byteBufHeapBuffer.writerIndex();
                this.f5812z.avail_out = iCeil;
                try {
                    int iDeflate = this.f5812z.deflate(2);
                    if (iDeflate != 0) {
                        throw new CompressionException("compression failure: " + iDeflate);
                    }
                    int i4 = this.f5812z.next_out_index - i3;
                    if (i4 > 0) {
                        byteBufHeapBuffer.writerIndex(byteBufHeapBuffer.writerIndex() + i4);
                    }
                    this.f5812z.next_in = null;
                    this.f5812z.next_out = null;
                    return byteBufHeapBuffer;
                } finally {
                    byteBufHeapBuffer.skipBytes(this.f5812z.next_in_index - i2);
                }
            } catch (Throwable th) {
                th = th;
                this.f5812z.next_in = null;
                this.f5812z.next_out = null;
                if (byteBufHeapBuffer != null) {
                    byteBufHeapBuffer.release();
                }
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            byteBufHeapBuffer = null;
        }
    }

    private void setInput(ByteBuf byteBuf) {
        byte[] bArrArray;
        int iArrayOffset;
        int i2 = byteBuf.readableBytes();
        if (byteBuf.hasArray()) {
            bArrArray = byteBuf.array();
            iArrayOffset = byteBuf.readerIndex() + byteBuf.arrayOffset();
        } else {
            bArrArray = new byte[i2];
            byteBuf.getBytes(byteBuf.readerIndex(), bArrArray);
            iArrayOffset = 0;
        }
        this.f5812z.next_in = bArrArray;
        this.f5812z.next_in_index = iArrayOffset;
        this.f5812z.avail_in = i2;
    }

    @Override // io.netty.handler.codec.spdy.SpdyHeaderBlockRawEncoder, io.netty.handler.codec.spdy.SpdyHeaderBlockEncoder
    public ByteBuf encode(ByteBufAllocator byteBufAllocator, SpdyHeadersFrame spdyHeadersFrame) {
        if (spdyHeadersFrame == null) {
            throw new IllegalArgumentException("frame");
        }
        if (this.finished) {
            return Unpooled.EMPTY_BUFFER;
        }
        ByteBuf byteBufEncode = super.encode(byteBufAllocator, spdyHeadersFrame);
        try {
            if (!byteBufEncode.isReadable()) {
                return Unpooled.EMPTY_BUFFER;
            }
            setInput(byteBufEncode);
            return encode(byteBufAllocator);
        } finally {
            byteBufEncode.release();
        }
    }

    @Override // io.netty.handler.codec.spdy.SpdyHeaderBlockRawEncoder, io.netty.handler.codec.spdy.SpdyHeaderBlockEncoder
    public void end() {
        if (this.finished) {
            return;
        }
        this.finished = true;
        this.f5812z.deflateEnd();
        this.f5812z.next_in = null;
        this.f5812z.next_out = null;
    }
}
