package io.netty.handler.codec;

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

/* 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 */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$NewlineType;
        static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$SeparatorType;

        static {
            int[] iArr = new int[NewlineType.values().length];
            $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) {
            }
            int[] iArr2 = new int[SeparatorType.values().length];
            $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);
    }

    private static int c2b(char c2) {
        if (c2 < 256) {
            return (byte) c2;
        }
        return 63;
    }

    private static void writeAscii(ByteBuf byteBuf, int i2, CharSequence charSequence, int i3) {
        if (charSequence instanceof AsciiString) {
            writeAsciiString(byteBuf, i2, (AsciiString) charSequence, i3);
        } else {
            writeCharSequence(byteBuf, i2, charSequence, i3);
        }
    }

    private static void writeAsciiString(ByteBuf byteBuf, int i2, AsciiString asciiString, int i3) {
        ByteBufUtil.copy(asciiString, 0, byteBuf, i2, i3);
    }

    private static void writeCharSequence(ByteBuf byteBuf, int i2, CharSequence charSequence, int i3) {
        int i4 = 0;
        while (i4 < i3) {
            byteBuf.setByte(i2, c2b(charSequence.charAt(i4)));
            i4++;
            i2++;
        }
    }

    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, length);
        int i4 = iWriterIndex + length;
        int i5 = AnonymousClass1.$SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$SeparatorType[this.separatorType.ordinal()];
        if (i5 == 1) {
            byteBuf.setByte(i4, 58);
            i2 = i4 + 1;
        } else {
            if (i5 != 2) {
                throw new Error();
            }
            int i6 = i4 + 1;
            byteBuf.setByte(i4, 58);
            i2 = i6 + 1;
            byteBuf.setByte(i6, 32);
        }
        writeAscii(byteBuf, i2, value, length2);
        int i7 = i2 + length2;
        int i8 = AnonymousClass1.$SwitchMap$io$netty$handler$codec$AsciiHeadersEncoder$NewlineType[this.newlineType.ordinal()];
        if (i8 == 1) {
            byteBuf.setByte(i7, 10);
            i3 = i7 + 1;
        } else {
            if (i8 != 2) {
                throw new Error();
            }
            int i9 = i7 + 1;
            byteBuf.setByte(i7, 13);
            i3 = i9 + 1;
            byteBuf.setByte(i9, 10);
        }
        byteBuf.writerIndex(i3);
    }

    public AsciiHeadersEncoder(ByteBuf byteBuf, SeparatorType separatorType, NewlineType newlineType) {
        if (byteBuf == null) {
            throw new NullPointerException("buf");
        }
        if (separatorType == null) {
            throw new NullPointerException("separatorType");
        }
        if (newlineType == null) {
            throw new NullPointerException("newlineType");
        }
        this.buf = byteBuf;
        this.separatorType = separatorType;
        this.newlineType = newlineType;
    }
}
