package defpackage;

import android.os.Build;
import android.os.StrictMode;
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.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* renamed from: ᵬːᵝ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4035 implements Closeable {

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public BufferedWriter f15153;

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public final long f15156;

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final File f15157;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final File f15160;

    /* renamed from: ᵮᵠـˌ, reason: contains not printable characters */
    public int f15161;

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public final File f15163;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final File f15164;

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public long f15155 = 0;

    /* renamed from: ˎˑː, reason: contains not printable characters */
    public final LinkedHashMap f15152 = new LinkedHashMap(0, 0.75f, true);

    /* renamed from: ᵧـˌᵎ, reason: contains not printable characters */
    public long f15159 = 0;

    /* renamed from: ᵺʙ, reason: contains not printable characters */
    public final ThreadPoolExecutor f15162 = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactoryC2801());

    /* renamed from: ᐧﾞ, reason: contains not printable characters */
    public final CallableC3631 f15154 = new CallableC3631(0, this);

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public final int f15158 = 1;

    /* renamed from: ˀᵗᵝᵭ, reason: contains not printable characters */
    public final int f15151 = 1;

    public C4035(File file, long j) {
        this.f15160 = file;
        this.f15157 = new File(file, "journal");
        this.f15164 = new File(file, "journal.tmp");
        this.f15163 = new File(file, "journal.bkp");
        this.f15156 = j;
    }

    /* renamed from: ˊʽˎ, reason: contains not printable characters */
    public static void m8879(File file, File file2, boolean z) throws IOException {
        if (z) {
            m8880(file2);
        }
        if (!file.renameTo(file2)) {
            throw new IOException();
        }
    }

    /* renamed from: ˌʾˀ, reason: contains not printable characters */
    public static void m8880(File file) throws IOException {
        if (file.exists() && !file.delete()) {
            throw new IOException();
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public static void m8881(BufferedWriter bufferedWriter) throws IOException {
        if (Build.VERSION.SDK_INT < 26) {
            bufferedWriter.close();
            return;
        }
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            bufferedWriter.close();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public static C4035 m8882(File file, long j) throws IOException {
        if (j <= 0) {
            ᵹˉᵞ.ˌᵜᵹᵩ("maxSize <= 0");
            return null;
        }
        File file2 = new File(file, "journal.bkp");
        if (file2.exists()) {
            File file3 = new File(file, "journal");
            if (file3.exists()) {
                file2.delete();
            } else {
                m8879(file2, file3, false);
            }
        }
        C4035 c4035 = new C4035(file, j);
        if (c4035.f15157.exists()) {
            try {
                c4035.m8885();
                c4035.m8886();
                return c4035;
            } catch (IOException e) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                c4035.close();
                AbstractC2882.m7050(c4035.f15160);
            }
        }
        file.mkdirs();
        C4035 c40352 = new C4035(file, j);
        c40352.m8892();
        return c40352;
    }

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public static void m8883(BufferedWriter bufferedWriter) throws IOException {
        if (Build.VERSION.SDK_INT < 26) {
            bufferedWriter.flush();
            return;
        }
        StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
        try {
            bufferedWriter.flush();
        } finally {
            StrictMode.setThreadPolicy(threadPolicy);
        }
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static void m8884(C4035 c4035, C4699 c4699, boolean z) {
        synchronized (c4035) {
            C0867 c0867 = (C0867) c4699.f17372;
            if (c0867.f3941 != c4699) {
                throw new IllegalStateException();
            }
            if (z && !c0867.f3939) {
                for (int i = 0; i < c4035.f15151; i++) {
                    if (!((boolean[]) c4699.f17375)[i]) {
                        c4699.m9884();
                        throw new IllegalStateException("Newly created entry didn't create value for index " + i);
                    }
                    if (!c0867.f3945[i].exists()) {
                        c4699.m9884();
                        return;
                    }
                }
            }
            for (int i2 = 0; i2 < c4035.f15151; i2++) {
                File file = c0867.f3945[i2];
                if (!z) {
                    m8880(file);
                } else if (file.exists()) {
                    File file2 = c0867.f3942[i2];
                    file.renameTo(file2);
                    long j = c0867.f3944[i2];
                    long length = file2.length();
                    c0867.f3944[i2] = length;
                    c4035.f15155 = (c4035.f15155 - j) + length;
                }
            }
            c4035.f15161++;
            c0867.f3941 = null;
            if (c0867.f3939 || z) {
                c0867.f3939 = true;
                c4035.f15153.append((CharSequence) "CLEAN");
                c4035.f15153.append(' ');
                c4035.f15153.append((CharSequence) c0867.f3943);
                c4035.f15153.append((CharSequence) c0867.m2987());
                c4035.f15153.append('\n');
                if (z) {
                    c4035.f15159++;
                }
            } else {
                c4035.f15152.remove(c0867.f3943);
                c4035.f15153.append((CharSequence) "REMOVE");
                c4035.f15153.append(' ');
                c4035.f15153.append((CharSequence) c0867.f3943);
                c4035.f15153.append('\n');
            }
            m8883(c4035.f15153);
            if (c4035.f15155 > c4035.f15156 || c4035.m8890()) {
                c4035.f15162.submit(c4035.f15154);
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.f15153 == null) {
                return;
            }
            ArrayList arrayList = new ArrayList(this.f15152.values());
            int size = arrayList.size();
            int i = 0;
            while (i < size) {
                Object obj = arrayList.get(i);
                i++;
                C4699 c4699 = ((C0867) obj).f3941;
                if (c4699 != null) {
                    c4699.m9884();
                }
            }
            m8887();
            m8881(this.f15153);
            this.f15153 = null;
        } catch (Throwable th) {
            throw th;
        }
    }

    /* renamed from: ˀᵗᵩ, reason: contains not printable characters */
    public final void m8885() {
        File file = this.f15157;
        C0142 c0142 = new C0142(new FileInputStream(file), AbstractC2882.f11167);
        try {
            String strM1288 = c0142.m1288();
            String strM12882 = c0142.m1288();
            String strM12883 = c0142.m1288();
            String strM12884 = c0142.m1288();
            String strM12885 = c0142.m1288();
            if (!"libcore.io.DiskLruCache".equals(strM1288) || !"1".equals(strM12882) || !Integer.toString(this.f15158).equals(strM12883) || !Integer.toString(this.f15151).equals(strM12884) || !"".equals(strM12885)) {
                throw new IOException("unexpected journal header: [" + strM1288 + ", " + strM12882 + ", " + strM12884 + ", " + strM12885 + "]");
            }
            int i = 0;
            while (true) {
                try {
                    m8888(c0142.m1288());
                    i++;
                } catch (EOFException unused) {
                    this.f15161 = i - this.f15152.size();
                    if (c0142.f1522 == -1) {
                        m8892();
                    } else {
                        this.f15153 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), AbstractC2882.f11167));
                    }
                    try {
                        c0142.close();
                        return;
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } catch (Throwable th) {
            try {
                c0142.close();
            } catch (RuntimeException e2) {
                throw e2;
            } catch (Exception unused3) {
            }
            throw th;
        }
    }

    /* renamed from: ˊᵸˊ, reason: contains not printable characters */
    public final void m8886() throws IOException {
        m8880(this.f15164);
        Iterator it = this.f15152.values().iterator();
        while (it.hasNext()) {
            C0867 c0867 = (C0867) it.next();
            C4699 c4699 = c0867.f3941;
            int i = this.f15151;
            int i2 = 0;
            if (c4699 == null) {
                while (i2 < i) {
                    this.f15155 += c0867.f3944[i2];
                    i2++;
                }
            } else {
                c0867.f3941 = null;
                while (i2 < i) {
                    m8880(c0867.f3942[i2]);
                    m8880(c0867.f3945[i2]);
                    i2++;
                }
                it.remove();
            }
        }
    }

    /* renamed from: ˎᵬˀ, reason: contains not printable characters */
    public final void m8887() {
        while (this.f15155 > this.f15156) {
            String str = (String) ((Map.Entry) this.f15152.entrySet().iterator().next()).getKey();
            synchronized (this) {
                try {
                    if (this.f15153 == null) {
                        throw new IllegalStateException("cache is closed");
                    }
                    C0867 c0867 = (C0867) this.f15152.get(str);
                    if (c0867 != null && c0867.f3941 == null) {
                        for (int i = 0; i < this.f15151; i++) {
                            File file = c0867.f3942[i];
                            if (file.exists() && !file.delete()) {
                                throw new IOException("failed to delete " + file);
                            }
                            long j = this.f15155;
                            long[] jArr = c0867.f3944;
                            this.f15155 = j - jArr[i];
                            jArr[i] = 0;
                        }
                        this.f15161++;
                        this.f15153.append((CharSequence) "REMOVE");
                        this.f15153.append(' ');
                        this.f15153.append((CharSequence) str);
                        this.f15153.append('\n');
                        this.f15152.remove(str);
                        if (m8890()) {
                            this.f15162.submit(this.f15154);
                        }
                    }
                } finally {
                }
            }
        }
    }

    /* renamed from: ᵛˈᵤ, reason: contains not printable characters */
    public final void m8888(String str) throws IOException {
        String strSubstring;
        int iIndexOf = str.indexOf(32);
        if (iIndexOf == -1) {
            C0910.m3045("unexpected journal line: ".concat(str));
            return;
        }
        int i = iIndexOf + 1;
        int iIndexOf2 = str.indexOf(32, i);
        LinkedHashMap linkedHashMap = this.f15152;
        if (iIndexOf2 == -1) {
            strSubstring = str.substring(i);
            if (iIndexOf == 6 && str.startsWith("REMOVE")) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iIndexOf2);
        }
        C0867 c0867 = (C0867) linkedHashMap.get(strSubstring);
        if (c0867 == null) {
            c0867 = new C0867(this, strSubstring);
            linkedHashMap.put(strSubstring, c0867);
        }
        if (iIndexOf2 == -1 || iIndexOf != 5 || !str.startsWith("CLEAN")) {
            if (iIndexOf2 == -1 && iIndexOf == 5 && str.startsWith("DIRTY")) {
                c0867.f3941 = new C4699(this, c0867);
                return;
            } else {
                if (iIndexOf2 == -1 && iIndexOf == 4 && str.startsWith("READ")) {
                    return;
                }
                C0910.m3045("unexpected journal line: ".concat(str));
                return;
            }
        }
        String[] strArrSplit = str.substring(iIndexOf2 + 1).split(" ");
        c0867.f3939 = true;
        c0867.f3941 = null;
        if (strArrSplit.length != c0867.f3940.f15151) {
            ᵨᴵᵺ.ᵸʓᵠ(Arrays.toString(strArrSplit), "unexpected journal line: ");
            return;
        }
        for (int i2 = 0; i2 < strArrSplit.length; i2++) {
            try {
                c0867.f3944[i2] = Long.parseLong(strArrSplit[i2]);
            } catch (NumberFormatException unused) {
                ᵨᴵᵺ.ᵸʓᵠ(Arrays.toString(strArrSplit), "unexpected journal line: ");
                return;
            }
        }
    }

    /* renamed from: ᵞʡ, reason: contains not printable characters */
    public final C4699 m8889(String str) {
        synchronized (this) {
            try {
                if (this.f15153 == null) {
                    throw new IllegalStateException("cache is closed");
                }
                C0867 c0867 = (C0867) this.f15152.get(str);
                if (c0867 == null) {
                    c0867 = new C0867(this, str);
                    this.f15152.put(str, c0867);
                } else if (c0867.f3941 != null) {
                    return null;
                }
                C4699 c4699 = new C4699(this, c0867);
                c0867.f3941 = c4699;
                this.f15153.append((CharSequence) "DIRTY");
                this.f15153.append(' ');
                this.f15153.append((CharSequence) str);
                this.f15153.append('\n');
                m8883(this.f15153);
                return c4699;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public final boolean m8890() {
        int i = this.f15161;
        return i >= 2000 && i >= this.f15152.size();
    }

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public final synchronized C2931 m8891(String str) {
        if (this.f15153 == null) {
            throw new IllegalStateException("cache is closed");
        }
        C0867 c0867 = (C0867) this.f15152.get(str);
        if (c0867 == null) {
            return null;
        }
        if (!c0867.f3939) {
            return null;
        }
        for (File file : c0867.f3942) {
            if (!file.exists()) {
                return null;
            }
        }
        this.f15161++;
        this.f15153.append((CharSequence) "READ");
        this.f15153.append(' ');
        this.f15153.append((CharSequence) str);
        this.f15153.append('\n');
        if (m8890()) {
            this.f15162.submit(this.f15154);
        }
        return new C2931(c0867.f3942);
    }

    /* renamed from: ᵳᵹᵺˋ, reason: contains not printable characters */
    public final synchronized void m8892() {
        try {
            BufferedWriter bufferedWriter = this.f15153;
            if (bufferedWriter != null) {
                m8881(bufferedWriter);
            }
            BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f15164), AbstractC2882.f11167));
            try {
                bufferedWriter2.write("libcore.io.DiskLruCache");
                bufferedWriter2.write("\n");
                bufferedWriter2.write("1");
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f15158));
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f15151));
                bufferedWriter2.write("\n");
                bufferedWriter2.write("\n");
                for (C0867 c0867 : this.f15152.values()) {
                    if (c0867.f3941 != null) {
                        bufferedWriter2.write("DIRTY " + c0867.f3943 + '\n');
                    } else {
                        bufferedWriter2.write("CLEAN " + c0867.f3943 + c0867.m2987() + '\n');
                    }
                }
                m8881(bufferedWriter2);
                if (this.f15157.exists()) {
                    m8879(this.f15157, this.f15163, true);
                }
                m8879(this.f15164, this.f15157, false);
                this.f15163.delete();
                this.f15153 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f15157, true), AbstractC2882.f11167));
            } catch (Throwable th) {
                m8881(bufferedWriter2);
                throw th;
            }
        } catch (Throwable th2) {
            throw th2;
        }
    }
}
