package io.netty.handler.codec.compression;

import com.jcraft.jzlib.Deflater;
import com.jcraft.jzlib.JZlib;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.internal.EmptyArrays;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class JZlibEncoder extends ZlibEncoder {
    private volatile ChannelHandlerContext ctx;
    private volatile boolean finished;
    private final int wrapperOverhead;
    private final Deflater z;

    public JZlibEncoder() {
        this(6);
    }

    /* 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;
        try {
            this.z.next_in = EmptyArrays.EMPTY_BYTES;
            this.z.next_in_index = 0;
            this.z.avail_in = 0;
            byte[] bArr = new byte[32];
            this.z.next_out = bArr;
            this.z.next_out_index = 0;
            this.z.avail_out = 32;
            int iDeflate = this.z.deflate(4);
            if (iDeflate != 0 && iDeflate != 1) {
                channelPromise.setFailure((Throwable) ZlibUtil.deflaterException(this.z, "compression failure", iDeflate));
                return channelPromise;
            }
            ByteBuf byteBufWrappedBuffer = this.z.next_out_index != 0 ? Unpooled.wrappedBuffer(bArr, 0, this.z.next_out_index) : Unpooled.EMPTY_BUFFER;
            this.z.deflateEnd();
            this.z.next_in = null;
            this.z.next_out = null;
            return channelHandlerContext.writeAndFlush(byteBufWrappedBuffer, channelPromise);
        } finally {
            this.z.deflateEnd();
            this.z.next_in = null;
            this.z.next_out = null;
        }
    }

    @Override // io.netty.handler.codec.compression.ZlibEncoder
    public ChannelFuture close() {
        return close(ctx().channel().newPromise());
    }

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

    @Override // io.netty.handler.codec.compression.ZlibEncoder
    public boolean isClosed() {
        return this.finished;
    }

    public JZlibEncoder(int i2) {
        this(ZlibWrapper.ZLIB, i2);
    }

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

    /* 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();
        if (i2 == 0) {
            return;
        }
        try {
            boolean zHasArray = byteBuf.hasArray();
            this.z.avail_in = i2;
            if (zHasArray) {
                this.z.next_in = byteBuf.array();
                this.z.next_in_index = byteBuf.arrayOffset() + byteBuf.readerIndex();
            } else {
                byte[] bArr = new byte[i2];
                byteBuf.getBytes(byteBuf.readerIndex(), bArr);
                this.z.next_in = bArr;
                this.z.next_in_index = 0;
            }
            int i3 = this.z.next_in_index;
            double d2 = i2;
            Double.isNaN(d2);
            int iCeil = ((int) Math.ceil(d2 * 1.001d)) + 12 + this.wrapperOverhead;
            byteBuf2.ensureWritable(iCeil);
            this.z.avail_out = iCeil;
            this.z.next_out = byteBuf2.array();
            this.z.next_out_index = byteBuf2.arrayOffset() + byteBuf2.writerIndex();
            int i4 = this.z.next_out_index;
            try {
                int iDeflate = this.z.deflate(2);
                if (iDeflate != 0) {
                    ZlibUtil.fail(this.z, "compression failure", iDeflate);
                }
                int i5 = this.z.next_out_index - i4;
                if (i5 > 0) {
                    byteBuf2.writerIndex(byteBuf2.writerIndex() + i5);
                }
            } finally {
                byteBuf.skipBytes(this.z.next_in_index - i3);
            }
        } finally {
            this.z.next_in = null;
            this.z.next_out = null;
        }
    }

    public JZlibEncoder(ZlibWrapper zlibWrapper) {
        this(zlibWrapper, 6);
    }

    public JZlibEncoder(ZlibWrapper zlibWrapper, int i2) {
        this(zlibWrapper, i2, 15, 8);
    }

    public JZlibEncoder(ZlibWrapper zlibWrapper, int i2, int i3, int i4) {
        Deflater deflater = new Deflater();
        this.z = deflater;
        if (i2 < 0 || i2 > 9) {
            throw new IllegalArgumentException("compressionLevel: " + i2 + " (expected: 0-9)");
        }
        if (i3 < 9 || i3 > 15) {
            throw new IllegalArgumentException("windowBits: " + i3 + " (expected: 9-15)");
        }
        if (i4 < 1 || i4 > 9) {
            throw new IllegalArgumentException("memLevel: " + i4 + " (expected: 1-9)");
        }
        if (zlibWrapper != null) {
            if (zlibWrapper != ZlibWrapper.ZLIB_OR_NONE) {
                int iInit = deflater.init(i2, i3, i4, ZlibUtil.convertWrapperType(zlibWrapper));
                if (iInit != 0) {
                    ZlibUtil.fail(this.z, "initialization failure", iInit);
                }
                this.wrapperOverhead = ZlibUtil.wrapperOverhead(zlibWrapper);
                return;
            }
            throw new IllegalArgumentException("wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not allowed for compression.");
        }
        throw new NullPointerException("wrapper");
    }

    @Override // io.netty.channel.ChannelOutboundHandlerAdapter, io.netty.channel.ChannelOutboundHandler
    public void close(final ChannelHandlerContext channelHandlerContext, final ChannelPromise channelPromise) {
        ChannelFuture channelFutureFinishEncode = finishEncode(channelHandlerContext, channelHandlerContext.newPromise());
        channelFutureFinishEncode.addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.handler.codec.compression.JZlibEncoder.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.JZlibEncoder.3
            @Override // java.lang.Runnable
            public void run() {
                channelHandlerContext.close(channelPromise);
            }
        }, 10L, TimeUnit.SECONDS);
    }

    public JZlibEncoder(byte[] bArr) {
        this(6, bArr);
    }

    public JZlibEncoder(int i2, byte[] bArr) {
        this(i2, 15, 8, bArr);
    }

    public JZlibEncoder(int i2, int i3, int i4, byte[] bArr) {
        Deflater deflater = new Deflater();
        this.z = deflater;
        if (i2 < 0 || i2 > 9) {
            throw new IllegalArgumentException("compressionLevel: " + i2 + " (expected: 0-9)");
        }
        if (i3 < 9 || i3 > 15) {
            throw new IllegalArgumentException("windowBits: " + i3 + " (expected: 9-15)");
        }
        if (i4 < 1 || i4 > 9) {
            throw new IllegalArgumentException("memLevel: " + i4 + " (expected: 1-9)");
        }
        if (bArr != null) {
            int iDeflateInit = deflater.deflateInit(i2, i3, i4, JZlib.W_ZLIB);
            if (iDeflateInit != 0) {
                ZlibUtil.fail(this.z, "initialization failure", iDeflateInit);
            } else {
                int iDeflateSetDictionary = this.z.deflateSetDictionary(bArr, bArr.length);
                if (iDeflateSetDictionary != 0) {
                    ZlibUtil.fail(this.z, "failed to set the dictionary", iDeflateSetDictionary);
                }
            }
            this.wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB);
            return;
        }
        throw new NullPointerException("dictionary");
    }
}
