package i;

import androidx.exifinterface.media.ExifInterface;
import d.AbstractC0421a;
import d.C0424d;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;
import l.AbstractC0613e;

/* JADX INFO: renamed from: i.I, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0516I extends d0 {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    protected final b0 f4222j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private int f4224l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private String f4225m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private String f4226n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private DateFormat f4227o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    protected IdentityHashMap f4228p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    protected Z f4229q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    protected TimeZone f4230r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    protected Locale f4231s;

    public C0516I(e0 e0Var) {
        this(e0Var, b0.g());
    }

    public final void A(Object obj, Object obj2, Type type, int i2) {
        try {
            if (obj == null) {
                this.f4223k.K();
            } else {
                m(obj.getClass()).a(this, obj, obj2, type, i2);
            }
        } catch (IOException e2) {
            throw new C0424d(e2.getMessage(), e2);
        }
    }

    public final void B(Object obj, String str) {
        if (obj instanceof Date) {
            if ("unixtime".equals(str)) {
                this.f4223k.G((int) (((Date) obj).getTime() / 1000));
                return;
            }
            if ("millis".equals(str)) {
                this.f4223k.I(((Date) obj).getTime());
                return;
            }
            DateFormat dateFormatK = k();
            if (dateFormatK == null) {
                try {
                    dateFormatK = new SimpleDateFormat(str, this.f4231s);
                } catch (IllegalArgumentException unused) {
                    dateFormatK = new SimpleDateFormat(str.replaceAll(ExifInterface.GPS_DIRECTION_TRUE, "'T'"), this.f4231s);
                }
                dateFormatK.setTimeZone(this.f4230r);
            }
            this.f4223k.O(dateFormatK.format((Date) obj));
            return;
        }
        if (!(obj instanceof byte[])) {
            if (!(obj instanceof Collection)) {
                v(obj);
                return;
            }
            Collection collection = (Collection) obj;
            Iterator it = collection.iterator();
            this.f4223k.write(91);
            for (int i2 = 0; i2 < collection.size(); i2++) {
                Object next = it.next();
                if (i2 != 0) {
                    this.f4223k.write(44);
                }
                B(next, str);
            }
            this.f4223k.write(93);
            return;
        }
        byte[] bArr = (byte[]) obj;
        if (!"gzip".equals(str) && !"gzip,base64".equals(str)) {
            if ("hex".equals(str)) {
                this.f4223k.F(bArr);
                return;
            } else {
                this.f4223k.m(bArr);
                return;
            }
        }
        GZIPOutputStream gZIPOutputStream = null;
        try {
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                gZIPOutputStream = bArr.length < 512 ? new GZIPOutputStream(byteArrayOutputStream, bArr.length) : new GZIPOutputStream(byteArrayOutputStream);
                gZIPOutputStream.write(bArr);
                gZIPOutputStream.finish();
                this.f4223k.m(byteArrayOutputStream.toByteArray());
                AbstractC0613e.a(gZIPOutputStream);
            } catch (IOException e2) {
                throw new C0424d("write gzipBytes error", e2);
            }
        } catch (Throwable th) {
            AbstractC0613e.a(gZIPOutputStream);
            throw th;
        }
    }

    public void h(f0 f0Var, boolean z2) {
        this.f4223k.f(f0Var, z2);
    }

    public boolean i(Object obj) {
        Z z2;
        IdentityHashMap identityHashMap = this.f4228p;
        if (identityHashMap == null || (z2 = (Z) identityHashMap.get(obj)) == null || obj == Collections.emptyMap()) {
            return false;
        }
        Object obj2 = z2.f4271c;
        return obj2 == null || (obj2 instanceof Integer) || (obj2 instanceof String);
    }

    public void j() {
        this.f4224l--;
    }

    public DateFormat k() {
        if (this.f4227o == null && this.f4226n != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(this.f4226n, this.f4231s);
            this.f4227o = simpleDateFormat;
            simpleDateFormat.setTimeZone(this.f4230r);
        }
        return this.f4227o;
    }

    public String l() {
        DateFormat dateFormat = this.f4227o;
        return dateFormat instanceof SimpleDateFormat ? ((SimpleDateFormat) dateFormat).toPattern() : this.f4226n;
    }

    public U m(Class cls) {
        return this.f4222j.h(cls);
    }

    public e0 n() {
        return this.f4223k;
    }

    public void o() {
        this.f4224l++;
    }

    public boolean p(f0 f0Var) {
        return this.f4223k.j(f0Var);
    }

    public final boolean q(Type type, Object obj) {
        Z z2;
        return this.f4223k.j(f0.WriteClassName) && !(type == null && this.f4223k.j(f0.NotWriteRootClassName) && ((z2 = this.f4229q) == null || z2.f4269a == null));
    }

    public void r() {
        this.f4223k.write(10);
        for (int i2 = 0; i2 < this.f4224l; i2++) {
            this.f4223k.write(this.f4225m);
        }
    }

    public void s(Z z2, Object obj, Object obj2, int i2) {
        t(z2, obj, obj2, i2, 0);
    }

    public void t(Z z2, Object obj, Object obj2, int i2, int i3) {
        if (this.f4223k.f4343L) {
            return;
        }
        this.f4229q = new Z(z2, obj, obj2, i2, i3);
        if (this.f4228p == null) {
            this.f4228p = new IdentityHashMap();
        }
        this.f4228p.put(obj, this.f4229q);
    }

    public String toString() {
        return this.f4223k.toString();
    }

    public void u(String str) {
        this.f4226n = str;
        if (this.f4227o != null) {
            this.f4227o = null;
        }
    }

    public final void v(Object obj) {
        if (obj == null) {
            this.f4223k.K();
            return;
        }
        try {
            m(obj.getClass()).a(this, obj, null, null, 0);
        } catch (IOException e2) {
            throw new C0424d(e2.getMessage(), e2);
        }
    }

    public final void w(String str) {
        g0.f4362a.f(this, str);
    }

    public void x() {
        this.f4223k.K();
    }

    public void y(Object obj) {
        Z z2 = this.f4229q;
        if (obj == z2.f4270b) {
            this.f4223k.write("{\"$ref\":\"@\"}");
            return;
        }
        Z z3 = z2.f4269a;
        if (z3 != null && obj == z3.f4270b) {
            this.f4223k.write("{\"$ref\":\"..\"}");
            return;
        }
        while (true) {
            Z z4 = z2.f4269a;
            if (z4 == null) {
                break;
            } else {
                z2 = z4;
            }
        }
        if (obj == z2.f4270b) {
            this.f4223k.write("{\"$ref\":\"$\"}");
            return;
        }
        this.f4223k.write("{\"$ref\":\"");
        this.f4223k.write(((Z) this.f4228p.get(obj)).toString());
        this.f4223k.write("\"}");
    }

    public final void z(Object obj, Object obj2) {
        A(obj, obj2, null, 0);
    }

    public C0516I(e0 e0Var, b0 b0Var) {
        this.f4224l = 0;
        this.f4225m = "\t";
        this.f4228p = null;
        this.f4230r = AbstractC0421a.f3673b;
        this.f4231s = AbstractC0421a.f3674g;
        this.f4223k = e0Var;
        this.f4222j = b0Var;
    }
}
