package o;

import java.io.Closeable;
import java.io.EOFException;
import java.io.Flushable;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.gs, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C1332gs implements Closeable, Flushable {
    public static final G10 B = new G10("[a-z0-9_-]{1,120}");
    public final C1236fs A;
    public final SV l;
    public final long m;
    public final SV n;

    /* renamed from: o, reason: collision with root package name */
    public final SV f544o;
    public final SV p;
    public final LinkedHashMap q;
    public final C0168Gm r;
    public long s;
    public int t;
    public C1632k00 u;
    public boolean v;
    public boolean w;
    public boolean x;
    public boolean y;
    public boolean z;

    public C1332gs(long j, AbstractC1628jy abstractC1628jy, SV sv) {
        ExecutorC1232fq executorC1232fq = ExecutorC1232fq.m;
        this.l = sv;
        this.m = j;
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        this.n = sv.d("journal");
        this.f544o = sv.d("journal.tmp");
        this.p = sv.d("journal.bkp");
        this.q = new LinkedHashMap(0, 0.75f, true);
        this.r = C80.e(AbstractC2798wI.t(AbstractC2378rs0.t(), Rh0.m.W(1)));
        this.A = new C1236fs(abstractC1628jy);
    }

    /* JADX WARN: Removed duplicated region for block: B:58:0x0118 A[Catch: all -> 0x0035, TRY_LEAVE, TryCatch #0 {, blocks: (B:3:0x0001, B:7:0x0011, B:11:0x0018, B:13:0x0020, B:15:0x0030, B:23:0x003e, B:25:0x0056, B:29:0x0073, B:31:0x0083, B:33:0x008a, B:26:0x005c, B:28:0x006c, B:37:0x00aa, B:39:0x00b1, B:42:0x00b6, B:44:0x00c4, B:47:0x00c9, B:52:0x0104, B:54:0x010f, B:58:0x0118, B:48:0x00e1, B:50:0x00f6, B:51:0x0101, B:36:0x009a, B:61:0x011d, B:62:0x0124), top: B:65:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public static final void b(o.C1332gs r9, o.C0949cs r10, boolean r11) {
        /*
            Method dump skipped, instructions count: 295
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: o.C1332gs.b(o.gs, o.cs, boolean):void");
    }

    public static void w(String str) {
        if (B.b(str)) {
            return;
        }
        throw new IllegalArgumentException(("keys must match regex [a-z0-9_-]{1,120}: \"" + str + '\"').toString());
    }

    public final synchronized C0949cs c(String str) {
        if (this.x) {
            throw new IllegalStateException("cache is closed");
        }
        w(str);
        f();
        C1044ds c1044ds = (C1044ds) this.q.get(str);
        if ((c1044ds != null ? c1044ds.g : null) != null) {
            return null;
        }
        if (c1044ds != null && c1044ds.h != 0) {
            return null;
        }
        if (!this.y && !this.z) {
            C1632k00 c1632k00 = this.u;
            c1632k00.J("DIRTY");
            c1632k00.writeByte(32);
            c1632k00.J(str);
            c1632k00.writeByte(10);
            c1632k00.flush();
            if (this.v) {
                return null;
            }
            if (c1044ds == null) {
                c1044ds = new C1044ds(this, str);
                this.q.put(str, c1044ds);
            }
            C0949cs c0949cs = new C0949cs(this, c1044ds);
            c1044ds.g = c0949cs;
            return c0949cs;
        }
        g();
        return null;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.w && !this.x) {
                for (C1044ds c1044ds : (C1044ds[]) this.q.values().toArray(new C1044ds[0])) {
                    C0949cs c0949cs = c1044ds.g;
                    if (c0949cs != null) {
                        C1044ds c1044ds2 = (C1044ds) c0949cs.b;
                        if (F3.c(c1044ds2.g, c0949cs)) {
                            c1044ds2.f = true;
                        }
                    }
                }
                u();
                C80.n(this.r, null);
                this.u.close();
                this.u = null;
                this.x = true;
                return;
            }
            this.x = true;
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized C1140es d(String str) {
        C1140es c1140esA;
        if (this.x) {
            throw new IllegalStateException("cache is closed");
        }
        w(str);
        f();
        C1044ds c1044ds = (C1044ds) this.q.get(str);
        if (c1044ds != null && (c1140esA = c1044ds.a()) != null) {
            boolean z = true;
            this.t++;
            C1632k00 c1632k00 = this.u;
            c1632k00.J("READ");
            c1632k00.writeByte(32);
            c1632k00.J(str);
            c1632k00.writeByte(10);
            if (this.t < 2000) {
                z = false;
            }
            if (z) {
                g();
            }
            return c1140esA;
        }
        return null;
    }

    public final synchronized void f() {
        try {
            if (this.w) {
                return;
            }
            this.A.d(this.f544o);
            if (this.A.e(this.p)) {
                if (this.A.e(this.n)) {
                    this.A.d(this.p);
                } else {
                    this.A.b(this.p, this.n);
                }
            }
            if (this.A.e(this.n)) {
                try {
                    l();
                    j();
                    this.w = true;
                    return;
                } catch (IOException unused) {
                    try {
                        close();
                        As0.t(this.A, this.l);
                        this.x = false;
                    } catch (Throwable th) {
                        this.x = false;
                        throw th;
                    }
                }
            }
            y();
            this.w = true;
        } catch (Throwable th2) {
            throw th2;
        }
    }

    @Override // java.io.Flushable
    public final synchronized void flush() {
        if (this.w) {
            if (this.x) {
                throw new IllegalStateException("cache is closed");
            }
            u();
            this.u.flush();
        }
    }

    public final void g() {
        AbstractC1692kh0.C(this.r, null, new C0684a4(this, null, 1), 3);
    }

    public final void j() {
        Iterator it = this.q.values().iterator();
        long j = 0;
        while (it.hasNext()) {
            C1044ds c1044ds = (C1044ds) it.next();
            int i = 0;
            if (c1044ds.g == null) {
                while (i < 2) {
                    j += c1044ds.b[i];
                    i++;
                }
            } else {
                c1044ds.g = null;
                while (i < 2) {
                    SV sv = (SV) c1044ds.c.get(i);
                    C1236fs c1236fs = this.A;
                    c1236fs.d(sv);
                    c1236fs.d((SV) c1044ds.d.get(i));
                    i++;
                }
                it.remove();
            }
        }
        this.s = j;
    }

    public final void l() throws Throwable {
        C1236fs c1236fs = this.A;
        AbstractC1628jy abstractC1628jy = c1236fs.b;
        SV sv = this.n;
        C1822m00 c1822m00 = new C1822m00(abstractC1628jy.k(sv));
        try {
            String strA = c1822m00.A(Long.MAX_VALUE);
            String strA2 = c1822m00.A(Long.MAX_VALUE);
            String strA3 = c1822m00.A(Long.MAX_VALUE);
            String strA4 = c1822m00.A(Long.MAX_VALUE);
            String strA5 = c1822m00.A(Long.MAX_VALUE);
            if (!"libcore.io.DiskLruCache".equals(strA) || !"1".equals(strA2) || !F3.c(String.valueOf(1), strA3) || !F3.c(String.valueOf(2), strA4) || strA5.length() > 0) {
                throw new IOException("unexpected journal header: [" + strA + ", " + strA2 + ", " + strA3 + ", " + strA4 + ", " + strA5 + ']');
            }
            int i = 0;
            while (true) {
                try {
                    m(c1822m00.A(Long.MAX_VALUE));
                    i++;
                } catch (EOFException unused) {
                    this.t = i - this.q.size();
                    if (c1822m00.b()) {
                        this.u = new C1632k00(new C0231Ix(c1236fs.b.a(sv), new C2769w(15, this)));
                    } else {
                        y();
                    }
                    try {
                        c1822m00.close();
                        th = null;
                    } catch (Throwable th) {
                        th = th;
                    }
                    if (th != null) {
                        throw th;
                    }
                    return;
                }
            }
        } catch (Throwable th2) {
            th = th2;
            try {
                c1822m00.close();
            } catch (Throwable th3) {
                C80.i(th, th3);
            }
        }
    }

    public final void m(String str) throws IOException {
        String strSubstring;
        int iV = Za0.V(str, ' ', 0, 6);
        if (iV == -1) {
            throw new IOException(AbstractC2462sn.u("unexpected journal line: ", str));
        }
        int i = iV + 1;
        int iV2 = Za0.V(str, ' ', i, 4);
        LinkedHashMap linkedHashMap = this.q;
        if (iV2 == -1) {
            strSubstring = str.substring(i);
            if (iV == 6 && AbstractC1299gb0.L(str, "REMOVE", false)) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iV2);
        }
        Object c1044ds = linkedHashMap.get(strSubstring);
        if (c1044ds == null) {
            c1044ds = new C1044ds(this, strSubstring);
            linkedHashMap.put(strSubstring, c1044ds);
        }
        C1044ds c1044ds2 = (C1044ds) c1044ds;
        if (iV2 == -1 || iV != 5 || !AbstractC1299gb0.L(str, "CLEAN", false)) {
            if (iV2 == -1 && iV == 5 && AbstractC1299gb0.L(str, "DIRTY", false)) {
                c1044ds2.g = new C0949cs(this, c1044ds2);
                return;
            } else {
                if (iV2 != -1 || iV != 4 || !AbstractC1299gb0.L(str, "READ", false)) {
                    throw new IOException(AbstractC2462sn.u("unexpected journal line: ", str));
                }
                return;
            }
        }
        List listK0 = Za0.k0(str.substring(iV2 + 1), new char[]{' '});
        c1044ds2.e = true;
        c1044ds2.g = null;
        if (listK0.size() != 2) {
            throw new IOException("unexpected journal line: " + listK0);
        }
        try {
            int size = listK0.size();
            for (int i2 = 0; i2 < size; i2++) {
                c1044ds2.b[i2] = Long.parseLong((String) listK0.get(i2));
            }
        } catch (NumberFormatException unused) {
            throw new IOException("unexpected journal line: " + listK0);
        }
    }

    public final void q(C1044ds c1044ds) {
        C1632k00 c1632k00;
        int i = c1044ds.h;
        String str = c1044ds.a;
        if (i > 0 && (c1632k00 = this.u) != null) {
            c1632k00.J("DIRTY");
            c1632k00.writeByte(32);
            c1632k00.J(str);
            c1632k00.writeByte(10);
            c1632k00.flush();
        }
        if (c1044ds.h > 0 || c1044ds.g != null) {
            c1044ds.f = true;
            return;
        }
        for (int i2 = 0; i2 < 2; i2++) {
            this.A.d((SV) c1044ds.c.get(i2));
            long j = this.s;
            long[] jArr = c1044ds.b;
            this.s = j - jArr[i2];
            jArr[i2] = 0;
        }
        this.t++;
        C1632k00 c1632k002 = this.u;
        if (c1632k002 != null) {
            c1632k002.J("REMOVE");
            c1632k002.writeByte(32);
            c1632k002.J(str);
            c1632k002.writeByte(10);
        }
        this.q.remove(str);
        if (this.t >= 2000) {
            g();
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:9:0x0022, code lost:
    
        q(r1);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void u() {
        /*
            r4 = this;
        L0:
            long r0 = r4.s
            long r2 = r4.m
            int r0 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            if (r0 <= 0) goto L27
            java.util.LinkedHashMap r0 = r4.q
            java.util.Collection r0 = r0.values()
            java.util.Iterator r0 = r0.iterator()
        L12:
            boolean r1 = r0.hasNext()
            if (r1 == 0) goto L26
            java.lang.Object r1 = r0.next()
            o.ds r1 = (o.C1044ds) r1
            boolean r2 = r1.f
            if (r2 != 0) goto L12
            r4.q(r1)
            goto L0
        L26:
            return
        L27:
            r0 = 0
            r4.y = r0
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: o.C1332gs.u():void");
    }

    public final synchronized void y() {
        Throwable th;
        try {
            C1632k00 c1632k00 = this.u;
            if (c1632k00 != null) {
                c1632k00.close();
            }
            C1632k00 c1632k002 = new C1632k00(this.A.j(this.f544o));
            try {
                c1632k002.J("libcore.io.DiskLruCache");
                c1632k002.writeByte(10);
                c1632k002.J("1");
                c1632k002.writeByte(10);
                c1632k002.K(1);
                c1632k002.writeByte(10);
                c1632k002.K(2);
                c1632k002.writeByte(10);
                c1632k002.writeByte(10);
                for (C1044ds c1044ds : this.q.values()) {
                    if (c1044ds.g != null) {
                        c1632k002.J("DIRTY");
                        c1632k002.writeByte(32);
                        c1632k002.J(c1044ds.a);
                        c1632k002.writeByte(10);
                    } else {
                        c1632k002.J("CLEAN");
                        c1632k002.writeByte(32);
                        c1632k002.J(c1044ds.a);
                        for (long j : c1044ds.b) {
                            c1632k002.writeByte(32);
                            c1632k002.K(j);
                        }
                        c1632k002.writeByte(10);
                    }
                }
                try {
                    c1632k002.close();
                    th = null;
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Throwable th3) {
                try {
                    c1632k002.close();
                } catch (Throwable th4) {
                    C80.i(th3, th4);
                }
                th = th3;
            }
            if (th != null) {
                throw th;
            }
            if (this.A.e(this.n)) {
                this.A.b(this.n, this.p);
                this.A.b(this.f544o, this.n);
                this.A.d(this.p);
            } else {
                this.A.b(this.f544o, this.n);
            }
            this.u = new C1632k00(new C0231Ix(this.A.b.a(this.n), new C2769w(15, this)));
            this.t = 0;
            this.v = false;
            this.z = false;
        } catch (Throwable th5) {
            throw th5;
        }
    }
}
