package defpackage;

import android.os.StrictMode;
import androidx.media3.extractor.metadata.icy.IcyHeaders;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public final class fc2 implements Closeable {
    public final File a;
    public final File b;
    public final File c;
    public final File d;
    public final long f;
    public BufferedWriter k;
    public int m;
    public long j = 0;
    public final LinkedHashMap l = new LinkedHashMap(0, 0.75f, true);
    public long n = 0;
    public final ThreadPoolExecutor o = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new b());
    public final Callable p = new a();
    public final int e = 1;
    public final int i = 1;

    public class a implements Callable<Void> {
        public a() {
        }

        @Override // java.util.concurrent.Callable
        public final Void call() {
            synchronized (fc2.this) {
                try {
                    fc2 fc2Var = fc2.this;
                    if (fc2Var.k == null) {
                        return null;
                    }
                    fc2Var.af();
                    if (fc2.this.p()) {
                        fc2.this.ab();
                        fc2.this.m = 0;
                    }
                    return null;
                } finally {
                }
            }
        }
    }

    public static final class b implements ThreadFactory {
        @Override // java.util.concurrent.ThreadFactory
        public final synchronized Thread newThread(Runnable runnable) {
            Thread thread;
            thread = new Thread(runnable, "glide-disk-lru-cache-thread");
            thread.setPriority(1);
            return thread;
        }
    }

    public final class c {
        public final d a;
        public final boolean[] b;
        public boolean c;

        public c(d dVar) {
            this.a = dVar;
            this.b = dVar.e ? null : new boolean[fc2.this.i];
        }

        public final void a() {
            fc2.a(fc2.this, this, false);
        }

        public final File b() {
            File file;
            synchronized (fc2.this) {
                try {
                    d dVar = this.a;
                    if (dVar.f != this) {
                        throw new IllegalStateException();
                    }
                    if (!dVar.e) {
                        this.b[0] = true;
                    }
                    file = dVar.d[0];
                    fc2.this.a.mkdirs();
                } catch (Throwable th) {
                    throw th;
                }
            }
            return file;
        }
    }

    public final class d {
        public final String a;
        public final long[] b;
        public final File[] c;
        public final File[] d;
        public boolean e;
        public c f;

        public d(String str) {
            this.a = str;
            int i = fc2.this.i;
            File file = fc2.this.a;
            this.b = new long[i];
            this.c = new File[i];
            this.d = new File[i];
            StringBuilder sb = new StringBuilder(str);
            sb.append('.');
            int length = sb.length();
            for (int i2 = 0; i2 < i; i2++) {
                sb.append(i2);
                this.c[i2] = new File(file, sb.toString());
                sb.append(".tmp");
                this.d[i2] = new File(file, sb.toString());
                sb.setLength(length);
            }
        }

        public final String a() {
            StringBuilder sb = new StringBuilder();
            for (long j : this.b) {
                sb.append(' ');
                sb.append(j);
            }
            return sb.toString();
        }
    }

    public final class e {
        public final File[] a;

        public e(File[] fileArr) {
            this.a = fileArr;
        }
    }

    public fc2(File file, long j) {
        this.a = file;
        this.b = new File(file, "journal");
        this.c = new File(file, "journal.tmp");
        this.d = new File(file, "journal.bkp");
        this.f = j;
    }

    public static void a(fc2 fc2Var, c cVar, boolean z) {
        synchronized (fc2Var) {
            d dVar = cVar.a;
            if (dVar.f != cVar) {
                throw new IllegalStateException();
            }
            if (z && !dVar.e) {
                for (int i = 0; i < fc2Var.i; i++) {
                    if (!cVar.b[i]) {
                        cVar.a();
                        throw new IllegalStateException("Newly created entry didn't create value for index " + i);
                    }
                    if (!dVar.d[i].exists()) {
                        cVar.a();
                        return;
                    }
                }
            }
            for (int i2 = 0; i2 < fc2Var.i; i2++) {
                File file = dVar.d[i2];
                if (!z) {
                    h(file);
                } else if (file.exists()) {
                    File file2 = dVar.c[i2];
                    file.renameTo(file2);
                    long j = dVar.b[i2];
                    long length = file2.length();
                    dVar.b[i2] = length;
                    fc2Var.j = (fc2Var.j - j) + length;
                }
            }
            fc2Var.m++;
            dVar.f = null;
            if (dVar.e || z) {
                dVar.e = true;
                fc2Var.k.append((CharSequence) "CLEAN");
                fc2Var.k.append(' ');
                fc2Var.k.append((CharSequence) dVar.a);
                fc2Var.k.append((CharSequence) dVar.a());
                fc2Var.k.append('\n');
                if (z) {
                    fc2Var.n++;
                }
            } else {
                fc2Var.l.remove(dVar.a);
                fc2Var.k.append((CharSequence) "REMOVE");
                fc2Var.k.append(' ');
                fc2Var.k.append((CharSequence) dVar.a);
                fc2Var.k.append('\n');
            }
            l(fc2Var.k);
            if (fc2Var.j > fc2Var.f || fc2Var.p()) {
                fc2Var.o.submit(fc2Var.p);
            }
        }
    }

    public static void ad(File file, File file2, boolean z) throws IOException {
        if (z) {
            h(file2);
        }
        if (!file.renameTo(file2)) {
            throw new IOException();
        }
    }

    public static void b(BufferedWriter bufferedWriter) {
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            bufferedWriter.close();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

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

    public static void l(BufferedWriter bufferedWriter) {
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            bufferedWriter.flush();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    public static fc2 u(File file, long j) throws IOException {
        if (j <= 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 {
                ad(file2, file3, false);
            }
        }
        fc2 fc2Var = new fc2(file, j);
        if (fc2Var.b.exists()) {
            try {
                fc2Var.z();
                fc2Var.w();
                return fc2Var;
            } catch (IOException e2) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e2.getMessage() + ", removing");
                fc2Var.d();
            }
        }
        file.mkdirs();
        fc2 fc2Var2 = new fc2(file, j);
        fc2Var2.ab();
        return fc2Var2;
    }

    public final void aa(String str) throws IOException {
        String strSubstring;
        int iIndexOf = str.indexOf(32);
        if (iIndexOf == -1) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
        int i = iIndexOf + 1;
        int iIndexOf2 = str.indexOf(32, i);
        LinkedHashMap linkedHashMap = this.l;
        if (iIndexOf2 == -1) {
            strSubstring = str.substring(i);
            if (iIndexOf == 6 && str.startsWith("REMOVE")) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iIndexOf2);
        }
        d dVar = (d) linkedHashMap.get(strSubstring);
        if (dVar == null) {
            dVar = new d(strSubstring);
            linkedHashMap.put(strSubstring, dVar);
        }
        if (iIndexOf2 == -1 || iIndexOf != 5 || !str.startsWith("CLEAN")) {
            if (iIndexOf2 == -1 && iIndexOf == 5 && str.startsWith("DIRTY")) {
                dVar.f = new c(dVar);
                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(" ");
        dVar.e = true;
        dVar.f = null;
        if (strArrSplit.length != fc2.this.i) {
            throw new IOException("unexpected journal line: " + Arrays.toString(strArrSplit));
        }
        for (int i2 = 0; i2 < strArrSplit.length; i2++) {
            try {
                dVar.b[i2] = Long.parseLong(strArrSplit[i2]);
            } catch (NumberFormatException unused) {
                throw new IOException("unexpected journal line: " + Arrays.toString(strArrSplit));
            }
        }
    }

    public final synchronized void ab() {
        try {
            BufferedWriter bufferedWriter = this.k;
            if (bufferedWriter != null) {
                b(bufferedWriter);
            }
            BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.c), gda.a));
            try {
                bufferedWriter2.write("libcore.io.DiskLruCache");
                bufferedWriter2.write("\n");
                bufferedWriter2.write(IcyHeaders.REQUEST_HEADER_ENABLE_METADATA_VALUE);
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.e));
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.i));
                bufferedWriter2.write("\n");
                bufferedWriter2.write("\n");
                for (d dVar : this.l.values()) {
                    if (dVar.f != null) {
                        bufferedWriter2.write("DIRTY " + dVar.a + '\n');
                    } else {
                        bufferedWriter2.write("CLEAN " + dVar.a + dVar.a() + '\n');
                    }
                }
                b(bufferedWriter2);
                if (this.b.exists()) {
                    ad(this.b, this.d, true);
                }
                ad(this.c, this.b, false);
                this.d.delete();
                this.k = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.b, true), gda.a));
            } catch (Throwable th) {
                b(bufferedWriter2);
                throw th;
            }
        } catch (Throwable th2) {
            throw th2;
        }
    }

    public final void af() {
        while (this.j > this.f) {
            String str = (String) ((Map.Entry) this.l.entrySet().iterator().next()).getKey();
            synchronized (this) {
                try {
                    if (this.k == null) {
                        throw new IllegalStateException("cache is closed");
                    }
                    d dVar = (d) this.l.get(str);
                    if (dVar != null && dVar.f == null) {
                        for (int i = 0; i < this.i; i++) {
                            File file = dVar.c[i];
                            if (file.exists() && !file.delete()) {
                                throw new IOException("failed to delete " + file);
                            }
                            long j = this.j;
                            long[] jArr = dVar.b;
                            this.j = j - jArr[i];
                            jArr[i] = 0;
                        }
                        this.m++;
                        this.k.append((CharSequence) "REMOVE");
                        this.k.append(' ');
                        this.k.append((CharSequence) str);
                        this.k.append('\n');
                        this.l.remove(str);
                        if (p()) {
                            this.o.submit(this.p);
                        }
                    }
                } finally {
                }
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.k == null) {
                return;
            }
            Iterator it = new ArrayList(this.l.values()).iterator();
            while (it.hasNext()) {
                c cVar = ((d) it.next()).f;
                if (cVar != null) {
                    cVar.a();
                }
            }
            af();
            b(this.k);
            this.k = null;
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void d() throws IOException {
        close();
        gda.a(this.a);
    }

    public final c k(String str) {
        synchronized (this) {
            try {
                if (this.k == null) {
                    throw new IllegalStateException("cache is closed");
                }
                d dVar = (d) this.l.get(str);
                if (dVar == null) {
                    dVar = new d(str);
                    this.l.put(str, dVar);
                } else if (dVar.f != null) {
                    return null;
                }
                c cVar = new c(dVar);
                dVar.f = cVar;
                this.k.append((CharSequence) "DIRTY");
                this.k.append(' ');
                this.k.append((CharSequence) str);
                this.k.append('\n');
                l(this.k);
                return cVar;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized e n(String str) {
        if (this.k == null) {
            throw new IllegalStateException("cache is closed");
        }
        d dVar = (d) this.l.get(str);
        if (dVar == null) {
            return null;
        }
        if (!dVar.e) {
            return null;
        }
        for (File file : dVar.c) {
            if (!file.exists()) {
                return null;
            }
        }
        this.m++;
        this.k.append((CharSequence) "READ");
        this.k.append(' ');
        this.k.append((CharSequence) str);
        this.k.append('\n');
        if (p()) {
            this.o.submit(this.p);
        }
        return new e(dVar.c);
    }

    public final boolean p() {
        int i = this.m;
        return i >= 2000 && i >= this.l.size();
    }

    public final void w() throws IOException {
        h(this.c);
        Iterator it = this.l.values().iterator();
        while (it.hasNext()) {
            d dVar = (d) it.next();
            c cVar = dVar.f;
            int i = this.i;
            int i2 = 0;
            if (cVar == null) {
                while (i2 < i) {
                    this.j += dVar.b[i2];
                    i2++;
                }
            } else {
                dVar.f = null;
                while (i2 < i) {
                    h(dVar.c[i2]);
                    h(dVar.d[i2]);
                    i2++;
                }
                it.remove();
            }
        }
    }

    public final void z() {
        File file = this.b;
        b79 b79Var = new b79(new FileInputStream(file), gda.a);
        try {
            String strA = b79Var.a();
            String strA2 = b79Var.a();
            String strA3 = b79Var.a();
            String strA4 = b79Var.a();
            String strA5 = b79Var.a();
            if (!"libcore.io.DiskLruCache".equals(strA) || !IcyHeaders.REQUEST_HEADER_ENABLE_METADATA_VALUE.equals(strA2) || !Integer.toString(this.e).equals(strA3) || !Integer.toString(this.i).equals(strA4) || !"".equals(strA5)) {
                throw new IOException("unexpected journal header: [" + strA + ", " + strA2 + ", " + strA4 + ", " + strA5 + "]");
            }
            int i = 0;
            while (true) {
                try {
                    aa(b79Var.a());
                    i++;
                } catch (EOFException unused) {
                    this.m = i - this.l.size();
                    if (b79Var.e == -1) {
                        ab();
                    } else {
                        this.k = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), gda.a));
                    }
                    try {
                        b79Var.close();
                        return;
                    } catch (RuntimeException e2) {
                        throw e2;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } catch (Throwable th) {
            try {
                b79Var.close();
            } catch (RuntimeException e3) {
                throw e3;
            } catch (Exception unused3) {
            }
            throw th;
        }
    }
}
