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;

/* JADX INFO: renamed from: ᵝʙᵜ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C2899 implements Closeable {

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public int f11134;

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public final File f11138;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final File f11141;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final File f11142;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public final File f11144;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final long f11146;

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public BufferedWriter f11147;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public long f11139 = 0;

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public final LinkedHashMap f11140 = new LinkedHashMap(0, 0.75f, true);

    /* JADX INFO: renamed from: ᵠˏˏ, reason: contains not printable characters */
    public long f11143 = 0;

    /* JADX INFO: renamed from: ˉᵵᵜ, reason: contains not printable characters */
    public final ThreadPoolExecutor f11137 = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactoryC5584());

    /* JADX INFO: renamed from: ˁˌᵩ, reason: contains not printable characters */
    public final CallableC2619 f11135 = new CallableC2619(0, this);

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final int f11145 = 1;

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public final int f11136 = 1;

    public C2899(File file, long j) {
        this.f11138 = file;
        this.f11142 = new File(file, "journal");
        this.f11141 = new File(file, "journal.tmp");
        this.f11144 = new File(file, "journal.bkp");
        this.f11146 = j;
    }

    /* JADX INFO: renamed from: ˀᵗ, reason: contains not printable characters */
    public static void m6946(File file, File file2, boolean z) throws IOException {
        if (z) {
            m6949(file2);
        }
        if (!file.renameTo(file2)) {
            throw new IOException();
        }
    }

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public static void m6947(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);
        }
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public static void m6948(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);
        }
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public static void m6949(File file) throws IOException {
        if (file.exists() && !file.delete()) {
            throw new IOException();
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public static void m6950(C2899 c2899, C0764 c0764, boolean z) {
        synchronized (c2899) {
            C3299 c3299 = (C3299) c0764.f3428;
            if (c3299.f12708 != c0764) {
                throw new IllegalStateException();
            }
            if (z && !c3299.f12704) {
                for (int i = 0; i < c2899.f11136; i++) {
                    if (!((boolean[]) c0764.f3427)[i]) {
                        c0764.m2748();
                        throw new IllegalStateException("Newly created entry didn't create value for index " + i);
                    }
                    if (!c3299.f12707[i].exists()) {
                        c0764.m2748();
                        return;
                    }
                }
            }
            for (int i2 = 0; i2 < c2899.f11136; i2++) {
                File file = c3299.f12707[i2];
                if (!z) {
                    m6949(file);
                } else if (file.exists()) {
                    File file2 = c3299.f12703[i2];
                    file.renameTo(file2);
                    long j = c3299.f12702[i2];
                    long length = file2.length();
                    c3299.f12702[i2] = length;
                    c2899.f11139 = (c2899.f11139 - j) + length;
                }
            }
            c2899.f11134++;
            c3299.f12708 = null;
            if (c3299.f12704 || z) {
                c3299.f12704 = true;
                c2899.f11147.append((CharSequence) "CLEAN");
                c2899.f11147.append(' ');
                c2899.f11147.append((CharSequence) c3299.f12705);
                c2899.f11147.append((CharSequence) c3299.m7566());
                c2899.f11147.append('\n');
                if (z) {
                    c2899.f11143++;
                }
            } else {
                c2899.f11140.remove(c3299.f12705);
                c2899.f11147.append((CharSequence) "REMOVE");
                c2899.f11147.append(' ');
                c2899.f11147.append((CharSequence) c3299.f12705);
                c2899.f11147.append('\n');
            }
            m6947(c2899.f11147);
            if (c2899.f11139 > c2899.f11146 || c2899.m6952()) {
                c2899.f11137.submit(c2899.f11135);
            }
        }
    }

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public static C2899 m6951(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 {
                m6946(file2, file3, false);
            }
        }
        C2899 c2899 = new C2899(file, j);
        if (c2899.f11142.exists()) {
            try {
                c2899.m6953();
                c2899.m6955();
                return c2899;
            } catch (IOException e) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                c2899.close();
                AbstractC4756.m9935(c2899.f11138);
            }
        }
        file.mkdirs();
        C2899 c28992 = new C2899(file, j);
        c28992.m6954();
        return c28992;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final synchronized void close() {
        try {
            if (this.f11147 == null) {
                return;
            }
            ArrayList arrayList = new ArrayList(this.f11140.values());
            int size = arrayList.size();
            int i = 0;
            while (i < size) {
                Object obj = arrayList.get(i);
                i++;
                C0764 c0764 = ((C3299) obj).f12708;
                if (c0764 != null) {
                    c0764.m2748();
                }
            }
            m6957();
            m6948(this.f11147);
            this.f11147 = null;
        } catch (Throwable th) {
            throw th;
        }
    }

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public final boolean m6952() {
        int i = this.f11134;
        return i >= 2000 && i >= this.f11140.size();
    }

    /* JADX INFO: renamed from: ˈⁱᵡ, reason: contains not printable characters */
    public final void m6953() {
        File file = this.f11142;
        C0036 c0036 = new C0036(new FileInputStream(file), AbstractC4756.f18131);
        try {
            String strM1022 = c0036.m1022();
            String strM10222 = c0036.m1022();
            String strM10223 = c0036.m1022();
            String strM10224 = c0036.m1022();
            String strM10225 = c0036.m1022();
            if (!"libcore.io.DiskLruCache".equals(strM1022) || !"1".equals(strM10222) || !Integer.toString(this.f11145).equals(strM10223) || !Integer.toString(this.f11136).equals(strM10224) || !"".equals(strM10225)) {
                throw new IOException("unexpected journal header: [" + strM1022 + ", " + strM10222 + ", " + strM10224 + ", " + strM10225 + "]");
            }
            int i = 0;
            while (true) {
                try {
                    m6958(c0036.m1022());
                    i++;
                } catch (EOFException unused) {
                    this.f11134 = i - this.f11140.size();
                    if (c0036.f1184 == -1) {
                        m6954();
                    } else {
                        this.f11147 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), AbstractC4756.f18131));
                    }
                    try {
                        c0036.close();
                        return;
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } catch (Throwable th) {
            try {
                c0036.close();
            } catch (RuntimeException e2) {
                throw e2;
            } catch (Exception unused3) {
            }
            throw th;
        }
    }

    /* JADX INFO: renamed from: ˊˌˊ, reason: contains not printable characters */
    public final synchronized void m6954() {
        try {
            BufferedWriter bufferedWriter = this.f11147;
            if (bufferedWriter != null) {
                m6948(bufferedWriter);
            }
            BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f11141), AbstractC4756.f18131));
            try {
                bufferedWriter2.write("libcore.io.DiskLruCache");
                bufferedWriter2.write("\n");
                bufferedWriter2.write("1");
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f11145));
                bufferedWriter2.write("\n");
                bufferedWriter2.write(Integer.toString(this.f11136));
                bufferedWriter2.write("\n");
                bufferedWriter2.write("\n");
                for (C3299 c3299 : this.f11140.values()) {
                    if (c3299.f12708 != null) {
                        bufferedWriter2.write("DIRTY " + c3299.f12705 + '\n');
                    } else {
                        bufferedWriter2.write("CLEAN " + c3299.f12705 + c3299.m7566() + '\n');
                    }
                }
                m6948(bufferedWriter2);
                if (this.f11142.exists()) {
                    m6946(this.f11142, this.f11144, true);
                }
                m6946(this.f11141, this.f11142, false);
                this.f11144.delete();
                this.f11147 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f11142, true), AbstractC4756.f18131));
            } catch (Throwable th) {
                m6948(bufferedWriter2);
                throw th;
            }
        } catch (Throwable th2) {
            throw th2;
        }
    }

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public final void m6955() throws IOException {
        m6949(this.f11141);
        Iterator it = this.f11140.values().iterator();
        while (it.hasNext()) {
            C3299 c3299 = (C3299) it.next();
            C0764 c0764 = c3299.f12708;
            int i = this.f11136;
            int i2 = 0;
            if (c0764 == null) {
                while (i2 < i) {
                    this.f11139 += c3299.f12702[i2];
                    i2++;
                }
            } else {
                c3299.f12708 = null;
                while (i2 < i) {
                    m6949(c3299.f12703[i2]);
                    m6949(c3299.f12707[i2]);
                    i2++;
                }
                it.remove();
            }
        }
    }

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final synchronized ᵟˑʽ m6956(String str) {
        if (this.f11147 == null) {
            throw new IllegalStateException("cache is closed");
        }
        C3299 c3299 = (C3299) this.f11140.get(str);
        if (c3299 == null) {
            return null;
        }
        if (!c3299.f12704) {
            return null;
        }
        for (File file : c3299.f12703) {
            if (!file.exists()) {
                return null;
            }
        }
        this.f11134++;
        this.f11147.append((CharSequence) "READ");
        this.f11147.append(' ');
        this.f11147.append((CharSequence) str);
        this.f11147.append('\n');
        if (m6952()) {
            this.f11137.submit(this.f11135);
        }
        return new ᵟˑʽ(c3299.f12703);
    }

    /* JADX INFO: renamed from: ᵛˀˊ, reason: contains not printable characters */
    public final void m6957() {
        while (this.f11139 > this.f11146) {
            String str = (String) ((Map.Entry) this.f11140.entrySet().iterator().next()).getKey();
            synchronized (this) {
                try {
                    if (this.f11147 == null) {
                        throw new IllegalStateException("cache is closed");
                    }
                    C3299 c3299 = (C3299) this.f11140.get(str);
                    if (c3299 != null && c3299.f12708 == null) {
                        for (int i = 0; i < this.f11136; i++) {
                            File file = c3299.f12703[i];
                            if (file.exists() && !file.delete()) {
                                throw new IOException("failed to delete " + file);
                            }
                            long j = this.f11139;
                            long[] jArr = c3299.f12702;
                            this.f11139 = j - jArr[i];
                            jArr[i] = 0;
                        }
                        this.f11134++;
                        this.f11147.append((CharSequence) "REMOVE");
                        this.f11147.append(' ');
                        this.f11147.append((CharSequence) str);
                        this.f11147.append('\n');
                        this.f11140.remove(str);
                        if (m6952()) {
                            this.f11137.submit(this.f11135);
                        }
                    }
                } finally {
                }
            }
        }
    }

    /* JADX INFO: renamed from: ᵨˈʼﹶ, reason: contains not printable characters */
    public final void m6958(String str) {
        String strSubstring;
        int iIndexOf = str.indexOf(32);
        if (iIndexOf == -1) {
            C1685.m4568("unexpected journal line: ".concat(str));
            return;
        }
        int i = iIndexOf + 1;
        int iIndexOf2 = str.indexOf(32, i);
        LinkedHashMap linkedHashMap = this.f11140;
        if (iIndexOf2 == -1) {
            strSubstring = str.substring(i);
            if (iIndexOf == 6 && str.startsWith("REMOVE")) {
                linkedHashMap.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iIndexOf2);
        }
        C3299 c3299 = (C3299) linkedHashMap.get(strSubstring);
        if (c3299 == null) {
            c3299 = new C3299(this, strSubstring);
            linkedHashMap.put(strSubstring, c3299);
        }
        if (iIndexOf2 == -1 || iIndexOf != 5 || !str.startsWith("CLEAN")) {
            if (iIndexOf2 == -1 && iIndexOf == 5 && str.startsWith("DIRTY")) {
                c3299.f12708 = new C0764(this, c3299);
                return;
            } else {
                if (iIndexOf2 == -1 && iIndexOf == 4 && str.startsWith("READ")) {
                    return;
                }
                C1685.m4568("unexpected journal line: ".concat(str));
                return;
            }
        }
        String[] strArrSplit = str.substring(iIndexOf2 + 1).split(" ");
        c3299.f12704 = true;
        c3299.f12708 = null;
        if (strArrSplit.length != c3299.f12706.f11136) {
            ᵷᵝˎ.ᵶᵥᵝ(Arrays.toString(strArrSplit), "unexpected journal line: ");
            return;
        }
        for (int i2 = 0; i2 < strArrSplit.length; i2++) {
            try {
                c3299.f12702[i2] = Long.parseLong(strArrSplit[i2]);
            } catch (NumberFormatException unused) {
                ᵷᵝˎ.ᵶᵥᵝ(Arrays.toString(strArrSplit), "unexpected journal line: ");
                return;
            }
        }
    }

    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    public final C0764 m6959(String str) {
        synchronized (this) {
            try {
                if (this.f11147 == null) {
                    throw new IllegalStateException("cache is closed");
                }
                C3299 c3299 = (C3299) this.f11140.get(str);
                if (c3299 == null) {
                    c3299 = new C3299(this, str);
                    this.f11140.put(str, c3299);
                } else if (c3299.f12708 != null) {
                    return null;
                }
                C0764 c0764 = new C0764(this, c3299);
                c3299.f12708 = c0764;
                this.f11147.append((CharSequence) "DIRTY");
                this.f11147.append(' ');
                this.f11147.append((CharSequence) str);
                this.f11147.append('\n');
                m6947(this.f11147);
                return c0764;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
