package com.facetec.sdk; import java.io.Closeable; import java.io.File; import java.io.Flushable; import java.io.IOException; import java.util.LinkedHashMap; import java.util.concurrent.Executor; import java.util.regex.Pattern; /* loaded from: classes.dex */ public final class kq implements Closeable, Flushable { private mv a; private mf b; private int c; private long d; private long e; private boolean f; private int g; private boolean h; private boolean i; private LinkedHashMap j; private final Runnable m; /* renamed from: o, reason: collision with root package name */ private final Executor f6449o; /* loaded from: classes.dex */ public final class b { boolean a; final kq c; final d d; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public final class d { final File[] a; final String b; boolean c; final long[] d; final File[] e; b j; } static { Pattern.compile("[a-z0-9_-]{1,120}"); } private void d(b bVar) throws IOException { synchronized (this) { d dVar = bVar.d; if (dVar.j != bVar) { throw new IllegalStateException(); } for (int i = 0; i < this.c; i++) { this.b.c(dVar.a[i]); } this.g++; dVar.j = null; if (dVar.c) { dVar.c = true; this.a.e("CLEAN").h(32); this.a.e(dVar.b); mv mvVar = this.a; for (long j : dVar.d) { mvVar.h(32).n(j); } this.a.h(10); } else { this.j.remove(dVar.b); this.a.e("REMOVE").h(32); this.a.e(dVar.b); this.a.h(10); } this.a.flush(); if (this.d > this.e || c()) { this.f6449o.execute(this.m); } } } private boolean c() { int i = this.g; return i >= 2000 && i >= this.j.size(); } private boolean d(d dVar) throws IOException { if (dVar.j != null) { b bVar = dVar.j; if (bVar.d.j == bVar) { for (int i = 0; i < bVar.c.c; i++) { try { bVar.c.b.c(bVar.d.a[i]); } catch (IOException unused) { } } bVar.d.j = null; } } for (int i2 = 0; i2 < this.c; i2++) { this.b.c(dVar.e[i2]); this.d -= dVar.d[i2]; dVar.d[i2] = 0; } this.g++; this.a.e("REMOVE").h(32).e(dVar.b).h(10); this.j.remove(dVar.b); if (c()) { this.f6449o.execute(this.m); } return true; } private boolean a() { boolean z; synchronized (this) { z = this.h; } return z; } private void d() { synchronized (this) { if (a()) { throw new IllegalStateException("cache is closed"); } } } @Override // java.io.Flushable public final void flush() throws IOException { synchronized (this) { if (this.i) { d(); e(); this.a.flush(); } } } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { synchronized (this) { if (this.i && !this.h) { for (d dVar : (d[]) this.j.values().toArray(new d[this.j.size()])) { if (dVar.j != null) { b bVar = dVar.j; synchronized (bVar.c) { if (bVar.a) { throw new IllegalStateException(); } if (bVar.d.j == bVar) { bVar.c.d(bVar); } bVar.a = true; } } } e(); this.a.close(); this.a = null; this.h = true; return; } this.h = true; } } private void e() throws IOException { while (this.d > this.e) { d(this.j.values().iterator().next()); } this.f = false; } }