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

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

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

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final boolean f8605g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public zzhyq f8606h;

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

    static {
        for (int i10 = 0; i10 <= 31; i10++) {
            f8598k[i10] = String.format("\\u%04x", Integer.valueOf(i10));
        }
        String[] strArr = f8598k;
        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 aa2(androidx.fragment.app.b2 b2Var) {
        int[] iArr = new int[32];
        this.f8600b = iArr;
        boolean z10 = false;
        this.f8601c = 0;
        if (iArr.length == 0) {
            this.f8600b = Arrays.copyOf(iArr, 0);
        }
        int[] iArr2 = this.f8600b;
        int i10 = this.f8601c;
        this.f8601c = i10 + 1;
        iArr2[i10] = 6;
        this.f8606h = zzhyq.f19852b;
        this.f8599a = b2Var;
        k92 k92Var = k92.f13184d;
        Objects.requireNonNull(k92Var);
        this.f8602d = k92Var;
        this.f8604f = ",";
        boolean z11 = k92Var.f13187c;
        String str = k92Var.f13185a;
        if (z11) {
            this.f8603e = ": ";
            if (str.isEmpty()) {
                this.f8604f = ", ";
            }
        } else {
            this.f8603e = ":";
        }
        if (str.isEmpty() && k92Var.f13186b.isEmpty()) {
            z10 = true;
        }
        this.f8605g = z10;
    }

    public final void E() throws IOException {
        int iK = k();
        if (iK == 1) {
            this.f8600b[this.f8601c - 1] = 2;
            x();
            return;
        }
        Writer writer = this.f8599a;
        if (iK == 2) {
            writer.append((CharSequence) this.f8604f);
            x();
        } else {
            if (iK == 4) {
                writer.append((CharSequence) this.f8603e);
                this.f8600b[this.f8601c - 1] = 5;
                return;
            }
            if (iK != 6) {
                if (iK != 7) {
                    throw new IllegalStateException("Nesting problem.");
                }
                if (this.f8606h != zzhyq.f19851a) {
                    throw new IllegalStateException("JSON must have only one top-level value.");
                }
            }
            this.f8600b[this.f8601c - 1] = 7;
        }
    }

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

    public final void e() throws IOException {
        if (this.f8607i != null) {
            m();
        }
        E();
        this.f8599a.write("null");
    }

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

    public final void g(int i10, int i11, char c6) throws IOException {
        int iK = k();
        if (iK != i11 && iK != i10) {
            throw new IllegalStateException("Nesting problem.");
        }
        String str = this.f8607i;
        if (str != null) {
            throw new IllegalStateException("Dangling name: ".concat(str));
        }
        this.f8601c--;
        if (iK == i11) {
            x();
        }
        this.f8599a.write(c6);
    }

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

    public final void m() throws IOException {
        if (this.f8607i != null) {
            int iK = k();
            if (iK == 5) {
                this.f8599a.write(this.f8604f);
            } else if (iK != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            x();
            this.f8600b[this.f8601c - 1] = 4;
            p(this.f8607i);
            this.f8607i = null;
        }
    }

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

    public final void x() throws IOException {
        if (this.f8605g) {
            return;
        }
        k92 k92Var = this.f8602d;
        String str = k92Var.f13185a;
        Writer writer = this.f8599a;
        writer.write(str);
        int i10 = this.f8601c;
        for (int i11 = 1; i11 < i10; i11++) {
            writer.write(k92Var.f13186b);
        }
    }
}
