package q3;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public class c implements Closeable, Flushable {

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final Pattern f9578s = Pattern.compile("-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?");

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public static final String[] f9579t = new String[128];

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final String[] f9580u;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final Writer f9581k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int[] f9582l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f9583m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final String f9584n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public boolean f9585o;
    public boolean p;
    public String q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public boolean f9586r;

    static {
        for (int i = 0; i <= 31; i++) {
            f9579t[i] = String.format("\\u%04x", Integer.valueOf(i));
        }
        String[] strArr = f9579t;
        strArr[34] = "\\\"";
        strArr[92] = "\\\\";
        strArr[9] = "\\t";
        strArr[8] = "\\b";
        strArr[10] = "\\n";
        strArr[13] = "\\r";
        strArr[12] = "\\f";
        String[] strArr2 = (String[]) strArr.clone();
        f9580u = strArr2;
        strArr2[60] = "\\u003c";
        strArr2[62] = "\\u003e";
        strArr2[38] = "\\u0026";
        strArr2[61] = "\\u003d";
        strArr2[39] = "\\u0027";
    }

    public c(Writer writer) {
        int[] iArr = new int[32];
        this.f9582l = iArr;
        this.f9583m = 0;
        if (iArr.length == 0) {
            this.f9582l = Arrays.copyOf(iArr, 0);
        }
        int[] iArr2 = this.f9582l;
        int i = this.f9583m;
        this.f9583m = i + 1;
        iArr2[i] = 6;
        this.f9584n = ":";
        this.f9586r = true;
        Objects.requireNonNull(writer, "out == null");
        this.f9581k = writer;
    }

    public final void A() throws IOException {
        if (this.q != null) {
            int iR = r();
            if (iR == 5) {
                this.f9581k.write(44);
            } else if (iR != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            n();
            this.f9582l[this.f9583m - 1] = 4;
            t(this.q);
            this.q = null;
        }
    }

    public final void a() throws IOException {
        int iR = r();
        if (iR == 1) {
            this.f9582l[this.f9583m - 1] = 2;
            n();
            return;
        }
        Writer writer = this.f9581k;
        if (iR == 2) {
            writer.append(',');
            n();
        } else {
            if (iR == 4) {
                writer.append((CharSequence) this.f9584n);
                this.f9582l[this.f9583m - 1] = 5;
                return;
            }
            if (iR != 6) {
                if (iR != 7) {
                    throw new IllegalStateException("Nesting problem.");
                }
                if (!this.f9585o) {
                    throw new IllegalStateException("JSON must have only one top-level value.");
                }
            }
            this.f9582l[this.f9583m - 1] = 7;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f9581k.close();
        int i = this.f9583m;
        if (i > 1 || (i == 1 && this.f9582l[i - 1] != 7)) {
            throw new IOException("Incomplete document");
        }
        this.f9583m = 0;
    }

    public void e() throws IOException {
        A();
        a();
        int i = this.f9583m;
        int[] iArr = this.f9582l;
        if (i == iArr.length) {
            this.f9582l = Arrays.copyOf(iArr, i * 2);
        }
        int[] iArr2 = this.f9582l;
        int i5 = this.f9583m;
        this.f9583m = i5 + 1;
        iArr2[i5] = 1;
        this.f9581k.write(91);
    }

    public void f() throws IOException {
        A();
        a();
        int i = this.f9583m;
        int[] iArr = this.f9582l;
        if (i == iArr.length) {
            this.f9582l = Arrays.copyOf(iArr, i * 2);
        }
        int[] iArr2 = this.f9582l;
        int i5 = this.f9583m;
        this.f9583m = i5 + 1;
        iArr2[i5] = 3;
        this.f9581k.write(123);
    }

    public void flush() throws IOException {
        if (this.f9583m == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        this.f9581k.flush();
    }

    public final void h(int i, int i5, char c6) throws IOException {
        int iR = r();
        if (iR != i5 && iR != i) {
            throw new IllegalStateException("Nesting problem.");
        }
        if (this.q != null) {
            throw new IllegalStateException("Dangling name: " + this.q);
        }
        this.f9583m--;
        if (iR == i5) {
            n();
        }
        this.f9581k.write(c6);
    }

    public void k() throws IOException {
        h(1, 2, ']');
    }

    public void l() throws IOException {
        h(3, 5, '}');
    }

    public void m(String str) {
        Objects.requireNonNull(str, "name == null");
        if (this.q != null) {
            throw new IllegalStateException();
        }
        if (this.f9583m == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        this.q = str;
    }

    public c o() throws IOException {
        if (this.q != null) {
            if (!this.f9586r) {
                this.q = null;
                return this;
            }
            A();
        }
        a();
        this.f9581k.write("null");
        return this;
    }

    public final int r() {
        int i = this.f9583m;
        if (i != 0) {
            return this.f9582l[i - 1];
        }
        throw new IllegalStateException("JsonWriter is closed.");
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0034  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final void t(java.lang.String r9) throws java.io.IOException {
        /*
            r8 = this;
            boolean r0 = r8.p
            if (r0 == 0) goto L7
            java.lang.String[] r0 = q3.c.f9580u
            goto L9
        L7:
            java.lang.String[] r0 = q3.c.f9579t
        L9:
            java.io.Writer r1 = r8.f9581k
            r2 = 34
            r1.write(r2)
            int r3 = r9.length()
            r4 = 0
            r5 = r4
        L16:
            if (r4 >= r3) goto L41
            char r6 = r9.charAt(r4)
            r7 = 128(0x80, float:1.8E-43)
            if (r6 >= r7) goto L25
            r6 = r0[r6]
            if (r6 != 0) goto L32
            goto L3e
        L25:
            r7 = 8232(0x2028, float:1.1535E-41)
            if (r6 != r7) goto L2c
            java.lang.String r6 = "\\u2028"
            goto L32
        L2c:
            r7 = 8233(0x2029, float:1.1537E-41)
            if (r6 != r7) goto L3e
            java.lang.String r6 = "\\u2029"
        L32:
            if (r5 >= r4) goto L39
            int r7 = r4 - r5
            r1.write(r9, r5, r7)
        L39:
            r1.write(r6)
            int r5 = r4 + 1
        L3e:
            int r4 = r4 + 1
            goto L16
        L41:
            if (r5 >= r3) goto L47
            int r3 = r3 - r5
            r1.write(r9, r5, r3)
        L47:
            r1.write(r2)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: q3.c.t(java.lang.String):void");
    }

    public void u(double d3) throws IOException {
        A();
        if (this.f9585o || !(Double.isNaN(d3) || Double.isInfinite(d3))) {
            a();
            this.f9581k.append((CharSequence) Double.toString(d3));
        } else {
            throw new IllegalArgumentException("Numeric values must be finite, but was " + d3);
        }
    }

    public void v(long j5) throws IOException {
        A();
        a();
        this.f9581k.write(Long.toString(j5));
    }

    public void w(Boolean bool) throws IOException {
        if (bool == null) {
            o();
            return;
        }
        A();
        a();
        this.f9581k.write(bool.booleanValue() ? "true" : "false");
    }

    public void x(Number number) throws IOException {
        if (number == null) {
            o();
            return;
        }
        A();
        String string = number.toString();
        if (!string.equals("-Infinity") && !string.equals("Infinity") && !string.equals("NaN")) {
            Class<?> cls = number.getClass();
            if (cls != Integer.class && cls != Long.class && cls != Double.class && cls != Float.class && cls != Byte.class && cls != Short.class && cls != BigDecimal.class && cls != BigInteger.class && cls != AtomicInteger.class && cls != AtomicLong.class && !f9578s.matcher(string).matches()) {
                throw new IllegalArgumentException("String created by " + cls + " is not a valid JSON number: " + string);
            }
        } else if (!this.f9585o) {
            throw new IllegalArgumentException("Numeric values must be finite, but was ".concat(string));
        }
        a();
        this.f9581k.append((CharSequence) string);
    }

    public void y(String str) throws IOException {
        if (str == null) {
            o();
            return;
        }
        A();
        a();
        t(str);
    }

    public void z(boolean z5) throws IOException {
        A();
        a();
        this.f9581k.write(z5 ? "true" : "false");
    }

    public final void n() {
    }
}
