package io.netty.handler.codec.http;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.FileRegion;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.haproxy.HAProxyConstants;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageToMessageEncoder<Object> {
    private static final ByteBuf CRLF_BUF;
    public static final int CRLF_SHORT = 3338;
    private static final float HEADERS_WEIGHT_HISTORICAL = 0.8f;
    private static final float HEADERS_WEIGHT_NEW = 0.2f;
    private static final int ST_CONTENT_ALWAYS_EMPTY = 3;
    private static final int ST_CONTENT_CHUNK = 2;
    private static final int ST_CONTENT_NON_CHUNK = 1;
    private static final int ST_INIT = 0;
    private static final float TRAILERS_WEIGHT_HISTORICAL = 0.8f;
    private static final float TRAILERS_WEIGHT_NEW = 0.2f;
    private static final byte[] ZERO_CRLF_CRLF;
    private static final ByteBuf ZERO_CRLF_CRLF_BUF;
    private static final int ZERO_CRLF_MEDIUM = 3149066;
    private int state = 0;
    private float headersEncodedSizeAccumulator = 256.0f;
    private float trailersEncodedSizeAccumulator = 256.0f;

    static {
        byte[] bArr = {HAProxyConstants.AF_UNIX_BYTE, 13, 10, 13, 10};
        ZERO_CRLF_CRLF = bArr;
        CRLF_BUF = Unpooled.unreleasableBuffer(Unpooled.directBuffer(2).writeByte(13).writeByte(10));
        ZERO_CRLF_CRLF_BUF = Unpooled.unreleasableBuffer(Unpooled.directBuffer(bArr.length).writeBytes(bArr));
    }

    private static long contentLength(Object obj) {
        if (obj instanceof HttpContent) {
            return ((HttpContent) obj).content().readableBytes();
        }
        if (obj instanceof ByteBuf) {
            return ((ByteBuf) obj).readableBytes();
        }
        if (obj instanceof FileRegion) {
            return ((FileRegion) obj).count();
        }
        StringBuilder sbF = a.F("unexpected message type: ");
        sbF.append(StringUtil.simpleClassName(obj));
        throw new IllegalStateException(sbF.toString());
    }

    private static Object encodeAndRetain(Object obj) {
        if (obj instanceof ByteBuf) {
            return ((ByteBuf) obj).retain();
        }
        if (obj instanceof HttpContent) {
            return ((HttpContent) obj).content().retain();
        }
        if (obj instanceof FileRegion) {
            return ((FileRegion) obj).retain();
        }
        StringBuilder sbF = a.F("unexpected message type: ");
        sbF.append(StringUtil.simpleClassName(obj));
        throw new IllegalStateException(sbF.toString());
    }

    @Deprecated
    public static void encodeAscii(String str, ByteBuf byteBuf) {
        byteBuf.writeCharSequence(str, CharsetUtil.US_ASCII);
    }

    private void encodeChunkedContent(ChannelHandlerContext channelHandlerContext, Object obj, long j2, List<Object> list) {
        Object objEncodeAndRetain;
        if (j2 > 0) {
            String hexString = Long.toHexString(j2);
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(hexString.length() + 2);
            byteBufBuffer.writeCharSequence(hexString, CharsetUtil.US_ASCII);
            ByteBufUtil.writeShortBE(byteBufBuffer, 3338);
            list.add(byteBufBuffer);
            list.add(encodeAndRetain(obj));
            list.add(CRLF_BUF.duplicate());
        }
        if (obj instanceof LastHttpContent) {
            HttpHeaders httpHeadersTrailingHeaders = ((LastHttpContent) obj).trailingHeaders();
            if (httpHeadersTrailingHeaders.isEmpty()) {
                objEncodeAndRetain = ZERO_CRLF_CRLF_BUF.duplicate();
            } else {
                ByteBuf byteBufBuffer2 = channelHandlerContext.alloc().buffer((int) this.trailersEncodedSizeAccumulator);
                ByteBufUtil.writeMediumBE(byteBufBuffer2, ZERO_CRLF_MEDIUM);
                encodeHeaders(httpHeadersTrailingHeaders, byteBufBuffer2);
                ByteBufUtil.writeShortBE(byteBufBuffer2, 3338);
                this.trailersEncodedSizeAccumulator = (this.trailersEncodedSizeAccumulator * 0.8f) + (padSizeForAccumulation(byteBufBuffer2.readableBytes()) * 0.2f);
                objEncodeAndRetain = byteBufBuffer2;
            }
        } else if (j2 != 0) {
            return;
        } else {
            objEncodeAndRetain = encodeAndRetain(obj);
        }
        list.add(objEncodeAndRetain);
    }

    private static int padSizeForAccumulation(int i2) {
        return (i2 << 2) / 3;
    }

    @Override // io.netty.handler.codec.MessageToMessageEncoder
    public boolean acceptOutboundMessage(Object obj) {
        return (obj instanceof HttpObject) || (obj instanceof ByteBuf) || (obj instanceof FileRegion);
    }

    /* JADX WARN: Removed duplicated region for block: B:60:0x00f6  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x00fa  */
    @Override // io.netty.handler.codec.MessageToMessageEncoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void encode(io.netty.channel.ChannelHandlerContext r11, java.lang.Object r12, java.util.List<java.lang.Object> r13) {
        /*
            Method dump skipped, instruction units count: 283
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.http.HttpObjectEncoder.encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List):void");
    }

    public void encodeHeaders(HttpHeaders httpHeaders, ByteBuf byteBuf) {
        Iterator<Map.Entry<CharSequence, CharSequence>> itIteratorCharSequence = httpHeaders.iteratorCharSequence();
        while (itIteratorCharSequence.hasNext()) {
            Map.Entry<CharSequence, CharSequence> next = itIteratorCharSequence.next();
            HttpHeadersEncoder.encoderHeader(next.getKey(), next.getValue(), byteBuf);
        }
    }

    public abstract void encodeInitialLine(ByteBuf byteBuf, H h2);

    public boolean isContentAlwaysEmpty(H h2) {
        return false;
    }

    public void sanitizeHeadersBeforeEncode(H h2, boolean z2) {
    }
}
