package com.bumptech.glide.disklrucache; 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; /* loaded from: classes.dex */ public final class DiskLruCache implements Closeable { private final File a; private final File b; private final File c; private final File d; private final int e; private long f; private final int g; private Writer i; private int k; private long h = 0; private final LinkedHashMap j = new LinkedHashMap<>(0, 0.75f, true); private long l = 0; final ThreadPoolExecutor m = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new DiskLruCacheThreadFactory()); private final Callable n = new Callable() { // from class: com.bumptech.glide.disklrucache.DiskLruCache.1 @Override // java.util.concurrent.Callable public Void call() throws Exception { synchronized (DiskLruCache.this) { if (DiskLruCache.this.i == null) { return null; } DiskLruCache.this.g(); if (DiskLruCache.this.c()) { DiskLruCache.this.f(); DiskLruCache.this.k = 0; } return null; } } }; private static final class DiskLruCacheThreadFactory implements ThreadFactory { private DiskLruCacheThreadFactory() { } @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; } } public final class Editor { private final Entry a; private final boolean[] b; private boolean c; public void c() throws IOException { DiskLruCache.this.a(this, true); this.c = true; } private Editor(Entry entry) { this.a = entry; this.b = entry.e ? null : new boolean[DiskLruCache.this.g]; } public File a(int i) throws IOException { File b; synchronized (DiskLruCache.this) { if (this.a.f != this) { throw new IllegalStateException(); } if (!this.a.e) { this.b[i] = true; } b = this.a.b(i); if (!DiskLruCache.this.a.exists()) { DiskLruCache.this.a.mkdirs(); } } return b; } public void b() { if (this.c) { return; } try { a(); } catch (IOException unused) { } } public void a() throws IOException { DiskLruCache.this.a(this, false); } } private final class Entry { private final String a; private final long[] b; File[] c; File[] d; private boolean e; private Editor f; private long g; private Entry(String str) { this.a = str; this.b = new long[DiskLruCache.this.g]; this.c = new File[DiskLruCache.this.g]; this.d = new File[DiskLruCache.this.g]; StringBuilder sb = new StringBuilder(str); sb.append('.'); int length = sb.length(); for (int i = 0; i < DiskLruCache.this.g; i++) { sb.append(i); this.c[i] = new File(DiskLruCache.this.a, sb.toString()); sb.append(".tmp"); this.d[i] = new File(DiskLruCache.this.a, sb.toString()); sb.setLength(length); } } /* JADX INFO: Access modifiers changed from: private */ public void b(String[] strArr) throws IOException { if (strArr.length != DiskLruCache.this.g) { a(strArr); throw null; } for (int i = 0; i < strArr.length; i++) { try { this.b[i] = Long.parseLong(strArr[i]); } catch (NumberFormatException unused) { a(strArr); throw null; } } } public String a() throws IOException { StringBuilder sb = new StringBuilder(); for (long j : this.b) { sb.append(' '); sb.append(j); } return sb.toString(); } public File b(int i) { return this.d[i]; } private IOException a(String[] strArr) throws IOException { throw new IOException("unexpected journal line: " + Arrays.toString(strArr)); } public File a(int i) { return this.c[i]; } } public final class Value { private final File[] a; public File a(int i) { return this.a[i]; } private Value(DiskLruCache diskLruCache, String str, long j, File[] fileArr, long[] jArr) { this.a = fileArr; } } private DiskLruCache(File file, int i, int i2, long j) { this.a = file; this.e = i; this.b = new File(file, "journal"); this.c = new File(file, "journal.tmp"); this.d = new File(file, "journal.bkp"); this.g = i2; this.f = j; } /* JADX INFO: Access modifiers changed from: private */ public void g() throws IOException { while (this.h > this.f) { c(this.j.entrySet().iterator().next().getKey()); } } @Override // java.io.Closeable, java.lang.AutoCloseable public synchronized void close() throws IOException { if (this.i == null) { return; } Iterator it = new ArrayList(this.j.values()).iterator(); while (it.hasNext()) { Entry entry = (Entry) it.next(); if (entry.f != null) { entry.f.a(); } } g(); this.i.close(); this.i = null; } /* JADX INFO: Access modifiers changed from: private */ public boolean c() { int i = this.k; return i >= 2000 && i >= this.j.size(); } private void d(String str) throws IOException { String substring; int indexOf = str.indexOf(32); if (indexOf == -1) { throw new IOException("unexpected journal line: " + str); } int i = indexOf + 1; int indexOf2 = str.indexOf(32, i); if (indexOf2 == -1) { substring = str.substring(i); if (indexOf == 6 && str.startsWith("REMOVE")) { this.j.remove(substring); return; } } else { substring = str.substring(i, indexOf2); } Entry entry = this.j.get(substring); if (entry == null) { entry = new Entry(substring); this.j.put(substring, entry); } if (indexOf2 != -1 && indexOf == 5 && str.startsWith("CLEAN")) { String[] split = str.substring(indexOf2 + 1).split(" "); entry.e = true; entry.f = null; entry.b(split); return; } if (indexOf2 == -1 && indexOf == 5 && str.startsWith("DIRTY")) { entry.f = new Editor(entry); return; } if (indexOf2 == -1 && indexOf == 4 && str.startsWith("READ")) { return; } throw new IOException("unexpected journal line: " + str); } private void e() throws IOException { StrictLineReader strictLineReader = new StrictLineReader(new FileInputStream(this.b), Util.a); try { String b = strictLineReader.b(); String b2 = strictLineReader.b(); String b3 = strictLineReader.b(); String b4 = strictLineReader.b(); String b5 = strictLineReader.b(); if (!"libcore.io.DiskLruCache".equals(b) || !"1".equals(b2) || !Integer.toString(this.e).equals(b3) || !Integer.toString(this.g).equals(b4) || !"".equals(b5)) { throw new IOException("unexpected journal header: [" + b + ", " + b2 + ", " + b4 + ", " + b5 + "]"); } int i = 0; while (true) { try { d(strictLineReader.b()); i++; } catch (EOFException unused) { this.k = i - this.j.size(); if (strictLineReader.a()) { f(); } else { this.i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.b, true), Util.a)); } Util.a(strictLineReader); return; } } } catch (Throwable th) { Util.a(strictLineReader); throw th; } } /* JADX INFO: Access modifiers changed from: private */ public synchronized void f() throws IOException { if (this.i != null) { this.i.close(); } BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.c), Util.a)); try { bufferedWriter.write("libcore.io.DiskLruCache"); bufferedWriter.write("\n"); bufferedWriter.write("1"); bufferedWriter.write("\n"); bufferedWriter.write(Integer.toString(this.e)); bufferedWriter.write("\n"); bufferedWriter.write(Integer.toString(this.g)); bufferedWriter.write("\n"); bufferedWriter.write("\n"); for (Entry entry : this.j.values()) { if (entry.f != null) { bufferedWriter.write("DIRTY " + entry.a + '\n'); } else { bufferedWriter.write("CLEAN " + entry.a + entry.a() + '\n'); } } bufferedWriter.close(); if (this.b.exists()) { a(this.b, this.d, true); } a(this.c, this.b, false); this.d.delete(); this.i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.b, true), Util.a)); } catch (Throwable th) { bufferedWriter.close(); throw th; } } public synchronized Value b(String str) throws IOException { b(); Entry entry = this.j.get(str); if (entry == null) { return null; } if (!entry.e) { return null; } for (File file : entry.c) { if (!file.exists()) { return null; } } this.k++; this.i.append((CharSequence) "READ"); this.i.append(' '); this.i.append((CharSequence) str); this.i.append('\n'); if (c()) { this.m.submit(this.n); } return new Value(str, entry.g, entry.c, entry.b); } public static DiskLruCache a(File file, int i, int i2, long j) throws IOException { if (j <= 0) { throw new IllegalArgumentException("maxSize <= 0"); } 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 { a(file2, file3, false); } } DiskLruCache diskLruCache = new DiskLruCache(file, i, i2, j); if (diskLruCache.b.exists()) { try { diskLruCache.e(); diskLruCache.d(); return diskLruCache; } catch (IOException e) { System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing"); diskLruCache.a(); } } file.mkdirs(); DiskLruCache diskLruCache2 = new DiskLruCache(file, i, i2, j); diskLruCache2.f(); return diskLruCache2; } throw new IllegalArgumentException("valueCount <= 0"); } public synchronized boolean c(String str) throws IOException { b(); Entry entry = this.j.get(str); if (entry != null && entry.f == null) { for (int i = 0; i < this.g; i++) { File a = entry.a(i); if (a.exists() && !a.delete()) { throw new IOException("failed to delete " + a); } this.h -= entry.b[i]; entry.b[i] = 0; } this.k++; this.i.append((CharSequence) "REMOVE"); this.i.append(' '); this.i.append((CharSequence) str); this.i.append('\n'); this.j.remove(str); if (c()) { this.m.submit(this.n); } return true; } return false; } private void b() { if (this.i == null) { throw new IllegalStateException("cache is closed"); } } private void d() throws IOException { a(this.c); Iterator it = this.j.values().iterator(); while (it.hasNext()) { Entry next = it.next(); int i = 0; if (next.f != null) { next.f = null; while (i < this.g) { a(next.a(i)); a(next.b(i)); i++; } it.remove(); } else { while (i < this.g) { this.h += next.b[i]; i++; } } } } private static void a(File file) throws IOException { if (file.exists() && !file.delete()) { throw new IOException(); } } private static void a(File file, File file2, boolean z) throws IOException { if (z) { a(file2); } if (!file.renameTo(file2)) { throw new IOException(); } } public Editor a(String str) throws IOException { return a(str, -1L); } private synchronized Editor a(String str, long j) throws IOException { b(); Entry entry = this.j.get(str); if (j != -1 && (entry == null || entry.g != j)) { return null; } if (entry != null) { if (entry.f != null) { return null; } } else { entry = new Entry(str); this.j.put(str, entry); } Editor editor = new Editor(entry); entry.f = editor; this.i.append((CharSequence) "DIRTY"); this.i.append(' '); this.i.append((CharSequence) str); this.i.append('\n'); this.i.flush(); return editor; } /* JADX INFO: Access modifiers changed from: private */ public synchronized void a(Editor editor, boolean z) throws IOException { Entry entry = editor.a; if (entry.f == editor) { if (z && !entry.e) { for (int i = 0; i < this.g; i++) { if (editor.b[i]) { if (!entry.b(i).exists()) { editor.a(); return; } } else { editor.a(); throw new IllegalStateException("Newly created entry didn't create value for index " + i); } } } for (int i2 = 0; i2 < this.g; i2++) { File b = entry.b(i2); if (z) { if (b.exists()) { File a = entry.a(i2); b.renameTo(a); long j = entry.b[i2]; long length = a.length(); entry.b[i2] = length; this.h = (this.h - j) + length; } } else { a(b); } } this.k++; entry.f = null; if (!(entry.e | z)) { this.j.remove(entry.a); this.i.append((CharSequence) "REMOVE"); this.i.append(' '); this.i.append((CharSequence) entry.a); this.i.append('\n'); } else { entry.e = true; this.i.append((CharSequence) "CLEAN"); this.i.append(' '); this.i.append((CharSequence) entry.a); this.i.append((CharSequence) entry.a()); this.i.append('\n'); if (z) { long j2 = this.l; this.l = 1 + j2; entry.g = j2; } } this.i.flush(); if (this.h > this.f || c()) { this.m.submit(this.n); } return; } throw new IllegalStateException(); } public void a() throws IOException { close(); Util.a(this.a); } }