package ඓ.ῲ.K.ᨨ;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.StrictMode;
import com.facebook.appevents.AppEventsConstants;
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.PrintStream;
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 ඓ.K.K.K.C1293;

/* renamed from: ඓ.ῲ.K.ᨨ.K  reason: contains not printable characters */
/* compiled from: DiskLruCache */
public final class C1155 implements Closeable {

    /* renamed from: ʬ  reason: contains not printable characters */
    public final File f4637;

    /* renamed from: Ԕ  reason: contains not printable characters */
    public final ThreadPoolExecutor f4638 = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactoryC1158(null));

    /* renamed from: ඓ  reason: contains not printable characters */
    public final File f4639;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public final LinkedHashMap<String, C1160> f4640 = new LinkedHashMap<>(0, 0.75f, true);

    /* renamed from: ᨨ  reason: contains not printable characters */
    public long f4641 = 0;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final File f4642;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public long f4643;

    /* renamed from: 㑃  reason: contains not printable characters */
    public int f4644;

    /* renamed from: 㗗  reason: contains not printable characters */
    public final Callable<Void> f4645 = new CallableC1159();

    /* renamed from: 㳳  reason: contains not printable characters */
    public Writer f4646;

    /* renamed from: 㴹  reason: contains not printable characters */
    public final int f4647;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final File f4648;

    /* renamed from: 㿎  reason: contains not printable characters */
    public final int f4649;

    /* renamed from: 䁗  reason: contains not printable characters */
    public long f4650 = 0;

    /* renamed from: ඓ.ῲ.K.ᨨ.K$ඓ  reason: contains not printable characters */
    /* compiled from: DiskLruCache */
    public final class C1157 {

        /* renamed from: K  reason: contains not printable characters */
        public final File[] f4655;

        public /* synthetic */ C1157(C1155 k, String str, long j, File[] fileArr, long[] jArr, CallableC1159 k2) {
            this.f4655 = fileArr;
        }
    }

    /* renamed from: ඓ.ῲ.K.ᨨ.K$ῲ  reason: contains not printable characters */
    /* compiled from: DiskLruCache */
    public static final class ThreadFactoryC1158 implements ThreadFactory {
        public /* synthetic */ ThreadFactoryC1158(CallableC1159 k) {
        }

        public synchronized Thread newThread(Runnable runnable) {
            Thread thread;
            thread = new Thread(runnable, "glide-disk-lru-cache-thread");
            thread.setPriority(1);
            return thread;
        }
    }

    /* renamed from: ඓ.ῲ.K.ᨨ.K$K  reason: contains not printable characters */
    /* compiled from: DiskLruCache */
    public class CallableC1159 implements Callable<Void> {
        public CallableC1159() {
        }

        @Override // java.util.concurrent.Callable
        public Void call() throws Exception {
            synchronized (C1155.this) {
                if (C1155.this.f4646 == null) {
                    return null;
                }
                C1155.this.m3089();
                if (C1155.this.m3096()) {
                    C1155.this.m3098();
                    C1155.this.f4644 = 0;
                }
                return null;
            }
        }
    }

    public C1155(File file, int i, int i2, long j) {
        this.f4642 = file;
        this.f4647 = i;
        this.f4637 = new File(file, "journal");
        this.f4648 = new File(file, "journal.tmp");
        this.f4639 = new File(file, "journal.bkp");
        this.f4649 = i2;
        this.f4643 = j;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() throws IOException {
        if (this.f4646 != null) {
            Iterator it2 = new ArrayList(this.f4640.values()).iterator();
            while (it2.hasNext()) {
                C1160 r1 = (C1160) it2.next();
                if (r1.f4662 != null) {
                    r1.f4662.m3100();
                }
            }
            m3089();
            m3086(this.f4646);
            this.f4646 = null;
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public final void m3088(String str) throws IOException {
        String str2;
        int indexOf = str.indexOf(32);
        if (indexOf != -1) {
            int i = indexOf + 1;
            int indexOf2 = str.indexOf(32, i);
            if (indexOf2 == -1) {
                str2 = str.substring(i);
                if (indexOf == 6 && str.startsWith("REMOVE")) {
                    this.f4640.remove(str2);
                    return;
                }
            } else {
                str2 = str.substring(i, indexOf2);
            }
            C1160 r5 = this.f4640.get(str2);
            if (r5 == null) {
                r5 = new C1160(str2, null);
                this.f4640.put(str2, r5);
            }
            if (indexOf2 != -1 && indexOf == 5 && str.startsWith("CLEAN")) {
                String[] split = str.substring(indexOf2 + 1).split(" ");
                r5.f4658 = true;
                r5.f4662 = null;
                r5.m3101(split);
            } else if (indexOf2 == -1 && indexOf == 5 && str.startsWith("DIRTY")) {
                r5.f4662 = new C1156(r5, null);
            } else if (indexOf2 != -1 || indexOf != 4 || !str.startsWith("READ")) {
                throw new IOException(C1293.m3412("unexpected journal line: ", str));
            }
        } else {
            throw new IOException(C1293.m3412("unexpected journal line: ", str));
        }
    }

    /* renamed from: Ԕ  reason: contains not printable characters */
    public final void m3089() throws IOException {
        while (this.f4641 > this.f4643) {
            m3097(this.f4640.entrySet().iterator().next().getKey());
        }
    }

    /* renamed from: ᕣ  reason: contains not printable characters */
    public final void m3090() throws IOException {
        m3084(this.f4648);
        Iterator<C1160> it2 = this.f4640.values().iterator();
        while (it2.hasNext()) {
            C1160 next = it2.next();
            int i = 0;
            if (next.f4662 == null) {
                while (i < this.f4649) {
                    this.f4641 += next.f4659[i];
                    i++;
                }
            } else {
                next.f4662 = null;
                while (i < this.f4649) {
                    m3084(next.f4657[i]);
                    m3084(next.f4663[i]);
                    i++;
                }
                it2.remove();
            }
        }
    }

    /* renamed from: ᨨ  reason: contains not printable characters */
    public final void m3091() {
        if (this.f4646 == null) {
            throw new IllegalStateException("cache is closed");
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public synchronized C1157 m3092(String str) throws IOException {
        m3091();
        C1160 r0 = this.f4640.get(str);
        if (r0 == null) {
            return null;
        }
        if (!r0.f4658) {
            return null;
        }
        for (File file : r0.f4657) {
            if (!file.exists()) {
                return null;
            }
        }
        this.f4644++;
        this.f4646.append((CharSequence) "READ");
        this.f4646.append(' ');
        this.f4646.append((CharSequence) str);
        this.f4646.append('\n');
        if (m3096()) {
            this.f4638.submit(this.f4645);
        }
        return new C1157(this, str, r0.f4660, r0.f4657, r0.f4659, null);
    }

    /* renamed from: 㑃  reason: contains not printable characters */
    public final void m3095() throws IOException {
        C1153 r1 = new C1153(new FileInputStream(this.f4637), C1152.f4630);
        try {
            String r2 = r1.m3081();
            String r3 = r1.m3081();
            String r4 = r1.m3081();
            String r5 = r1.m3081();
            String r6 = r1.m3081();
            if (!"libcore.io.DiskLruCache".equals(r2) || !AppEventsConstants.EVENT_PARAM_VALUE_YES.equals(r3) || !Integer.toString(this.f4647).equals(r4) || !Integer.toString(this.f4649).equals(r5) || !"".equals(r6)) {
                throw new IOException("unexpected journal header: [" + r2 + ", " + r3 + ", " + r5 + ", " + r6 + "]");
            }
            boolean z = false;
            int i = 0;
            while (true) {
                try {
                    m3088(r1.m3081());
                    i++;
                } catch (EOFException unused) {
                    this.f4644 = i - this.f4640.size();
                    if (r1.f4634 == -1) {
                        z = true;
                    }
                    if (z) {
                        m3098();
                    } else {
                        this.f4646 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f4637, true), C1152.f4630));
                    }
                    try {
                        return;
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception unused2) {
                        return;
                    }
                }
            }
        } finally {
            try {
                r1.close();
            } catch (RuntimeException e2) {
                throw e2;
            } catch (Exception unused3) {
            }
        }
    }

    /* renamed from: 㳳  reason: contains not printable characters */
    public final boolean m3096() {
        int i = this.f4644;
        return i >= 2000 && i >= this.f4640.size();
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public synchronized boolean m3097(String str) throws IOException {
        m3091();
        C1160 r0 = this.f4640.get(str);
        if (r0 != null) {
            if (r0.f4662 == null) {
                for (int i = 0; i < this.f4649; i++) {
                    File file = r0.f4657[i];
                    if (file.exists()) {
                        if (!file.delete()) {
                            throw new IOException("failed to delete " + file);
                        }
                    }
                    long j = this.f4641;
                    long[] jArr = r0.f4659;
                    this.f4641 = j - jArr[i];
                    jArr[i] = 0;
                }
                this.f4644++;
                this.f4646.append((CharSequence) "REMOVE");
                this.f4646.append(' ');
                this.f4646.append((CharSequence) str);
                this.f4646.append('\n');
                this.f4640.remove(str);
                if (m3096()) {
                    this.f4638.submit(this.f4645);
                }
                return true;
            }
        }
        return false;
    }

    /* JADX INFO: finally extract failed */
    /* renamed from: 䁗  reason: contains not printable characters */
    public final synchronized void m3098() throws IOException {
        if (this.f4646 != null) {
            m3086(this.f4646);
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f4648), C1152.f4630));
        try {
            bufferedWriter.write("libcore.io.DiskLruCache");
            bufferedWriter.write("\n");
            bufferedWriter.write(AppEventsConstants.EVENT_PARAM_VALUE_YES);
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.f4647));
            bufferedWriter.write("\n");
            bufferedWriter.write(Integer.toString(this.f4649));
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            for (C1160 r2 : this.f4640.values()) {
                if (r2.f4662 != null) {
                    bufferedWriter.write("DIRTY " + r2.f4661 + '\n');
                } else {
                    bufferedWriter.write("CLEAN " + r2.f4661 + r2.m3103() + '\n');
                }
            }
            m3086(bufferedWriter);
            if (this.f4637.exists()) {
                m3085(this.f4637, this.f4639, true);
            }
            m3085(this.f4648, this.f4637, false);
            this.f4639.delete();
            this.f4646 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f4637, true), C1152.f4630));
        } catch (Throwable th) {
            m3086(bufferedWriter);
            throw th;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public static C1155 m3083(File file, int i, int i2, long j) throws IOException {
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        } else if (i2 > 0) {
            File file2 = new File(file, "journal.bkp");
            if (file2.exists()) {
                File file3 = new File(file, "journal");
                if (file3.exists()) {
                    file2.delete();
                } else {
                    m3085(file2, file3, false);
                }
            }
            C1155 k = new C1155(file, i, i2, j);
            if (k.f4637.exists()) {
                try {
                    k.m3095();
                    k.m3090();
                    return k;
                } catch (IOException e) {
                    PrintStream printStream = System.out;
                    printStream.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                    k.close();
                    C1152.m3079(k.f4642);
                }
            }
            file.mkdirs();
            C1155 k2 = new C1155(file, i, i2, j);
            k2.m3098();
            return k2;
        } else {
            throw new IllegalArgumentException("valueCount <= 0");
        }
    }

    /* renamed from: ඓ.ῲ.K.ᨨ.K$㸾  reason: contains not printable characters */
    /* compiled from: DiskLruCache */
    public final class C1160 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public File[] f4657;

        /* renamed from: ඓ  reason: contains not printable characters */
        public boolean f4658;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final long[] f4659;

        /* renamed from: ⁱ  reason: contains not printable characters */
        public long f4660;

        /* renamed from: K  reason: contains not printable characters */
        public final String f4661;

        /* renamed from: 㴹  reason: contains not printable characters */
        public C1156 f4662;

        /* renamed from: 㸾  reason: contains not printable characters */
        public File[] f4663;

        public /* synthetic */ C1160(String str, CallableC1159 k) {
            this.f4661 = str;
            int i = C1155.this.f4649;
            this.f4659 = new long[i];
            this.f4657 = new File[i];
            this.f4663 = new File[i];
            StringBuilder sb = new StringBuilder(str);
            sb.append('.');
            int length = sb.length();
            for (int i2 = 0; i2 < C1155.this.f4649; i2++) {
                sb.append(i2);
                this.f4657[i2] = new File(C1155.this.f4642, sb.toString());
                sb.append(".tmp");
                this.f4663[i2] = new File(C1155.this.f4642, sb.toString());
                sb.setLength(length);
            }
        }

        /* renamed from: ῲ  reason: contains not printable characters */
        public final void m3101(String[] strArr) throws IOException {
            if (strArr.length == C1155.this.f4649) {
                for (int i = 0; i < strArr.length; i++) {
                    try {
                        this.f4659[i] = Long.parseLong(strArr[i]);
                    } catch (NumberFormatException unused) {
                        m3102(strArr);
                        throw null;
                    }
                }
                return;
            }
            m3102(strArr);
            throw null;
        }

        /* renamed from: K  reason: contains not printable characters */
        public String m3103() throws IOException {
            StringBuilder sb = new StringBuilder();
            long[] jArr = this.f4659;
            for (long j : jArr) {
                sb.append(' ');
                sb.append(j);
            }
            return sb.toString();
        }

        /* renamed from: K  reason: contains not printable characters */
        public final IOException m3102(String[] strArr) throws IOException {
            StringBuilder r1 = C1293.m3416("unexpected journal line: ");
            r1.append(Arrays.toString(strArr));
            throw new IOException(r1.toString());
        }
    }

    /* renamed from: ඓ.ῲ.K.ᨨ.K$ʬ  reason: contains not printable characters */
    /* compiled from: DiskLruCache */
    public final class C1156 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public boolean f4651;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final boolean[] f4652;

        /* renamed from: K  reason: contains not printable characters */
        public final C1160 f4653;

        public /* synthetic */ C1156(C1160 r2, CallableC1159 k) {
            boolean[] zArr;
            this.f4653 = r2;
            if (r2.f4658) {
                zArr = null;
            } else {
                zArr = new boolean[C1155.this.f4649];
            }
            this.f4652 = zArr;
        }

        /* renamed from: K  reason: contains not printable characters */
        public File m3099(int i) throws IOException {
            File file;
            synchronized (C1155.this) {
                if (this.f4653.f4662 == this) {
                    if (!this.f4653.f4658) {
                        this.f4652[i] = true;
                    }
                    file = this.f4653.f4663[i];
                    if (!C1155.this.f4642.exists()) {
                        C1155.this.f4642.mkdirs();
                    }
                } else {
                    throw new IllegalStateException();
                }
            }
            return file;
        }

        /* renamed from: K  reason: contains not printable characters */
        public void m3100() throws IOException {
            C1155.this.m3094((C1155) this, (C1156) false);
        }
    }

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

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

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

    /* renamed from: K  reason: contains not printable characters */
    public final synchronized C1156 m3093(String str, long j) throws IOException {
        m3091();
        C1160 r0 = this.f4640.get(str);
        if (j != -1 && (r0 == null || r0.f4660 != j)) {
            return null;
        }
        if (r0 == null) {
            r0 = new C1160(str, null);
            this.f4640.put(str, r0);
        } else if (r0.f4662 != null) {
            return null;
        }
        C1156 r7 = new C1156(r0, null);
        r0.f4662 = r7;
        this.f4646.append((CharSequence) "DIRTY");
        this.f4646.append(' ');
        this.f4646.append((CharSequence) str);
        this.f4646.append('\n');
        m3082(this.f4646);
        return r7;
    }

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

    /* renamed from: K  reason: contains not printable characters */
    public final synchronized void m3094(C1156 r10, boolean z) throws IOException {
        C1160 r0 = r10.f4653;
        if (r0.f4662 == r10) {
            if (z && !r0.f4658) {
                for (int i = 0; i < this.f4649; i++) {
                    if (!r10.f4652[i]) {
                        r10.m3100();
                        throw new IllegalStateException("Newly created entry didn't create value for index " + i);
                    } else if (!r0.f4663[i].exists()) {
                        r10.m3100();
                        return;
                    }
                }
            }
            for (int i2 = 0; i2 < this.f4649; i2++) {
                File file = r0.f4663[i2];
                if (!z) {
                    m3084(file);
                } else if (file.exists()) {
                    File file2 = r0.f4657[i2];
                    file.renameTo(file2);
                    long j = r0.f4659[i2];
                    long length = file2.length();
                    r0.f4659[i2] = length;
                    this.f4641 = (this.f4641 - j) + length;
                }
            }
            this.f4644++;
            r0.f4662 = null;
            if (r0.f4658 || z) {
                r0.f4658 = true;
                this.f4646.append((CharSequence) "CLEAN");
                this.f4646.append(' ');
                this.f4646.append((CharSequence) r0.f4661);
                this.f4646.append((CharSequence) r0.m3103());
                this.f4646.append('\n');
                if (z) {
                    long j2 = this.f4650;
                    this.f4650 = 1 + j2;
                    r0.f4660 = j2;
                }
            } else {
                this.f4640.remove(r0.f4661);
                this.f4646.append((CharSequence) "REMOVE");
                this.f4646.append(' ');
                this.f4646.append((CharSequence) r0.f4661);
                this.f4646.append('\n');
            }
            m3082(this.f4646);
            if (this.f4641 > this.f4643 || m3096()) {
                this.f4638.submit(this.f4645);
            }
            return;
        }
        throw new IllegalStateException();
    }
}
