package io.netty.handler.codec.http;

import g.a.a.a.a;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.compression.ZlibCodecFactory;
import io.netty.handler.codec.compression.ZlibWrapper;
import io.netty.handler.codec.http.HttpContentEncoder;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;

/* JADX INFO: loaded from: classes.dex */
public class HttpContentCompressor extends HttpContentEncoder {
    private final int compressionLevel;
    private final int contentSizeThreshold;
    private ChannelHandlerContext ctx;
    private final int memLevel;
    private final int windowBits;

    /* JADX INFO: renamed from: io.netty.handler.codec.http.HttpContentCompressor$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$compression$ZlibWrapper;

        static {
            ZlibWrapper.values();
            int[] iArr = new int[4];
            $SwitchMap$io$netty$handler$codec$compression$ZlibWrapper = iArr;
            try {
                iArr[ZlibWrapper.GZIP.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$ZlibWrapper[ZlibWrapper.ZLIB.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    public HttpContentCompressor() {
        this(6);
    }

    public HttpContentCompressor(int i2) {
        this(i2, 15, 8, 0);
    }

    public HttpContentCompressor(int i2, int i3, int i4) {
        this(i2, i3, i4, 0);
    }

    public HttpContentCompressor(int i2, int i3, int i4, int i5) {
        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)"));
        }
        if (i5 < 0) {
            throw new IllegalArgumentException(a.n("contentSizeThreshold: ", i5, " (expected: non negative number)"));
        }
        this.compressionLevel = i2;
        this.windowBits = i3;
        this.memLevel = i4;
        this.contentSizeThreshold = i5;
    }

    @Override // io.netty.handler.codec.http.HttpContentEncoder
    public HttpContentEncoder.Result beginEncode(HttpResponse httpResponse, String str) {
        ZlibWrapper zlibWrapperDetermineWrapper;
        String str2;
        if ((this.contentSizeThreshold > 0 && (httpResponse instanceof HttpContent) && ((HttpContent) httpResponse).content().readableBytes() < this.contentSizeThreshold) || httpResponse.headers().get(HttpHeaderNames.CONTENT_ENCODING) != null || (zlibWrapperDetermineWrapper = determineWrapper(str)) == null) {
            return null;
        }
        int iOrdinal = zlibWrapperDetermineWrapper.ordinal();
        if (iOrdinal == 0) {
            str2 = "deflate";
        } else {
            if (iOrdinal != 1) {
                throw new Error();
            }
            str2 = "gzip";
        }
        return new HttpContentEncoder.Result(str2, new EmbeddedChannel(this.ctx.channel().id(), this.ctx.channel().metadata().hasDisconnect(), this.ctx.channel().config(), ZlibCodecFactory.newZlibEncoder(zlibWrapperDetermineWrapper, this.compressionLevel, this.windowBits, this.memLevel)));
    }

    public ZlibWrapper determineWrapper(String str) {
        String[] strArrSplit = str.split(",");
        int length = strArrSplit.length;
        int i2 = 0;
        float f2 = -1.0f;
        float f3 = -1.0f;
        float f4 = -1.0f;
        while (true) {
            float f5 = 0.0f;
            if (i2 >= length) {
                break;
            }
            String str2 = strArrSplit[i2];
            int iIndexOf = str2.indexOf(61);
            if (iIndexOf != -1) {
                try {
                    f5 = Float.parseFloat(str2.substring(iIndexOf + 1));
                } catch (NumberFormatException unused) {
                }
            } else {
                f5 = 1.0f;
            }
            if (str2.contains(WebSocketServerHandshaker.SUB_PROTOCOL_WILDCARD)) {
                f4 = f5;
            } else if (str2.contains("gzip") && f5 > f2) {
                f2 = f5;
            } else if (str2.contains("deflate") && f5 > f3) {
                f3 = f5;
            }
            i2++;
        }
        if (f2 > 0.0f || f3 > 0.0f) {
            return f2 >= f3 ? ZlibWrapper.GZIP : ZlibWrapper.ZLIB;
        }
        if (f4 <= 0.0f) {
            return null;
        }
        if (f2 == -1.0f) {
            return ZlibWrapper.GZIP;
        }
        if (f3 == -1.0f) {
            return ZlibWrapper.ZLIB;
        }
        return null;
    }

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