package kotlinx.serialization.json.internal;

import g.a.a.a.a;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import kotlin.Metadata;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000D\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\u0019\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\r\n\u0002\u0010\f\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0006\b\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u0018\u0010\u000b\u001a\u00020\f2\u0006\u0010\r\u001a\u00020\n2\u0006\u0010\u000e\u001a\u00020\u000fH\u0002J\u0011\u0010\u0010\u001a\u00020\f2\u0006\u0010\u0011\u001a\u00020\nH\u0082\bJ\u0018\u0010\u0012\u001a\u00020\n2\u0006\u0010\u0013\u001a\u00020\n2\u0006\u0010\u0014\u001a\u00020\nH\u0002J\b\u0010\u0015\u001a\u00020\fH\u0002J\b\u0010\u0016\u001a\u00020\fH\u0016J\t\u0010\u0017\u001a\u00020\nH\u0082\bJ\u0011\u0010\u0018\u001a\u00020\f2\u0006\u0010\u0019\u001a\u00020\nH\u0082\bJ\u0010\u0010\u0018\u001a\u00020\f2\u0006\u0010\u001a\u001a\u00020\u000fH\u0016J\u0010\u0010\u001b\u001a\u00020\f2\u0006\u0010\u001c\u001a\u00020\u001dH\u0016J\u0010\u0010\u001e\u001a\u00020\f2\u0006\u0010\u001f\u001a\u00020 H\u0016J\u0010\u0010!\u001a\u00020\f2\u0006\u0010\u001a\u001a\u00020\u000fH\u0016J\u0018\u0010\"\u001a\u00020\f2\u0006\u0010\u000e\u001a\u00020\b2\u0006\u0010#\u001a\u00020\nH\u0002J\u0010\u0010$\u001a\u00020\f2\u0006\u0010%\u001a\u00020\nH\u0002R\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\bX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\nX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006&"}, d2 = {"Lkotlinx/serialization/json/internal/JsonToJavaStreamWriter;", "Lkotlinx/serialization/json/internal/JsonWriter;", "stream", "Ljava/io/OutputStream;", "(Ljava/io/OutputStream;)V", "buffer", "", "charArray", "", "indexInBuffer", "", "appendStringSlowPath", "", "currentSize", "string", "", "ensure", "bytesCount", "ensureTotalCapacity", "oldSize", "additional", "flush", "release", "rest", "write", "byte", "text", "writeChar", "char", "", "writeLong", "value", "", "writeQuoted", "writeUtf8", "count", "writeUtf8CodePoint", "codePoint", "kotlinx-serialization-json"}, k = 1, mv = {1, 7, 1}, xi = 48)
public final class JsonToJavaStreamWriter implements JsonWriter {
    private final byte[] buffer;
    private char[] charArray;
    private int indexInBuffer;
    private final OutputStream stream;

    public JsonToJavaStreamWriter(OutputStream outputStream) {
        o.f(outputStream, "stream");
        this.stream = outputStream;
        this.buffer = ByteArrayPool.INSTANCE.take();
        this.charArray = CharArrayPool.INSTANCE.take();
    }

    private final void appendStringSlowPath(int currentSize, String string) throws IOException {
        byte b2;
        int length = string.length();
        for (int i2 = currentSize - 1; i2 < length; i2++) {
            int iEnsureTotalCapacity = ensureTotalCapacity(currentSize, 2);
            char cCharAt = string.charAt(i2);
            if (cCharAt >= StringOpsKt.getESCAPE_MARKERS().length || (b2 = StringOpsKt.getESCAPE_MARKERS()[cCharAt]) == 0) {
                int i3 = iEnsureTotalCapacity + 1;
                this.charArray[iEnsureTotalCapacity] = cCharAt;
                currentSize = i3;
            } else {
                if (b2 == 1) {
                    String str = StringOpsKt.getESCAPE_STRINGS()[cCharAt];
                    o.c(str);
                    int iEnsureTotalCapacity2 = ensureTotalCapacity(iEnsureTotalCapacity, str.length());
                    str.getChars(0, str.length(), this.charArray, iEnsureTotalCapacity2);
                    currentSize = str.length() + iEnsureTotalCapacity2;
                } else {
                    char[] cArr = this.charArray;
                    cArr[iEnsureTotalCapacity] = AbstractJsonLexerKt.STRING_ESC;
                    cArr[iEnsureTotalCapacity + 1] = (char) b2;
                    currentSize = iEnsureTotalCapacity + 2;
                }
            }
        }
        ensureTotalCapacity(currentSize, 1);
        char[] cArr2 = this.charArray;
        cArr2[currentSize] = '\"';
        writeUtf8(cArr2, currentSize + 1);
        flush();
    }

    private final void ensure(int bytesCount) throws IOException {
        if (this.buffer.length - this.indexInBuffer < bytesCount) {
            flush();
        }
    }

    private final int ensureTotalCapacity(int oldSize, int additional) {
        int i2 = additional + oldSize;
        char[] cArr = this.charArray;
        if (cArr.length <= i2) {
            int i3 = oldSize * 2;
            if (i2 < i3) {
                i2 = i3;
            }
            char[] cArrCopyOf = Arrays.copyOf(cArr, i2);
            o.e(cArrCopyOf, "copyOf(this, newSize)");
            this.charArray = cArrCopyOf;
        }
        return oldSize;
    }

    private final void flush() throws IOException {
        this.stream.write(this.buffer, 0, this.indexInBuffer);
        this.indexInBuffer = 0;
    }

    private final int rest() {
        return this.buffer.length - this.indexInBuffer;
    }

    private final void write(int i2) {
        byte[] bArr = this.buffer;
        int i3 = this.indexInBuffer;
        this.indexInBuffer = i3 + 1;
        bArr[i3] = (byte) i2;
    }

    private final void writeUtf8(char[] string, int count) throws IOException {
        if (!(count >= 0)) {
            throw new IllegalArgumentException("count < 0".toString());
        }
        if (!(count <= string.length)) {
            StringBuilder sbG = a.G("count > string.length: ", count, " > ");
            sbG.append(string.length);
            throw new IllegalArgumentException(sbG.toString().toString());
        }
        int i2 = 0;
        while (i2 < count) {
            char c2 = string[i2];
            if (c2 < 128) {
                if (this.buffer.length - this.indexInBuffer < 1) {
                    flush();
                }
                byte[] bArr = this.buffer;
                int i3 = this.indexInBuffer;
                int i4 = i3 + 1;
                this.indexInBuffer = i4;
                bArr[i3] = (byte) c2;
                i2++;
                int iMin = Math.min(count, (bArr.length - i4) + i2);
                while (i2 < iMin) {
                    char c3 = string[i2];
                    if (c3 < 128) {
                        byte[] bArr2 = this.buffer;
                        int i5 = this.indexInBuffer;
                        this.indexInBuffer = i5 + 1;
                        bArr2[i5] = (byte) c3;
                        i2++;
                    }
                }
            } else {
                if (c2 < 2048) {
                    if (this.buffer.length - this.indexInBuffer < 2) {
                        flush();
                    }
                    byte[] bArr3 = this.buffer;
                    int i6 = this.indexInBuffer;
                    int i7 = i6 + 1;
                    this.indexInBuffer = i7;
                    bArr3[i6] = (byte) ((c2 >> 6) | 192);
                    this.indexInBuffer = i7 + 1;
                    bArr3[i7] = (byte) ((c2 & '?') | 128);
                } else if (c2 < 55296 || c2 > 57343) {
                    if (this.buffer.length - this.indexInBuffer < 3) {
                        flush();
                    }
                    byte[] bArr4 = this.buffer;
                    int i8 = this.indexInBuffer;
                    int i9 = i8 + 1;
                    this.indexInBuffer = i9;
                    bArr4[i8] = (byte) ((c2 >> '\f') | 224);
                    int i10 = i9 + 1;
                    this.indexInBuffer = i10;
                    bArr4[i9] = (byte) (((c2 >> 6) & 63) | 128);
                    this.indexInBuffer = i10 + 1;
                    bArr4[i10] = (byte) ((c2 & '?') | 128);
                } else {
                    int i11 = i2 + 1;
                    char c4 = i11 < count ? string[i11] : (char) 0;
                    if (c2 <= 56319) {
                        if (56320 <= c4 && c4 < 57344) {
                            int i12 = (((c2 & 1023) << 10) | (c4 & 1023)) + 65536;
                            if (this.buffer.length - this.indexInBuffer < 4) {
                                flush();
                            }
                            byte[] bArr5 = this.buffer;
                            int i13 = this.indexInBuffer;
                            int i14 = i13 + 1;
                            this.indexInBuffer = i14;
                            bArr5[i13] = (byte) ((i12 >> 18) | 240);
                            int i15 = i14 + 1;
                            this.indexInBuffer = i15;
                            bArr5[i14] = (byte) (((i12 >> 12) & 63) | 128);
                            int i16 = i15 + 1;
                            this.indexInBuffer = i16;
                            bArr5[i15] = (byte) (((i12 >> 6) & 63) | 128);
                            this.indexInBuffer = i16 + 1;
                            bArr5[i16] = (byte) ((i12 & 63) | 128);
                            i2 += 2;
                        }
                    }
                    if (this.buffer.length - this.indexInBuffer < 1) {
                        flush();
                    }
                    byte[] bArr6 = this.buffer;
                    int i17 = this.indexInBuffer;
                    this.indexInBuffer = i17 + 1;
                    bArr6[i17] = (byte) 63;
                    i2 = i11;
                }
                i2++;
            }
        }
    }

    private final void writeUtf8CodePoint(int codePoint) throws IOException {
        if (codePoint < 128) {
            if (this.buffer.length - this.indexInBuffer < 1) {
                flush();
            }
            byte[] bArr = this.buffer;
            int i2 = this.indexInBuffer;
            this.indexInBuffer = i2 + 1;
            bArr[i2] = (byte) codePoint;
            return;
        }
        if (codePoint < 2048) {
            if (this.buffer.length - this.indexInBuffer < 2) {
                flush();
            }
            byte[] bArr2 = this.buffer;
            int i3 = this.indexInBuffer;
            int i4 = i3 + 1;
            this.indexInBuffer = i4;
            bArr2[i3] = (byte) ((codePoint >> 6) | 192);
            this.indexInBuffer = i4 + 1;
            bArr2[i4] = (byte) ((codePoint & 63) | 128);
            return;
        }
        boolean z2 = false;
        if (55296 <= codePoint && codePoint < 57344) {
            z2 = true;
        }
        if (z2) {
            if (this.buffer.length - this.indexInBuffer < 1) {
                flush();
            }
            byte[] bArr3 = this.buffer;
            int i5 = this.indexInBuffer;
            this.indexInBuffer = i5 + 1;
            bArr3[i5] = (byte) 63;
            return;
        }
        if (codePoint < 65536) {
            if (this.buffer.length - this.indexInBuffer < 3) {
                flush();
            }
            byte[] bArr4 = this.buffer;
            int i6 = this.indexInBuffer;
            int i7 = i6 + 1;
            this.indexInBuffer = i7;
            bArr4[i6] = (byte) ((codePoint >> 12) | 224);
            int i8 = i7 + 1;
            this.indexInBuffer = i8;
            bArr4[i7] = (byte) (((codePoint >> 6) & 63) | 128);
            this.indexInBuffer = i8 + 1;
            bArr4[i8] = (byte) ((codePoint & 63) | 128);
            return;
        }
        if (codePoint > 1114111) {
            throw new JsonEncodingException(a.l("Unexpected code point: ", codePoint));
        }
        if (this.buffer.length - this.indexInBuffer < 4) {
            flush();
        }
        byte[] bArr5 = this.buffer;
        int i9 = this.indexInBuffer;
        int i10 = i9 + 1;
        this.indexInBuffer = i10;
        bArr5[i9] = (byte) ((codePoint >> 18) | 240);
        int i11 = i10 + 1;
        this.indexInBuffer = i11;
        bArr5[i10] = (byte) (((codePoint >> 12) & 63) | 128);
        int i12 = i11 + 1;
        this.indexInBuffer = i12;
        bArr5[i11] = (byte) (((codePoint >> 6) & 63) | 128);
        this.indexInBuffer = i12 + 1;
        bArr5[i12] = (byte) ((codePoint & 63) | 128);
    }

    @Override // kotlinx.serialization.json.internal.JsonWriter
    public void release() throws IOException {
        flush();
        CharArrayPool.INSTANCE.release(this.charArray);
        ByteArrayPool.INSTANCE.release(this.buffer);
    }

    @Override // kotlinx.serialization.json.internal.JsonWriter
    public void write(String text) throws IOException {
        o.f(text, "text");
        int length = text.length();
        ensureTotalCapacity(0, length);
        text.getChars(0, length, this.charArray, 0);
        writeUtf8(this.charArray, length);
    }

    @Override // kotlinx.serialization.json.internal.JsonWriter
    public void writeChar(char c2) throws IOException {
        writeUtf8CodePoint(c2);
    }

    @Override // kotlinx.serialization.json.internal.JsonWriter
    public void writeLong(long value) throws IOException {
        write(String.valueOf(value));
    }

    @Override // kotlinx.serialization.json.internal.JsonWriter
    public void writeQuoted(String text) throws IOException {
        o.f(text, "text");
        ensureTotalCapacity(0, text.length() + 2);
        char[] cArr = this.charArray;
        cArr[0] = '\"';
        int length = text.length();
        text.getChars(0, length, cArr, 1);
        int i2 = length + 1;
        for (int i3 = 1; i3 < i2; i3++) {
            char c2 = cArr[i3];
            if (c2 < StringOpsKt.getESCAPE_MARKERS().length && StringOpsKt.getESCAPE_MARKERS()[c2] != 0) {
                appendStringSlowPath(i3, text);
                return;
            }
        }
        cArr[i2] = '\"';
        writeUtf8(cArr, length + 2);
        flush();
    }
}
