package h2;

import B0.C0016c;
import X6.n;
import Z6.AbstractC0385z;
import com.google.android.gms.internal.measurement.AbstractC0707u1;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.logging.Logger;
import s7.l;
import x7.A;
import x7.B;
import x7.C1726b;
import x7.J;
import x7.t;
import x7.v;
import x7.x;

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

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public static final X6.d f13373G = new X6.d("[a-z0-9_-]{1,120}");

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

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

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public boolean f13376C;
    public boolean D;

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

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final x f13379q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final long f13380r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final x f13381s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final x f13382t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final x f13383u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final LinkedHashMap f13384v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final e7.c f13385w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public long f13386x;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public A f13388z;

    public f(long j8, g7.d dVar, t tVar, x xVar) {
        this.f13379q = xVar;
        this.f13380r = j8;
        if (j8 <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        this.f13381s = xVar.e("journal");
        this.f13382t = xVar.e("journal.tmp");
        this.f13383u = xVar.e("journal.bkp");
        this.f13384v = new LinkedHashMap(0, 0.75f, true);
        this.f13385w = AbstractC0385z.b(l.x(AbstractC0385z.c(), dVar.l0(1)));
        this.f13378F = new d(tVar);
    }

    public static void O(String str) {
        X6.d dVar = f13373G;
        dVar.getClass();
        P6.g.e(str, "input");
        if (dVar.f7248q.matcher(str).matches()) {
            return;
        }
        throw new IllegalArgumentException(("keys must match regex [a-z0-9_-]{1,120}: \"" + str + '\"').toString());
    }

    /* JADX WARN: Removed duplicated region for block: B:58:0x011b 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:0x00c7, B:47:0x00cc, B:52:0x0107, B:54:0x0112, B:58:0x011b, B:48:0x00e4, B:50:0x00f9, B:51:0x0104, B:36:0x009a, B:61:0x0120, B:62:0x0127), top: B:65:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void a(f fVar, C0016c c0016c, boolean z8) {
        synchronized (fVar) {
            C1029b c1029b = (C1029b) c0016c.f718r;
            if (!P6.g.a(c1029b.f13366g, c0016c)) {
                throw new IllegalStateException("Check failed.");
            }
            if (!z8 || c1029b.f13365f) {
                for (int i = 0; i < 2; i++) {
                    fVar.f13378F.d((x) c1029b.f13363d.get(i));
                }
            } else {
                for (int i5 = 0; i5 < 2; i5++) {
                    if (((boolean[]) c0016c.f719s)[i5] && !fVar.f13378F.e((x) c1029b.f13363d.get(i5))) {
                        c0016c.a(false);
                        return;
                    }
                }
                for (int i8 = 0; i8 < 2; i8++) {
                    x xVar = (x) c1029b.f13363d.get(i8);
                    x xVar2 = (x) c1029b.f13362c.get(i8);
                    if (fVar.f13378F.e(xVar)) {
                        fVar.f13378F.l(xVar, xVar2);
                    } else {
                        d dVar = fVar.f13378F;
                        x xVar3 = (x) c1029b.f13362c.get(i8);
                        if (!dVar.e(xVar3)) {
                            u2.f.a(dVar.j(xVar3));
                        }
                    }
                    long j8 = c1029b.f13361b[i8];
                    Long l8 = (Long) fVar.f13378F.g(xVar2).f12859e;
                    long jLongValue = l8 != null ? l8.longValue() : 0L;
                    c1029b.f13361b[i8] = jLongValue;
                    fVar.f13386x = (fVar.f13386x - j8) + jLongValue;
                }
            }
            c1029b.f13366g = null;
            if (c1029b.f13365f) {
                fVar.H(c1029b);
                return;
            }
            fVar.f13387y++;
            A a3 = fVar.f13388z;
            P6.g.b(a3);
            if (z8 || c1029b.f13364e) {
                c1029b.f13364e = true;
                a3.Y("CLEAN");
                a3.d0(32);
                a3.Y(c1029b.f13360a);
                for (long j9 : c1029b.f13361b) {
                    a3.d0(32);
                    a3.a0(j9);
                }
                a3.d0(10);
            } else {
                fVar.f13384v.remove(c1029b.f13360a);
                a3.Y("REMOVE");
                a3.d0(32);
                a3.Y(c1029b.f13360a);
                a3.d0(10);
            }
            a3.flush();
            if (fVar.f13386x > fVar.f13380r) {
                fVar.m();
            } else if (fVar.f13387y >= 2000) {
            }
        }
    }

    public final void A(String str) throws IOException {
        String strSubstring;
        int iS = X6.e.S(str, ' ', 0, 6);
        if (iS == -1) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
        int i = iS + 1;
        int iS2 = X6.e.S(str, ' ', i, 4);
        LinkedHashMap linkedHashMap = this.f13384v;
        if (iS2 == -1) {
            strSubstring = str.substring(i);
            P6.g.d(strSubstring, "substring(...)");
            if (iS == 6 && n.M(str, "REMOVE", false)) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iS2);
            P6.g.d(strSubstring, "substring(...)");
        }
        Object c1029b = linkedHashMap.get(strSubstring);
        if (c1029b == null) {
            c1029b = new C1029b(this, strSubstring);
            linkedHashMap.put(strSubstring, c1029b);
        }
        C1029b c1029b2 = (C1029b) c1029b;
        if (iS2 == -1 || iS != 5 || !n.M(str, "CLEAN", false)) {
            if (iS2 == -1 && iS == 5 && n.M(str, "DIRTY", false)) {
                c1029b2.f13366g = new C0016c(this, c1029b2);
                return;
            } else {
                if (iS2 != -1 || iS != 4 || !n.M(str, "READ", false)) {
                    throw new IOException("unexpected journal line: ".concat(str));
                }
                return;
            }
        }
        String strSubstring2 = str.substring(iS2 + 1);
        P6.g.d(strSubstring2, "substring(...)");
        List listC0 = X6.e.c0(strSubstring2, new char[]{' '});
        c1029b2.f13364e = true;
        c1029b2.f13366g = null;
        int size = listC0.size();
        c1029b2.i.getClass();
        if (size != 2) {
            throw new IOException("unexpected journal line: " + listC0);
        }
        try {
            int size2 = listC0.size();
            for (int i5 = 0; i5 < size2; i5++) {
                c1029b2.f13361b[i5] = Long.parseLong((String) listC0.get(i5));
            }
        } catch (NumberFormatException unused) {
            throw new IOException("unexpected journal line: " + listC0);
        }
    }

    public final void H(C1029b c1029b) {
        A a3;
        int i = c1029b.f13367h;
        String str = c1029b.f13360a;
        if (i > 0 && (a3 = this.f13388z) != null) {
            a3.Y("DIRTY");
            a3.d0(32);
            a3.Y(str);
            a3.d0(10);
            a3.flush();
        }
        if (c1029b.f13367h > 0 || c1029b.f13366g != null) {
            c1029b.f13365f = true;
            return;
        }
        for (int i5 = 0; i5 < 2; i5++) {
            this.f13378F.d((x) c1029b.f13362c.get(i5));
            long j8 = this.f13386x;
            long[] jArr = c1029b.f13361b;
            this.f13386x = j8 - jArr[i5];
            jArr[i5] = 0;
        }
        this.f13387y++;
        A a8 = this.f13388z;
        if (a8 != null) {
            a8.Y("REMOVE");
            a8.d0(32);
            a8.Y(str);
            a8.d0(10);
        }
        this.f13384v.remove(str);
        if (this.f13387y >= 2000) {
            m();
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:9:0x0022, code lost:
    
        H(r1);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void K() {
        while (this.f13386x > this.f13380r) {
            for (C1029b c1029b : this.f13384v.values()) {
                if (!c1029b.f13365f) {
                    break;
                }
            }
            return;
        }
        this.D = false;
    }

    public final synchronized void Q() {
        Throwable th;
        try {
            A a3 = this.f13388z;
            if (a3 != null) {
                a3.close();
            }
            A aB = l.b(this.f13378F.j(this.f13382t));
            try {
                aB.Y("libcore.io.DiskLruCache");
                aB.d0(10);
                aB.Y("1");
                aB.d0(10);
                aB.a0(1);
                aB.d0(10);
                aB.a0(2);
                aB.d0(10);
                aB.d0(10);
                for (C1029b c1029b : this.f13384v.values()) {
                    if (c1029b.f13366g != null) {
                        aB.Y("DIRTY");
                        aB.d0(32);
                        aB.Y(c1029b.f13360a);
                        aB.d0(10);
                    } else {
                        aB.Y("CLEAN");
                        aB.d0(32);
                        aB.Y(c1029b.f13360a);
                        for (long j8 : c1029b.f13361b) {
                            aB.d0(32);
                            aB.a0(j8);
                        }
                        aB.d0(10);
                    }
                }
                try {
                    aB.close();
                    th = null;
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Throwable th3) {
                try {
                    aB.close();
                } catch (Throwable th4) {
                    android.support.v4.media.session.a.a(th3, th4);
                }
                th = th3;
            }
            if (th != null) {
                throw th;
            }
            if (this.f13378F.e(this.f13381s)) {
                this.f13378F.l(this.f13381s, this.f13383u);
                this.f13378F.l(this.f13382t, this.f13381s);
                this.f13378F.d(this.f13383u);
            } else {
                this.f13378F.l(this.f13382t, this.f13381s);
            }
            this.f13388z = n();
            this.f13387y = 0;
            this.f13374A = false;
            this.f13377E = false;
        } catch (Throwable th5) {
            throw th5;
        }
    }

    public final synchronized C0016c b(String str) {
        try {
            if (this.f13376C) {
                throw new IllegalStateException("cache is closed");
            }
            O(str);
            j();
            C1029b c1029b = (C1029b) this.f13384v.get(str);
            if ((c1029b != null ? c1029b.f13366g : null) != null) {
                return null;
            }
            if (c1029b != null && c1029b.f13367h != 0) {
                return null;
            }
            if (!this.D && !this.f13377E) {
                A a3 = this.f13388z;
                P6.g.b(a3);
                a3.Y("DIRTY");
                a3.d0(32);
                a3.Y(str);
                a3.d0(10);
                a3.flush();
                if (this.f13374A) {
                    return null;
                }
                if (c1029b == null) {
                    c1029b = new C1029b(this, str);
                    this.f13384v.put(str, c1029b);
                }
                C0016c c0016c = new C0016c(this, c1029b);
                c1029b.f13366g = c0016c;
                return c0016c;
            }
            m();
            return null;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.f13375B && !this.f13376C) {
                for (C1029b c1029b : (C1029b[]) this.f13384v.values().toArray(new C1029b[0])) {
                    C0016c c0016c = c1029b.f13366g;
                    if (c0016c != null) {
                        C1029b c1029b2 = (C1029b) c0016c.f718r;
                        if (P6.g.a(c1029b2.f13366g, c0016c)) {
                            c1029b2.f13365f = true;
                        }
                    }
                }
                K();
                AbstractC0385z.f(this.f13385w);
                A a3 = this.f13388z;
                P6.g.b(a3);
                a3.close();
                this.f13388z = null;
                this.f13376C = true;
                return;
            }
            this.f13376C = true;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Flushable
    public final synchronized void flush() {
        if (this.f13375B) {
            if (this.f13376C) {
                throw new IllegalStateException("cache is closed");
            }
            K();
            A a3 = this.f13388z;
            P6.g.b(a3);
            a3.flush();
        }
    }

    public final synchronized C1030c g(String str) {
        C1030c c1030cA;
        if (this.f13376C) {
            throw new IllegalStateException("cache is closed");
        }
        O(str);
        j();
        C1029b c1029b = (C1029b) this.f13384v.get(str);
        if (c1029b != null && (c1030cA = c1029b.a()) != null) {
            boolean z8 = true;
            this.f13387y++;
            A a3 = this.f13388z;
            P6.g.b(a3);
            a3.Y("READ");
            a3.d0(32);
            a3.Y(str);
            a3.d0(10);
            if (this.f13387y < 2000) {
                z8 = false;
            }
            if (z8) {
                m();
            }
            return c1030cA;
        }
        return null;
    }

    public final synchronized void j() {
        try {
            if (this.f13375B) {
                return;
            }
            this.f13378F.d(this.f13382t);
            if (this.f13378F.e(this.f13383u)) {
                if (this.f13378F.e(this.f13381s)) {
                    this.f13378F.d(this.f13383u);
                } else {
                    this.f13378F.l(this.f13383u, this.f13381s);
                }
            }
            if (this.f13378F.e(this.f13381s)) {
                try {
                    u();
                    r();
                    this.f13375B = true;
                    return;
                } catch (IOException unused) {
                    try {
                        close();
                        AbstractC0707u1.g(this.f13378F, this.f13379q);
                        this.f13376C = false;
                        Q();
                        this.f13375B = true;
                    } catch (Throwable th) {
                        this.f13376C = false;
                        throw th;
                    }
                }
            }
            Q();
            this.f13375B = true;
        } catch (Throwable th2) {
            throw th2;
        }
    }

    public final void m() {
        AbstractC0385z.r(this.f13385w, null, new e(this, null), 3);
    }

    public final A n() {
        d dVar = this.f13378F;
        dVar.getClass();
        x xVar = this.f13381s;
        P6.g.e(xVar, "file");
        dVar.getClass();
        P6.g.e(xVar, "file");
        dVar.f13371b.getClass();
        File fileF = xVar.f();
        Logger logger = v.f18794a;
        return l.b(new g(new C1726b(new FileOutputStream(fileF, true), 1, new J()), new B6.a(5, this)));
    }

    public final void r() {
        Iterator it = this.f13384v.values().iterator();
        long j8 = 0;
        while (it.hasNext()) {
            C1029b c1029b = (C1029b) it.next();
            int i = 0;
            if (c1029b.f13366g == null) {
                while (i < 2) {
                    j8 += c1029b.f13361b[i];
                    i++;
                }
            } else {
                c1029b.f13366g = null;
                while (i < 2) {
                    x xVar = (x) c1029b.f13362c.get(i);
                    d dVar = this.f13378F;
                    dVar.d(xVar);
                    dVar.d((x) c1029b.f13363d.get(i));
                    i++;
                }
                it.remove();
            }
        }
        this.f13386x = j8;
    }

    public final void u() throws Throwable {
        B bC = l.c(this.f13378F.k(this.f13381s));
        try {
            String strK = bC.K(Long.MAX_VALUE);
            String strK2 = bC.K(Long.MAX_VALUE);
            String strK3 = bC.K(Long.MAX_VALUE);
            String strK4 = bC.K(Long.MAX_VALUE);
            String strK5 = bC.K(Long.MAX_VALUE);
            if (!"libcore.io.DiskLruCache".equals(strK) || !"1".equals(strK2) || !P6.g.a(String.valueOf(1), strK3) || !P6.g.a(String.valueOf(2), strK4) || strK5.length() > 0) {
                throw new IOException("unexpected journal header: [" + strK + ", " + strK2 + ", " + strK3 + ", " + strK4 + ", " + strK5 + ']');
            }
            int i = 0;
            while (true) {
                try {
                    A(bC.K(Long.MAX_VALUE));
                    i++;
                } catch (EOFException unused) {
                    this.f13387y = i - this.f13384v.size();
                    if (bC.a()) {
                        this.f13388z = n();
                    } else {
                        Q();
                    }
                    try {
                        bC.close();
                        th = null;
                    } catch (Throwable th) {
                        th = th;
                    }
                }
            }
        } catch (Throwable th2) {
            th = th2;
            try {
                bC.close();
            } catch (Throwable th3) {
                android.support.v4.media.session.a.a(th, th3);
            }
        }
        if (th != null) {
            throw th;
        }
    }
}
