package com.fyber.inneractive.sdk.player.cache; import com.fyber.inneractive.sdk.util.IAlog; import com.ironsource.v8; import csdk.gluads.Consts; import java.io.BufferedWriter; import java.io.Closeable; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; 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.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; /* loaded from: classes2.dex */ public final class c implements Closeable { public static final Pattern p = Pattern.compile("[a-z0-9_-]{1,120}"); public static final b q = new b(); public final File a; public final File b; public final File c; public final File d; public BufferedWriter i; public int k; public e l; public long h = 0; public final LinkedHashMap j = new LinkedHashMap<>(0, 0.75f, true); public long m = 0; public final ThreadPoolExecutor n = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue()); public final a o = new a(); public final int e = 0; public final int g = 1; public final long f = 52428800; public class a implements Callable { public a() { } @Override // java.util.concurrent.Callable public final Void call() throws Exception { synchronized (c.this) { try { c cVar = c.this; if (cVar.i == null) { return null; } cVar.e(); c cVar2 = c.this; int i = cVar2.k; if (i >= 2000 && i >= cVar2.j.size()) { c.this.d(); c.this.k = 0; } return null; } finally { } } } } public class b extends OutputStream { @Override // java.io.OutputStream public final void write(int i) throws IOException { } } /* renamed from: com.fyber.inneractive.sdk.player.cache.c$c, reason: collision with other inner class name */ public final class C0081c { public final d a; public final boolean[] b; public boolean c; /* renamed from: com.fyber.inneractive.sdk.player.cache.c$c$a */ public class a extends FilterOutputStream { @Override // java.io.FilterOutputStream, java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public final void close() { try { ((FilterOutputStream) this).out.close(); } catch (IOException unused) { C0081c.this.c = true; } } @Override // java.io.FilterOutputStream, java.io.OutputStream, java.io.Flushable public final void flush() { try { ((FilterOutputStream) this).out.flush(); } catch (IOException unused) { C0081c.this.c = true; } } @Override // java.io.FilterOutputStream, java.io.OutputStream public final void write(int i) { try { ((FilterOutputStream) this).out.write(i); } catch (IOException unused) { C0081c.this.c = true; } } public a(FileOutputStream fileOutputStream) { super(fileOutputStream); } @Override // java.io.FilterOutputStream, java.io.OutputStream public final void write(byte[] bArr, int i, int i2) { try { ((FilterOutputStream) this).out.write(bArr, i, i2); } catch (IOException unused) { C0081c.this.c = true; } } } public C0081c(d dVar) { this.a = dVar; this.b = dVar.c ? null : new boolean[c.this.g]; } public final void a() throws IOException { c.a(c.this, this, false); } public final OutputStream b() throws IOException { FileOutputStream fileOutputStream; a aVar; c cVar = c.this; if (cVar.g <= 0) { throw new IllegalArgumentException("Expected index 0 to be greater than 0 and less than the maximum value count of " + c.this.g); } synchronized (cVar) { try { d dVar = this.a; if (dVar.d != this) { throw new IllegalStateException(); } if (!dVar.c) { this.b[0] = true; } File b = dVar.b(0); try { fileOutputStream = new FileOutputStream(b); } catch (FileNotFoundException unused) { c.this.a.mkdirs(); try { fileOutputStream = new FileOutputStream(b); } catch (FileNotFoundException unused2) { return c.q; } } aVar = new a(fileOutputStream); } catch (Throwable th) { throw th; } } return aVar; } } public final class d { public final String a; public final long[] b; public boolean c; public C0081c d; public /* synthetic */ d() { throw null; } public d(String str) { this.a = str; this.b = new long[c.this.g]; } public final File b(int i) { return new File(c.this.a, this.a + Consts.STRING_PERIOD + i + ".tmp"); } public final File a(int i) { return new File(c.this.a, this.a + Consts.STRING_PERIOD + i); } } public interface e { } public final class f implements Closeable { public final InputStream[] a; public f(InputStream[] inputStreamArr) { this.a = inputStreamArr; } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() { for (InputStream inputStream : this.a) { o.a(inputStream); } } } public c(File file) { this.a = file; this.b = new File(file, "journal"); this.c = new File(file, "journal.tmp"); this.d = new File(file, "journal.bkp"); } public static c b(File file) throws IOException { File file2 = new File(file, "journal.bkp"); if (file2.exists()) { File file3 = new File(file, "journal"); if (file3.exists()) { file2.delete(); } else if (!file2.renameTo(file3)) { throw new IOException(); } } c cVar = new c(file); if (cVar.b.exists()) { try { cVar.c(); cVar.b(); return cVar; } catch (IOException e2) { System.out.println("DiskLruCache " + file + " is corrupt: " + e2.getMessage() + ", removing"); IAlog.e("DiskLruCache delete cache", new Object[0]); cVar.close(); o.a(cVar.a); } } file.mkdirs(); c cVar2 = new c(file); cVar2.d(); return cVar2; } public final void c() throws IOException { n nVar = new n(new FileInputStream(this.b), o.a); try { String a2 = nVar.a(); String a3 = nVar.a(); String a4 = nVar.a(); String a5 = nVar.a(); String a6 = nVar.a(); if (!"libcore.io.DiskLruCache".equals(a2) || !"1".equals(a3) || !Integer.toString(this.e).equals(a4) || !Integer.toString(this.g).equals(a5) || !"".equals(a6)) { throw new IOException("unexpected journal header: [" + a2 + ", " + a3 + ", " + a5 + ", " + a6 + v8.i.e); } int i = 0; while (true) { try { c(nVar.a()); i++; } catch (EOFException unused) { this.k = i - this.j.size(); if (nVar.e == -1) { d(); } else { this.i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.b, true), o.a)); } o.a(nVar); return; } } } catch (Throwable th) { o.a(nVar); throw th; } } @Override // java.io.Closeable, java.lang.AutoCloseable public final synchronized void close() throws IOException { try { if (this.i == null) { return; } Iterator it = new ArrayList(this.j.values()).iterator(); while (it.hasNext()) { C0081c c0081c = ((d) it.next()).d; if (c0081c != null) { c0081c.a(); } } e(); this.i.close(); this.i = null; } catch (Throwable th) { throw th; } } public final synchronized void flush() throws IOException { a(); e(); this.i.flush(); } public static void a(c cVar, C0081c c0081c, boolean z) throws IOException { int i; synchronized (cVar) { d dVar = c0081c.a; if (dVar.d != c0081c) { throw new IllegalStateException(); } if (z && !dVar.c) { for (int i2 = 0; i2 < cVar.g; i2++) { if (!c0081c.b[i2]) { c0081c.a(); throw new IllegalStateException("Newly created entry didn't create value for index " + i2); } if (!dVar.b(i2).exists()) { c0081c.a(); return; } } } for (int i3 = 0; i3 < cVar.g; i3++) { File b2 = dVar.b(i3); if (!z) { a(b2); } else if (b2.exists()) { File a2 = dVar.a(i3); b2.renameTo(a2); long j = dVar.b[i3]; long length = a2.length(); dVar.b[i3] = length; cVar.h = (cVar.h - j) + length; } } cVar.k++; dVar.d = null; if (dVar.c || z) { dVar.c = true; BufferedWriter bufferedWriter = cVar.i; StringBuilder sb = new StringBuilder("CLEAN "); sb.append(dVar.a); StringBuilder sb2 = new StringBuilder(); for (long j2 : dVar.b) { sb2.append(' '); sb2.append(j2); } sb.append(sb2.toString()); sb.append('\n'); bufferedWriter.write(sb.toString()); if (z) { cVar.m++; } } else { cVar.j.remove(dVar.a); cVar.i.write("REMOVE " + dVar.a + '\n'); } cVar.i.flush(); if (cVar.h > cVar.f || ((i = cVar.k) >= 2000 && i >= cVar.j.size())) { cVar.n.submit(cVar.o); } } } public final synchronized void d() throws IOException { try { BufferedWriter bufferedWriter = this.i; if (bufferedWriter != null) { bufferedWriter.close(); } BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.c), o.a)); try { bufferedWriter2.write("libcore.io.DiskLruCache"); bufferedWriter2.write("\n"); bufferedWriter2.write("1"); bufferedWriter2.write("\n"); bufferedWriter2.write(Integer.toString(this.e)); bufferedWriter2.write("\n"); bufferedWriter2.write(Integer.toString(this.g)); bufferedWriter2.write("\n"); bufferedWriter2.write("\n"); for (d dVar : this.j.values()) { if (dVar.d != null) { bufferedWriter2.write("DIRTY " + dVar.a + '\n'); } else { StringBuilder sb = new StringBuilder(); sb.append("CLEAN "); sb.append(dVar.a); StringBuilder sb2 = new StringBuilder(); for (long j : dVar.b) { sb2.append(' '); sb2.append(j); } sb.append(sb2.toString()); sb.append('\n'); bufferedWriter2.write(sb.toString()); } } bufferedWriter2.close(); if (this.b.exists()) { File file = this.b; File file2 = this.d; a(file2); if (!file.renameTo(file2)) { throw new IOException(); } } if (!this.c.renameTo(this.b)) { throw new IOException(); } this.d.delete(); this.i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.b, true), o.a)); } catch (Throwable th) { bufferedWriter2.close(); throw th; } } catch (Throwable th2) { throw th2; } } public final void e() throws IOException { while (this.h > this.f) { String key = this.j.entrySet().iterator().next().getKey(); e eVar = this.l; if (eVar != null) { Iterator it = ((p) eVar).d.iterator(); while (true) { if (!it.hasNext()) { d(key); break; } if (((String) it.next()).equalsIgnoreCase(key)) { boolean z = false; for (String str : this.j.keySet()) { Iterator it2 = ((p) this.l).d.iterator(); while (true) { if (it2.hasNext()) { if (((String) it2.next()).equalsIgnoreCase(str)) { break; } } else { z |= d(str); break; } } } if (!z) { return; } } } } else { d(key); } } } public static void e(String str) { if (p.matcher(str).matches()) { return; } throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,120}: \"" + str + "\""); } public final void b() throws IOException { a(this.c); Iterator it = this.j.values().iterator(); while (it.hasNext()) { d next = it.next(); int i = 0; if (next.d == null) { while (i < this.g) { this.h += next.b[i]; i++; } } else { next.d = null; while (i < this.g) { a(next.a(i)); a(next.b(i)); i++; } it.remove(); } } } public final synchronized f b(String str) throws IOException { InputStream inputStream; a(); e(str); d dVar = this.j.get(str); if (dVar == null) { return null; } if (!dVar.c) { return null; } InputStream[] inputStreamArr = new InputStream[this.g]; for (int i = 0; i < this.g; i++) { try { inputStreamArr[i] = new FileInputStream(dVar.a(i)); } catch (FileNotFoundException unused) { for (int i2 = 0; i2 < this.g && (inputStream = inputStreamArr[i2]) != null; i2++) { o.a(inputStream); } return null; } } this.k++; this.i.append((CharSequence) ("READ " + str + '\n')); int i3 = this.k; if (i3 >= 2000 && i3 >= this.j.size()) { this.n.submit(this.o); } return new f(inputStreamArr); } public final synchronized boolean d(String str) throws IOException { try { IAlog.e("DiskLruCache remove %s", str); a(); e(str); d dVar = this.j.get(str); if (dVar != null && dVar.d == null) { for (int i = 0; i < this.g; i++) { File a2 = dVar.a(i); if (a2.exists() && !a2.delete()) { throw new IOException("failed to delete " + a2); } long j = this.h; long[] jArr = dVar.b; this.h = j - jArr[i]; jArr[i] = 0; } this.k++; this.i.append((CharSequence) ("REMOVE " + str + '\n')); this.j.remove(str); int i2 = this.k; if (i2 >= 2000 && i2 >= this.j.size()) { this.n.submit(this.o); } return true; } return false; } catch (Throwable th) { throw th; } } public final void c(String str) throws IOException { String substring; int indexOf = str.indexOf(32); if (indexOf != -1) { 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); } d dVar = this.j.get(substring); if (dVar == null) { dVar = new d(substring); this.j.put(substring, dVar); } if (indexOf2 != -1 && indexOf == 5 && str.startsWith("CLEAN")) { String[] split = str.substring(indexOf2 + 1).split(" "); dVar.c = true; dVar.d = null; if (split.length == c.this.g) { for (int i2 = 0; i2 < split.length; i2++) { try { dVar.b[i2] = Long.parseLong(split[i2]); } catch (NumberFormatException unused) { throw new IOException("unexpected journal line: " + Arrays.toString(split)); } } return; } throw new IOException("unexpected journal line: " + Arrays.toString(split)); } if (indexOf2 == -1 && indexOf == 5 && str.startsWith("DIRTY")) { dVar.d = new C0081c(dVar); return; } else { if (indexOf2 != -1 || indexOf != 4 || !str.startsWith("READ")) { throw new IOException("unexpected journal line: ".concat(str)); } return; } } throw new IOException("unexpected journal line: ".concat(str)); } public static void a(File file) throws IOException { IAlog.e("DiskLruCache deleteIfExists - %s", file); if (file.exists() && !file.delete()) { throw new IOException(); } } public final C0081c a(String str) throws IOException { synchronized (this) { try { a(); e(str); d dVar = this.j.get(str); if (dVar == null) { dVar = new d(str); this.j.put(str, dVar); } else if (dVar.d != null) { return null; } C0081c c0081c = new C0081c(dVar); dVar.d = c0081c; this.i.write("DIRTY " + str + '\n'); this.i.flush(); return c0081c; } catch (Throwable th) { throw th; } } } public final void a() { if (this.i == null) { throw new IllegalStateException("cache is closed"); } } }