package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class AsciiHeadersEncoder {
    private final ByteBuf buf;
    private final NewlineType newlineType;
    private final SeparatorType separatorType;

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

        static {
            NewlineType.values();
            int[] iArr = new int[2];
            $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$NewlineType = iArr;
            try {
                iArr[NewlineType.LF.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$NewlineType[NewlineType.CRLF.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            SeparatorType.values();
            int[] iArr2 = new int[2];
            $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$SeparatorType = iArr2;
            try {
                iArr2[SeparatorType.COLON.ordinal()] = 1;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$SeparatorType[SeparatorType.COLON_SPACE.ordinal()] = 2;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    public enum NewlineType {
        LF,
        CRLF
    }

    public enum SeparatorType {
        COLON,
        COLON_SPACE
    }

    public AsciiHeadersEncoder(ByteBuf byteBuf) {
        this(byteBuf, SeparatorType.COLON_SPACE, NewlineType.CRLF);
    }

    public AsciiHeadersEncoder(ByteBuf byteBuf, SeparatorType separatorType, NewlineType newlineType) {
        Objects.requireNonNull(byteBuf, "buf");
        Objects.requireNonNull(separatorType, "separatorType");
        Objects.requireNonNull(newlineType, "newlineType");
        this.buf = byteBuf;
        this.separatorType = separatorType;
        this.newlineType = newlineType;
    }

    private static void writeAscii(ByteBuf byteBuf, int i2, CharSequence charSequence) {
        if (charSequence instanceof AsciiString) {
            ByteBufUtil.copy((AsciiString) charSequence, 0, byteBuf, i2, charSequence.length());
        } else {
            byteBuf.setCharSequence(i2, charSequence, CharsetUtil.US_ASCII);
        }
    }

    public void encode(Map.Entry<CharSequence, CharSequence> entry) {
        int i2;
        int i3;
        CharSequence key = entry.getKey();
        CharSequence value = entry.getValue();
        ByteBuf byteBuf = this.buf;
        int length = key.length();
        int length2 = value.length();
        int iWriterIndex = byteBuf.writerIndex();
        byteBuf.ensureWritable(length + length2 + 4);
        writeAscii(byteBuf, iWriterIndex, key);
        int i4 = iWriterIndex + length;
        int iOrdinal = this.separatorType.ordinal();
        if (iOrdinal == 0) {
            byteBuf.setByte(i4, 58);
            i2 = i4 + 1;
        } else {
            if (iOrdinal != 1) {
                throw new Error();
            }
            int i5 = i4 + 1;
            byteBuf.setByte(i4, 58);
            i2 = i5 + 1;
            byteBuf.setByte(i5, 32);
        }
        writeAscii(byteBuf, i2, value);
        int i6 = i2 + length2;
        int iOrdinal2 = this.newlineType.ordinal();
        if (iOrdinal2 == 0) {
            byteBuf.setByte(i6, 10);
            i3 = i6 + 1;
        } else {
            if (iOrdinal2 != 1) {
                throw new Error();
            }
            int i7 = i6 + 1;
            byteBuf.setByte(i6, 13);
            i3 = i7 + 1;
            byteBuf.setByte(i7, 10);
        }
        byteBuf.writerIndex(i3);
    }
}
