package com.google.android.gms.internal.pal;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes2.dex */
public final class g implements Closeable, Flushable {

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f20920f;

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

    public g(StringWriter stringWriter) {
        int[] iArr = new int[32];
        this.f20916b = iArr;
        this.f20917c = 0;
        if (iArr.length == 0) {
            this.f20916b = Arrays.copyOf(iArr, 0);
        }
        int[] iArr2 = this.f20916b;
        int i10 = this.f20917c;
        this.f20917c = i10 + 1;
        iArr2[i10] = 6;
        this.f20918d = ":";
        this.f20915a = stringWriter;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        this.f20915a.close();
        int i10 = this.f20917c;
        if (i10 > 1 || (i10 == 1 && this.f20916b[0] != 7)) {
            throw new IOException("Incomplete document");
        }
        this.f20917c = 0;
    }

    public final int e() {
        int i10 = this.f20917c;
        if (i10 != 0) {
            return this.f20916b[i10 - 1];
        }
        throw new IllegalStateException("JsonWriter is closed.");
    }

    @Override // java.io.Flushable
    public final void flush() throws IOException {
        if (this.f20917c == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        this.f20915a.flush();
    }

    public final void g(int i10, int i11, char c6) throws IOException {
        int iE = e();
        if (iE != i11 && iE != i10) {
            throw new IllegalStateException("Nesting problem.");
        }
        String str = this.f20920f;
        if (str != null) {
            throw new IllegalStateException("Dangling name: ".concat(str));
        }
        this.f20917c--;
        this.f20915a.write(c6);
    }

    public final void k() throws IOException {
        int iE = e();
        if (iE == 1) {
            this.f20916b[this.f20917c - 1] = 2;
            return;
        }
        Writer writer = this.f20915a;
        if (iE == 2) {
            writer.append(',');
            return;
        }
        if (iE == 4) {
            writer.append((CharSequence) this.f20918d);
            this.f20916b[this.f20917c - 1] = 5;
            return;
        }
        if (iE != 6) {
            if (iE != 7) {
                throw new IllegalStateException("Nesting problem.");
            }
            if (!this.f20919e) {
                throw new IllegalStateException("JSON must have only one top-level value.");
            }
        }
        this.f20916b[this.f20917c - 1] = 7;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x002d  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void m(String str) throws IOException {
        String str2;
        Writer writer = this.f20915a;
        writer.write(34);
        int length = str.length();
        int i10 = 0;
        for (int i11 = 0; i11 < length; i11++) {
            char cCharAt = str.charAt(i11);
            if (cCharAt < 128) {
                str2 = f20914h[cCharAt];
                if (str2 != null) {
                    if (i10 < i11) {
                        writer.write(str, i10, i11 - i10);
                    }
                    writer.write(str2);
                    i10 = i11 + 1;
                }
            } else {
                if (cCharAt == 8232) {
                    str2 = "\\u2028";
                } else if (cCharAt == 8233) {
                    str2 = "\\u2029";
                }
                if (i10 < i11) {
                }
                writer.write(str2);
                i10 = i11 + 1;
            }
        }
        if (i10 < length) {
            writer.write(str, i10, length - i10);
        }
        writer.write(34);
    }

    public final void p() throws IOException {
        if (this.f20920f != null) {
            int iE = e();
            if (iE == 5) {
                this.f20915a.write(44);
            } else if (iE != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            this.f20916b[this.f20917c - 1] = 4;
            m(this.f20920f);
            this.f20920f = null;
        }
    }
}
