package p138.p145.p146.p147;

import android.annotation.TargetApi;
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.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
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;
import p138.p139.p143.p144.C2803;

/* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C2821 implements Closeable {

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final File f8704;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final File f8705;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final File f8706;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final File f8707;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final int f8708;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public long f8709;

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public final int f8710;

    /* JADX INFO: renamed from: ދ, reason: contains not printable characters */
    public Writer f8712;

    /* JADX INFO: renamed from: ލ, reason: contains not printable characters */
    public int f8714;

    /* JADX INFO: renamed from: ފ, reason: contains not printable characters */
    public long f8711 = 0;

    /* JADX INFO: renamed from: ތ, reason: contains not printable characters */
    public final LinkedHashMap<String, C2825> f8713 = new LinkedHashMap<>(0, 0.75f, true);

    /* JADX INFO: renamed from: ގ, reason: contains not printable characters */
    public long f8715 = 0;

    /* JADX INFO: renamed from: ޏ, reason: contains not printable characters */
    public final ThreadPoolExecutor f8716 = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactoryC2823(null));

    /* JADX INFO: renamed from: ސ, reason: contains not printable characters */
    public final Callable<Void> f8717 = new CallableC2822();

    /* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏$֏, reason: contains not printable characters */
    public class CallableC2822 implements Callable<Void> {
        public CallableC2822() {
        }

        @Override // java.util.concurrent.Callable
        public /* bridge */ /* synthetic */ Void call() {
            call2();
            return null;
        }

        @Override // java.util.concurrent.Callable
        /* JADX INFO: renamed from: call, reason: avoid collision after fix types in other method */
        public Void call2() {
            synchronized (C2821.this) {
                if (C2821.this.f8712 == null) {
                    return null;
                }
                C2821.this.m4942();
                if (C2821.this.m4936()) {
                    C2821.this.m4941();
                    C2821.this.f8714 = 0;
                }
                return null;
            }
        }
    }

    /* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏$ؠ, reason: contains not printable characters */
    public static final class ThreadFactoryC2823 implements ThreadFactory {
        public /* synthetic */ ThreadFactoryC2823(CallableC2822 callableC2822) {
        }

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

    /* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏$ހ, reason: contains not printable characters */
    public final class C2824 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final C2825 f8719;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final boolean[] f8720;

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public boolean f8721;

        public /* synthetic */ C2824(C2825 c2825, CallableC2822 callableC2822) {
            this.f8719 = c2825;
            this.f8720 = c2825.f8727 ? null : new boolean[C2821.this.f8710];
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public File m4943(int i) {
            File file;
            synchronized (C2821.this) {
                if (this.f8719.f8728 != this) {
                    throw new IllegalStateException();
                }
                if (!this.f8719.f8727) {
                    this.f8720[i] = true;
                }
                file = this.f8719.f8726[i];
                if (!C2821.this.f8704.exists()) {
                    C2821.this.f8704.mkdirs();
                }
            }
            return file;
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public void m4944() {
            C2821.this.m4934(this, false);
        }
    }

    /* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏$ށ, reason: contains not printable characters */
    public final class C2825 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final String f8723;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final long[] f8724;

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public File[] f8725;

        /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
        public File[] f8726;

        /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
        public boolean f8727;

        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public C2824 f8728;

        /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
        public long f8729;

        public /* synthetic */ C2825(String str, CallableC2822 callableC2822) {
            this.f8723 = str;
            int i = C2821.this.f8710;
            this.f8724 = new long[i];
            this.f8725 = new File[i];
            this.f8726 = new File[i];
            StringBuilder sb = new StringBuilder(str);
            sb.append('.');
            int length = sb.length();
            for (int i2 = 0; i2 < C2821.this.f8710; i2++) {
                sb.append(i2);
                this.f8725[i2] = new File(C2821.this.f8704, sb.toString());
                sb.append(".tmp");
                this.f8726[i2] = new File(C2821.this.f8704, sb.toString());
                sb.setLength(length);
            }
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final IOException m4945(String[] strArr) throws IOException {
            StringBuilder sbM4889 = C2803.m4889("unexpected journal line: ");
            sbM4889.append(Arrays.toString(strArr));
            throw new IOException(sbM4889.toString());
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public String m4946() {
            StringBuilder sb = new StringBuilder();
            for (long j : this.f8724) {
                sb.append(' ');
                sb.append(j);
            }
            return sb.toString();
        }
    }

    /* JADX INFO: renamed from: ދ.ؠ.֏.މ.֏$ނ, reason: contains not printable characters */
    public final class C2826 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final File[] f8731;

        public /* synthetic */ C2826(C2821 c2821, String str, long j, File[] fileArr, long[] jArr, CallableC2822 callableC2822) {
            this.f8731 = fileArr;
        }
    }

    public C2821(File file, int i, int i2, long j) {
        this.f8704 = file;
        this.f8708 = i;
        this.f8705 = new File(file, "journal");
        this.f8706 = new File(file, "journal.tmp");
        this.f8707 = new File(file, "journal.bkp");
        this.f8710 = i2;
        this.f8709 = j;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static C2821 m4927(File file, int i, int i2, long j) throws IOException {
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        if (i2 <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        }
        File file2 = new File(file, "journal.bkp");
        if (file2.exists()) {
            File file3 = new File(file, "journal");
            if (file3.exists()) {
                file2.delete();
            } else {
                m4929(file2, file3, false);
            }
        }
        C2821 c2821 = new C2821(file, i, i2, j);
        if (c2821.f8705.exists()) {
            try {
                c2821.m4940();
                c2821.m4937();
                return c2821;
            } catch (IOException e) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                c2821.close();
                C2829.m4949(c2821.f8704);
            }
        }
        file.mkdirs();
        C2821 c28212 = new C2821(file, i, i2, j);
        c28212.m4941();
        return c28212;
    }

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

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

    @TargetApi(26)
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static void m4930(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);
        }
    }

    @TargetApi(26)
    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public static void m4931(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);
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() {
        if (this.f8712 == null) {
            return;
        }
        for (C2825 c2825 : new ArrayList(this.f8713.values())) {
            if (c2825.f8728 != null) {
                c2825.f8728.m4944();
            }
        }
        m4942();
        m4930(this.f8712);
        this.f8712 = null;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final synchronized C2824 m4932(String str, long j) {
        m4933();
        C2825 c2825 = this.f8713.get(str);
        CallableC2822 callableC2822 = null;
        if (j != -1 && (c2825 == null || c2825.f8729 != j)) {
            return null;
        }
        if (c2825 == null) {
            c2825 = new C2825(str, callableC2822);
            this.f8713.put(str, c2825);
        } else if (c2825.f8728 != null) {
            return null;
        }
        C2824 c2824 = new C2824(c2825, callableC2822);
        c2825.f8728 = c2824;
        this.f8712.append((CharSequence) "DIRTY");
        this.f8712.append(' ');
        this.f8712.append((CharSequence) str);
        this.f8712.append('\n');
        m4931(this.f8712);
        return c2824;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m4933() {
        if (this.f8712 == null) {
            throw new IllegalStateException("cache is closed");
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final synchronized void m4934(C2824 c2824, boolean z) {
        C2825 c2825 = c2824.f8719;
        if (c2825.f8728 != c2824) {
            throw new IllegalStateException();
        }
        if (z && !c2825.f8727) {
            for (int i = 0; i < this.f8710; i++) {
                if (!c2824.f8720[i]) {
                    c2824.m4944();
                    throw new IllegalStateException("Newly created entry didn't create value for index " + i);
                }
                if (!c2825.f8726[i].exists()) {
                    c2824.m4944();
                    return;
                }
            }
        }
        for (int i2 = 0; i2 < this.f8710; i2++) {
            File file = c2825.f8726[i2];
            if (!z) {
                m4928(file);
            } else if (file.exists()) {
                File file2 = c2825.f8725[i2];
                file.renameTo(file2);
                long j = c2825.f8724[i2];
                long length = file2.length();
                c2825.f8724[i2] = length;
                this.f8711 = (this.f8711 - j) + length;
            }
        }
        this.f8714++;
        c2825.f8728 = null;
        if (c2825.f8727 || z) {
            c2825.f8727 = true;
            this.f8712.append((CharSequence) "CLEAN");
            this.f8712.append(' ');
            this.f8712.append((CharSequence) c2825.f8723);
            this.f8712.append((CharSequence) c2825.m4946());
            this.f8712.append('\n');
            if (z) {
                long j2 = this.f8715;
                this.f8715 = 1 + j2;
                c2825.f8729 = j2;
            }
        } else {
            this.f8713.remove(c2825.f8723);
            this.f8712.append((CharSequence) "REMOVE");
            this.f8712.append(' ');
            this.f8712.append((CharSequence) c2825.f8723);
            this.f8712.append('\n');
        }
        m4931(this.f8712);
        if (this.f8711 > this.f8709 || m4936()) {
            this.f8716.submit(this.f8717);
        }
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public synchronized C2826 m4935(String str) {
        m4933();
        C2825 c2825 = this.f8713.get(str);
        if (c2825 == null) {
            return null;
        }
        if (!c2825.f8727) {
            return null;
        }
        for (File file : c2825.f8725) {
            if (!file.exists()) {
                return null;
            }
        }
        this.f8714++;
        this.f8712.append((CharSequence) "READ");
        this.f8712.append(' ');
        this.f8712.append((CharSequence) str);
        this.f8712.append('\n');
        if (m4936()) {
            this.f8716.submit(this.f8717);
        }
        return new C2826(this, str, c2825.f8729, c2825.f8725, c2825.f8724, null);
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final boolean m4936() {
        int i = this.f8714;
        return i >= 2000 && i >= this.f8713.size();
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final void m4937() throws IOException {
        m4928(this.f8706);
        Iterator<C2825> it = this.f8713.values().iterator();
        while (it.hasNext()) {
            C2825 next = it.next();
            int i = 0;
            if (next.f8728 == null) {
                while (i < this.f8710) {
                    this.f8711 += next.f8724[i];
                    i++;
                }
            } else {
                next.f8728 = null;
                while (i < this.f8710) {
                    m4928(next.f8725[i]);
                    m4928(next.f8726[i]);
                    i++;
                }
                it.remove();
            }
        }
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final void m4938(String str) throws IOException {
        String strSubstring;
        int iIndexOf = str.indexOf(32);
        if (iIndexOf == -1) {
            throw new IOException(C2803.m4879("unexpected journal line: ", str));
        }
        int i = iIndexOf + 1;
        int iIndexOf2 = str.indexOf(32, i);
        if (iIndexOf2 == -1) {
            strSubstring = str.substring(i);
            if (iIndexOf == 6 && str.startsWith("REMOVE")) {
                this.f8713.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i, iIndexOf2);
        }
        C2825 c2825 = this.f8713.get(strSubstring);
        CallableC2822 callableC2822 = null;
        if (c2825 == null) {
            c2825 = new C2825(strSubstring, callableC2822);
            this.f8713.put(strSubstring, c2825);
        }
        if (iIndexOf2 == -1 || iIndexOf != 5 || !str.startsWith("CLEAN")) {
            if (iIndexOf2 == -1 && iIndexOf == 5 && str.startsWith("DIRTY")) {
                c2825.f8728 = new C2824(c2825, callableC2822);
                return;
            } else {
                if (iIndexOf2 != -1 || iIndexOf != 4 || !str.startsWith("READ")) {
                    throw new IOException(C2803.m4879("unexpected journal line: ", str));
                }
                return;
            }
        }
        String[] strArrSplit = str.substring(iIndexOf2 + 1).split(" ");
        c2825.f8727 = true;
        c2825.f8728 = null;
        if (strArrSplit.length != C2821.this.f8710) {
            c2825.m4945(strArrSplit);
            throw null;
        }
        for (int i2 = 0; i2 < strArrSplit.length; i2++) {
            try {
                c2825.f8724[i2] = Long.parseLong(strArrSplit[i2]);
            } catch (NumberFormatException unused) {
                c2825.m4945(strArrSplit);
                throw null;
            }
        }
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public synchronized boolean m4939(String str) {
        m4933();
        C2825 c2825 = this.f8713.get(str);
        if (c2825 != null && c2825.f8728 == null) {
            for (int i = 0; i < this.f8710; i++) {
                File file = c2825.f8725[i];
                if (file.exists() && !file.delete()) {
                    throw new IOException("failed to delete " + file);
                }
                this.f8711 -= c2825.f8724[i];
                c2825.f8724[i] = 0;
            }
            this.f8714++;
            this.f8712.append((CharSequence) "REMOVE");
            this.f8712.append(' ');
            this.f8712.append((CharSequence) str);
            this.f8712.append('\n');
            this.f8713.remove(str);
            if (m4936()) {
                this.f8716.submit(this.f8717);
            }
            return true;
        }
        return false;
    }

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final void m4940() {
        C2827 c2827 = new C2827(new FileInputStream(this.f8705), C2829.f8738);
        try {
            String strM4948 = c2827.m4948();
            String strM49482 = c2827.m4948();
            String strM49483 = c2827.m4948();
            String strM49484 = c2827.m4948();
            String strM49485 = c2827.m4948();
            if (!"libcore.io.DiskLruCache".equals(strM4948) || !"1".equals(strM49482) || !Integer.toString(this.f8708).equals(strM49483) || !Integer.toString(this.f8710).equals(strM49484) || !"".equals(strM49485)) {
                throw new IOException("unexpected journal header: [" + strM4948 + ", " + strM49482 + ", " + strM49484 + ", " + strM49485 + "]");
            }
            int i = 0;
            while (true) {
                try {
                    m4938(c2827.m4948());
                    i++;
                } catch (EOFException unused) {
                    this.f8714 = i - this.f8713.size();
                    if (c2827.f8736 == -1) {
                        m4941();
                    } else {
                        this.f8712 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f8705, true), C2829.f8738));
                    }
                    try {
                        c2827.close();
                        return;
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } catch (Throwable th) {
            try {
                c2827.close();
            } catch (RuntimeException e2) {
                throw e2;
            } catch (Exception unused3) {
            }
            throw th;
        }
    }

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final synchronized void m4941() {
        if (this.f8712 != null) {
            m4930(this.f8712);
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f8706), C2829.f8738));
        try {
            bufferedWriter.write("libcore.io.DiskLruCache");
            bufferedWriter.write("\n");
            bufferedWriter.write("1");
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.f8708));
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.f8710));
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            for (C2825 c2825 : this.f8713.values()) {
                if (c2825.f8728 != null) {
                    bufferedWriter.write("DIRTY " + c2825.f8723 + '\n');
                } else {
                    bufferedWriter.write("CLEAN " + c2825.f8723 + c2825.m4946() + '\n');
                }
            }
            m4930(bufferedWriter);
            if (this.f8705.exists()) {
                m4929(this.f8705, this.f8707, true);
            }
            m4929(this.f8706, this.f8705, false);
            this.f8707.delete();
            this.f8712 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f8705, true), C2829.f8738));
        } catch (Throwable th) {
            m4930(bufferedWriter);
            throw th;
        }
    }

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final void m4942() {
        while (this.f8711 > this.f8709) {
            m4939(this.f8713.entrySet().iterator().next().getKey());
        }
    }
}
