package x4;

import android.os.Build;
import android.os.StrictMode;
import androidx.room.x;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public final class e implements Closeable {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final File f34726b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final File f34727c;

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public long f34732h = 0;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final LinkedHashMap f34734j = new LinkedHashMap(0, 0.75f, true);

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public long f34736l = 0;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final ThreadPoolExecutor f34737m = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new b());
    public final a Q = new a(this, 0);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f34729e = 1;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final int f34731g = 1;

    public e(File file, long j10) {
        this.f34725a = file;
        this.f34726b = new File(file, "journal");
        this.f34727c = new File(file, "journal.tmp");
        this.f34728d = new File(file, "journal.bkp");
        this.f34730f = j10;
    }

    public static e H(File file, long j10) throws IOException {
        if (j10 <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        File file2 = new File(file, "journal.bkp");
        if (file2.exists()) {
            File file3 = new File(file, "journal");
            if (file3.exists()) {
                file2.delete();
            } else {
                P(file2, file3, false);
            }
        }
        e eVar = new e(file, j10);
        if (eVar.f34726b.exists()) {
            try {
                eVar.M();
                eVar.L();
                return eVar;
            } catch (IOException e10) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e10.getMessage() + ", removing");
                eVar.close();
                h.a(eVar.f34725a);
            }
        }
        file.mkdirs();
        e eVar2 = new e(file, j10);
        eVar2.O();
        return eVar2;
    }

    public static void P(File file, File file2, boolean z10) throws IOException {
        if (z10) {
            k(file2);
        }
        if (!file.renameTo(file2)) {
            throw new IOException();
        }
    }

    public static void e(e eVar, x xVar, boolean z10) {
        synchronized (eVar) {
            c cVar = (c) xVar.f4197c;
            if (cVar.f34717f != xVar) {
                throw new IllegalStateException();
            }
            if (z10 && !cVar.f34716e) {
                for (int i10 = 0; i10 < eVar.f34731g; i10++) {
                    if (!((boolean[]) xVar.f4195a)[i10]) {
                        xVar.c();
                        throw new IllegalStateException("Newly created entry didn't create value for index " + i10);
                    }
                    if (!cVar.f34715d[i10].exists()) {
                        xVar.c();
                        return;
                    }
                }
            }
            for (int i11 = 0; i11 < eVar.f34731g; i11++) {
                File file = cVar.f34715d[i11];
                if (!z10) {
                    k(file);
                } else if (file.exists()) {
                    File file2 = cVar.f34714c[i11];
                    file.renameTo(file2);
                    long j10 = cVar.f34713b[i11];
                    long length = file2.length();
                    cVar.f34713b[i11] = length;
                    eVar.f34732h = (eVar.f34732h - j10) + length;
                }
            }
            eVar.f34735k++;
            cVar.f34717f = null;
            if (cVar.f34716e || z10) {
                cVar.f34716e = true;
                eVar.f34733i.append((CharSequence) "CLEAN");
                eVar.f34733i.append(' ');
                eVar.f34733i.append((CharSequence) cVar.f34712a);
                eVar.f34733i.append((CharSequence) cVar.a());
                eVar.f34733i.append('\n');
                if (z10) {
                    long j11 = eVar.f34736l;
                    eVar.f34736l = 1 + j11;
                    cVar.f34718g = j11;
                }
            } else {
                eVar.f34734j.remove(cVar.f34712a);
                eVar.f34733i.append((CharSequence) "REMOVE");
                eVar.f34733i.append(' ');
                eVar.f34733i.append((CharSequence) cVar.f34712a);
                eVar.f34733i.append('\n');
            }
            p(eVar.f34733i);
            if (eVar.f34732h > eVar.f34730f || eVar.E()) {
                eVar.f34737m.submit(eVar.Q);
            }
        }
    }

    public static void g(Writer writer) throws IOException {
        if (Build.VERSION.SDK_INT < 26) {
            writer.close();
            return;
        }
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            writer.close();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    public static void k(File file) throws IOException {
        if (file.exists() && !file.delete()) {
            throw new IOException();
        }
    }

    public static void p(Writer writer) throws IOException {
        if (Build.VERSION.SDK_INT < 26) {
            writer.flush();
            return;
        }
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            writer.flush();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    public final boolean E() {
        int i10 = this.f34735k;
        return i10 >= 2000 && i10 >= this.f34734j.size();
    }

    public final void L() throws IOException {
        k(this.f34727c);
        Iterator it = this.f34734j.values().iterator();
        while (it.hasNext()) {
            c cVar = (c) it.next();
            x xVar = cVar.f34717f;
            int i10 = this.f34731g;
            int i11 = 0;
            if (xVar == null) {
                while (i11 < i10) {
                    this.f34732h += cVar.f34713b[i11];
                    i11++;
                }
            } else {
                cVar.f34717f = null;
                while (i11 < i10) {
                    k(cVar.f34714c[i11]);
                    k(cVar.f34715d[i11]);
                    i11++;
                }
                it.remove();
            }
        }
    }

    public final void M() {
        File file = this.f34726b;
        g gVar = new g(new FileInputStream(file), h.f34744a);
        try {
            String strE = gVar.e();
            String strE2 = gVar.e();
            String strE3 = gVar.e();
            String strE4 = gVar.e();
            String strE5 = gVar.e();
            if (!"libcore.io.DiskLruCache".equals(strE) || !"1".equals(strE2) || !Integer.toString(this.f34729e).equals(strE3) || !Integer.toString(this.f34731g).equals(strE4) || !"".equals(strE5)) {
                throw new IOException("unexpected journal header: [" + strE + ", " + strE2 + ", " + strE4 + ", " + strE5 + "]");
            }
            int i10 = 0;
            while (true) {
                try {
                    N(gVar.e());
                    i10++;
                } catch (EOFException unused) {
                    this.f34735k = i10 - this.f34734j.size();
                    if (gVar.f34743e == -1) {
                        O();
                    } else {
                        this.f34733i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), h.f34744a));
                    }
                    try {
                        gVar.close();
                        return;
                    } catch (RuntimeException e10) {
                        throw e10;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } catch (Throwable th) {
            try {
                gVar.close();
            } catch (RuntimeException e11) {
                throw e11;
            } catch (Exception unused3) {
            }
            throw th;
        }
    }

    public final void N(String str) throws IOException {
        String strSubstring;
        int iIndexOf = str.indexOf(32);
        if (iIndexOf == -1) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
        int i10 = iIndexOf + 1;
        int iIndexOf2 = str.indexOf(32, i10);
        LinkedHashMap linkedHashMap = this.f34734j;
        if (iIndexOf2 == -1) {
            strSubstring = str.substring(i10);
            if (iIndexOf == 6 && str.startsWith("REMOVE")) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i10, iIndexOf2);
        }
        c cVar = (c) linkedHashMap.get(strSubstring);
        if (cVar == null) {
            cVar = new c(this, strSubstring);
            linkedHashMap.put(strSubstring, cVar);
        }
        if (iIndexOf2 == -1 || iIndexOf != 5 || !str.startsWith("CLEAN")) {
            if (iIndexOf2 == -1 && iIndexOf == 5 && str.startsWith("DIRTY")) {
                cVar.f34717f = new x(this, cVar);
                return;
            } else {
                if (iIndexOf2 != -1 || iIndexOf != 4 || !str.startsWith("READ")) {
                    throw new IOException("unexpected journal line: ".concat(str));
                }
                return;
            }
        }
        String[] strArrSplit = str.substring(iIndexOf2 + 1).split(" ");
        cVar.f34716e = true;
        cVar.f34717f = null;
        if (strArrSplit.length != cVar.f34719h.f34731g) {
            throw new IOException("unexpected journal line: " + Arrays.toString(strArrSplit));
        }
        for (int i11 = 0; i11 < strArrSplit.length; i11++) {
            try {
                cVar.f34713b[i11] = Long.parseLong(strArrSplit[i11]);
            } catch (NumberFormatException unused) {
                throw new IOException("unexpected journal line: " + Arrays.toString(strArrSplit));
            }
        }
    }

    public final synchronized void O() {
        try {
            BufferedWriter bufferedWriter = this.f34733i;
            if (bufferedWriter != null) {
                g(bufferedWriter);
            }
            BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f34727c), h.f34744a));
            try {
                bufferedWriter2.write("libcore.io.DiskLruCache");
                bufferedWriter2.write("\n");
                bufferedWriter2.write("1");
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f34729e));
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f34731g));
                bufferedWriter2.write("\n");
                bufferedWriter2.write("\n");
                for (c cVar : this.f34734j.values()) {
                    if (cVar.f34717f != null) {
                        bufferedWriter2.write("DIRTY " + cVar.f34712a + '\n');
                    } else {
                        bufferedWriter2.write("CLEAN " + cVar.f34712a + cVar.a() + '\n');
                    }
                }
                g(bufferedWriter2);
                if (this.f34726b.exists()) {
                    P(this.f34726b, this.f34728d, true);
                }
                P(this.f34727c, this.f34726b, false);
                this.f34728d.delete();
                this.f34733i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f34726b, true), h.f34744a));
            } catch (Throwable th) {
                g(bufferedWriter2);
                throw th;
            }
        } catch (Throwable th2) {
            throw th2;
        }
    }

    public final void Q() {
        while (this.f34732h > this.f34730f) {
            String str = (String) ((Map.Entry) this.f34734j.entrySet().iterator().next()).getKey();
            synchronized (this) {
                try {
                    if (this.f34733i == null) {
                        throw new IllegalStateException("cache is closed");
                    }
                    c cVar = (c) this.f34734j.get(str);
                    if (cVar != null && cVar.f34717f == null) {
                        for (int i10 = 0; i10 < this.f34731g; i10++) {
                            File file = cVar.f34714c[i10];
                            if (file.exists() && !file.delete()) {
                                throw new IOException("failed to delete " + file);
                            }
                            long j10 = this.f34732h;
                            long[] jArr = cVar.f34713b;
                            this.f34732h = j10 - jArr[i10];
                            jArr[i10] = 0;
                        }
                        this.f34735k++;
                        this.f34733i.append((CharSequence) "REMOVE");
                        this.f34733i.append(' ');
                        this.f34733i.append((CharSequence) str);
                        this.f34733i.append('\n');
                        this.f34734j.remove(str);
                        if (E()) {
                            this.f34737m.submit(this.Q);
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.f34733i == null) {
                return;
            }
            Iterator it = new ArrayList(this.f34734j.values()).iterator();
            while (it.hasNext()) {
                x xVar = ((c) it.next()).f34717f;
                if (xVar != null) {
                    xVar.c();
                }
            }
            Q();
            g(this.f34733i);
            this.f34733i = null;
        } catch (Throwable th) {
            throw th;
        }
    }

    public final x m(String str) {
        synchronized (this) {
            try {
                if (this.f34733i == null) {
                    throw new IllegalStateException("cache is closed");
                }
                c cVar = (c) this.f34734j.get(str);
                if (cVar == null) {
                    cVar = new c(this, str);
                    this.f34734j.put(str, cVar);
                } else if (cVar.f34717f != null) {
                    return null;
                }
                x xVar = new x(this, cVar);
                cVar.f34717f = xVar;
                this.f34733i.append((CharSequence) "DIRTY");
                this.f34733i.append(' ');
                this.f34733i.append((CharSequence) str);
                this.f34733i.append('\n');
                p(this.f34733i);
                return xVar;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized d x(String str) {
        if (this.f34733i == null) {
            throw new IllegalStateException("cache is closed");
        }
        c cVar = (c) this.f34734j.get(str);
        if (cVar == null) {
            return null;
        }
        if (!cVar.f34716e) {
            return null;
        }
        for (File file : cVar.f34714c) {
            if (!file.exists()) {
                return null;
            }
        }
        this.f34735k++;
        this.f34733i.append((CharSequence) "READ");
        this.f34733i.append(' ');
        this.f34733i.append((CharSequence) str);
        this.f34733i.append('\n');
        if (E()) {
            this.f34737m.submit(this.Q);
        }
        long j10 = cVar.f34718g;
        File[] fileArr = cVar.f34714c;
        long[] jArr = cVar.f34713b;
        d dVar = new d();
        dVar.f34721b = this;
        dVar.f34722c = str;
        dVar.f34720a = j10;
        dVar.f34724e = fileArr;
        dVar.f34723d = jArr;
        return dVar;
    }
}
