package io.netty.handler.codec.http2;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http2.Http2HeadersDecoder;
import io.netty.util.internal.ObjectUtil;

/* JADX INFO: loaded from: classes.dex */
public class DefaultHttp2HeadersDecoder implements Http2HeadersDecoder, Http2HeadersDecoder.Configuration {
    private static final float HEADERS_COUNT_WEIGHT_HISTORICAL = 0.8f;
    private static final float HEADERS_COUNT_WEIGHT_NEW = 0.2f;
    private float headerArraySizeAccumulator;
    private final HpackDecoder hpackDecoder;
    private final boolean validateHeaders;

    public DefaultHttp2HeadersDecoder() {
        this(true);
    }

    public DefaultHttp2HeadersDecoder(boolean z2) {
        this(z2, Http2CodecUtil.DEFAULT_HEADER_LIST_SIZE);
    }

    public DefaultHttp2HeadersDecoder(boolean z2, long j2) {
        this(z2, j2, 32);
    }

    public DefaultHttp2HeadersDecoder(boolean z2, long j2, int i2) {
        this(z2, new HpackDecoder(j2, i2));
    }

    public DefaultHttp2HeadersDecoder(boolean z2, HpackDecoder hpackDecoder) {
        this.headerArraySizeAccumulator = 8.0f;
        this.hpackDecoder = (HpackDecoder) ObjectUtil.checkNotNull(hpackDecoder, "hpackDecoder");
        this.validateHeaders = z2;
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder
    public Http2HeadersDecoder.Configuration configuration() {
        return this;
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder
    public Http2Headers decodeHeaders(int i2, ByteBuf byteBuf) throws Http2Exception {
        try {
            Http2Headers http2HeadersNewHeaders = newHeaders();
            this.hpackDecoder.decode(i2, byteBuf, http2HeadersNewHeaders, this.validateHeaders);
            this.headerArraySizeAccumulator = (this.headerArraySizeAccumulator * HEADERS_COUNT_WEIGHT_HISTORICAL) + (http2HeadersNewHeaders.size() * HEADERS_COUNT_WEIGHT_NEW);
            return http2HeadersNewHeaders;
        } catch (Http2Exception e2) {
            throw e2;
        } catch (Throwable th) {
            throw Http2Exception.connectionError(Http2Error.COMPRESSION_ERROR, th, th.getMessage(), new Object[0]);
        }
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder.Configuration
    public long maxHeaderListSize() {
        return this.hpackDecoder.getMaxHeaderListSize();
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder.Configuration
    public void maxHeaderListSize(long j2, long j3) throws Http2Exception {
        this.hpackDecoder.setMaxHeaderListSize(j2, j3);
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder.Configuration
    public long maxHeaderListSizeGoAway() {
        return this.hpackDecoder.getMaxHeaderListSizeGoAway();
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder.Configuration
    public long maxHeaderTableSize() {
        return this.hpackDecoder.getMaxHeaderTableSize();
    }

    @Override // io.netty.handler.codec.http2.Http2HeadersDecoder.Configuration
    public void maxHeaderTableSize(long j2) throws Http2Exception {
        this.hpackDecoder.setMaxHeaderTableSize(j2);
    }

    public Http2Headers newHeaders() {
        return new DefaultHttp2Headers(this.validateHeaders, (int) this.headerArraySizeAccumulator);
    }

    public final int numberOfHeadersGuess() {
        return (int) this.headerArraySizeAccumulator;
    }

    public final boolean validateHeaders() {
        return this.validateHeaders;
    }
}
