package o; import android.os.SystemClock; import android.text.TextUtils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import o.InterfaceC17333vqS; /* loaded from: classes.dex */ public final class RRt implements InterfaceC17333vqS { private final int a; private final Sts b; private long d; private final Map e; /* loaded from: classes.dex */ public interface Sts { File b(); } private RRt(Sts sts, byte b) { this.e = new LinkedHashMap(16, 0.75f, true); this.d = 0L; this.b = sts; this.a = 5242880; } public RRt(Sts sts) { this(sts, (byte) 0); } @Override // o.InterfaceC17333vqS public final InterfaceC17333vqS.IeS d(String str) { synchronized (this) { HBt hBt = this.e.get(str); if (hBt == null) { return null; } File file = new File(this.b.b(), c(str)); try { IeS ieS = new IeS(new BufferedInputStream(new FileInputStream(file)), file.length()); try { HBt c = HBt.c(ieS); if (!TextUtils.equals(str, c.d)) { qWd.d("%s: key=%s, found=%s", file.getAbsolutePath(), str, c.d); e(str); return null; } byte[] b = b(ieS, ieS.a - ieS.d); InterfaceC17333vqS.IeS ieS2 = new InterfaceC17333vqS.IeS(); ieS2.a = b; ieS2.e = hBt.c; ieS2.h = hBt.b; ieS2.b = hBt.e; ieS2.f = hBt.f; ieS2.i = hBt.g; ieS2.c = HNc.b(hBt.a); ieS2.d = Collections.unmodifiableList(hBt.a); return ieS2; } finally { ieS.close(); } } catch (IOException e) { qWd.d("%s: %s", file.getAbsolutePath(), e.toString()); synchronized (this) { boolean delete = new File(this.b.b(), c(str)).delete(); e(str); if (!delete) { qWd.d("Could not delete cache entry for key=%s, filename=%s", str, c(str)); } return null; } } } } @Override // o.InterfaceC17333vqS public final void c() { synchronized (this) { File b = this.b.b(); if (!b.exists()) { if (!b.mkdirs()) { qWd.a("Unable to create cache dir %s", b.getAbsolutePath()); } return; } File[] listFiles = b.listFiles(); if (listFiles == null) { return; } for (File file : listFiles) { try { long length = file.length(); IeS ieS = new IeS(new BufferedInputStream(new FileInputStream(file)), length); try { HBt c = HBt.c(ieS); c.j = length; c(c.d, c); ieS.close(); } catch (Throwable th) { ieS.close(); throw th; break; } } catch (IOException unused) { file.delete(); } } } } @Override // o.InterfaceC17333vqS public final void a(String str) { synchronized (this) { InterfaceC17333vqS.IeS d = d(str); if (d != null) { d.i = 0L; d.f = 0L; c(str, d); } } } @Override // o.InterfaceC17333vqS public final void c(String str, InterfaceC17333vqS.IeS ieS) { BufferedOutputStream bufferedOutputStream; HBt hBt; synchronized (this) { if (this.d + ieS.a.length <= this.a || ieS.a.length <= this.a * 0.9f) { File file = new File(this.b.b(), c(str)); try { bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); hBt = new HBt(str, ieS); } catch (IOException unused) { if (!file.delete()) { qWd.d("Could not clean up file %s", file.getAbsolutePath()); } b(); } if (!hBt.b(bufferedOutputStream)) { bufferedOutputStream.close(); qWd.d("Failed to write header for %s", file.getAbsolutePath()); throw new IOException(); } bufferedOutputStream.write(ieS.a); bufferedOutputStream.close(); hBt.j = file.length(); c(str, hBt); a(); } } } private static String c(String str) { int length = str.length() / 2; int hashCode = str.substring(0, length).hashCode(); StringBuilder sb = new StringBuilder(); sb.append(String.valueOf(hashCode)); sb.append(String.valueOf(str.substring(length).hashCode())); return sb.toString(); } private void b() { if (this.b.b().exists()) { return; } qWd.d("Re-initializing cache after external clearing.", new Object[0]); this.e.clear(); this.d = 0L; c(); } private void a() { if (this.d < this.a) { return; } int i = 0; if (qWd.a) { qWd.e("Pruning old cache entries.", new Object[0]); } long j = this.d; long elapsedRealtime = SystemClock.elapsedRealtime(); Iterator> it = this.e.entrySet().iterator(); while (it.hasNext()) { HBt value = it.next().getValue(); if (new File(this.b.b(), c(value.d)).delete()) { this.d -= value.j; } else { qWd.d("Could not delete cache entry for key=%s, filename=%s", value.d, c(value.d)); } it.remove(); i++; if (((float) this.d) < this.a * 0.9f) { break; } } if (qWd.a) { qWd.e("pruned %d files, %d bytes, %d ms", Integer.valueOf(i), Long.valueOf(this.d - j), Long.valueOf(SystemClock.elapsedRealtime() - elapsedRealtime)); } } private void c(String str, HBt hBt) { if (!this.e.containsKey(str)) { this.d += hBt.j; } else { this.d += hBt.j - this.e.get(str).j; } this.e.put(str, hBt); } private void e(String str) { HBt remove = this.e.remove(str); if (remove != null) { this.d -= remove.j; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class HBt { final List a; final long b; final String c; final String d; final long e; final long f; final long g; long j; private HBt(String str, String str2, long j, long j2, long j3, long j4, List list) { this.d = str; this.c = "".equals(str2) ? null : str2; this.b = j; this.e = j2; this.f = j3; this.g = j4; this.a = list; } /* JADX WARN: Illegal instructions before constructor call */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ HBt(java.lang.String r13, o.InterfaceC17333vqS.IeS r14) { /* r12 = this; java.lang.String r2 = r14.e long r3 = r14.h long r5 = r14.b long r7 = r14.f long r9 = r14.i java.util.List r0 = r14.d if (r0 == 0) goto L11 java.util.List r14 = r14.d goto L17 L11: java.util.Map r14 = r14.c java.util.List r14 = o.HNc.a(r14) L17: r11 = r14 r0 = r12 r1 = r13 r0.(r1, r2, r3, r5, r7, r9, r11) return */ throw new UnsupportedOperationException("Method not decompiled: o.RRt.HBt.(java.lang.String, o.vqS$IeS):void"); } static HBt c(IeS ieS) throws IOException { if (RRt.d(ieS) != 538247942) { throw new IOException(); } return new HBt(new String(RRt.b(ieS, RRt.c((InputStream) ieS)), "UTF-8"), new String(RRt.b(ieS, RRt.c((InputStream) ieS)), "UTF-8"), RRt.c((InputStream) ieS), RRt.c((InputStream) ieS), RRt.c((InputStream) ieS), RRt.c((InputStream) ieS), RRt.c(ieS)); } final boolean b(OutputStream outputStream) { try { RRt.a(outputStream, 538247942); byte[] bytes = this.d.getBytes("UTF-8"); RRt.c(outputStream, bytes.length); outputStream.write(bytes, 0, bytes.length); String str = this.c; if (str == null) { str = ""; } byte[] bytes2 = str.getBytes("UTF-8"); RRt.c(outputStream, bytes2.length); outputStream.write(bytes2, 0, bytes2.length); RRt.c(outputStream, this.b); RRt.c(outputStream, this.e); RRt.c(outputStream, this.f); RRt.c(outputStream, this.g); List list = this.a; if (list != null) { RRt.a(outputStream, list.size()); for (ucE uce : list) { byte[] bytes3 = uce.e.getBytes("UTF-8"); RRt.c(outputStream, bytes3.length); outputStream.write(bytes3, 0, bytes3.length); byte[] bytes4 = uce.b.getBytes("UTF-8"); RRt.c(outputStream, bytes4.length); outputStream.write(bytes4, 0, bytes4.length); } } else { RRt.a(outputStream, 0); } outputStream.flush(); return true; } catch (IOException e) { qWd.d("%s", e.toString()); return false; } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class IeS extends FilterInputStream { final long a; long d; IeS(InputStream inputStream, long j) { super(inputStream); this.a = j; } @Override // java.io.FilterInputStream, java.io.InputStream public final int read() throws IOException { int read = super.read(); if (read != -1) { this.d++; } return read; } @Override // java.io.FilterInputStream, java.io.InputStream public final int read(byte[] bArr, int i, int i2) throws IOException { int read = super.read(bArr, i, i2); if (read != -1) { this.d += read; } return read; } } static void a(OutputStream outputStream, int i) throws IOException { outputStream.write(i & 255); outputStream.write((i >> 8) & 255); outputStream.write((i >> 16) & 255); outputStream.write(i >>> 24); } static void c(OutputStream outputStream, long j) throws IOException { outputStream.write((byte) j); outputStream.write((byte) (j >>> 8)); outputStream.write((byte) (j >>> 16)); outputStream.write((byte) (j >>> 24)); outputStream.write((byte) (j >>> 32)); outputStream.write((byte) (j >>> 40)); outputStream.write((byte) (j >>> 48)); outputStream.write((byte) (j >>> 56)); } static List c(IeS ieS) throws IOException { int d = d(ieS); if (d < 0) { throw new IOException("readHeaderList size=".concat(String.valueOf(d))); } List emptyList = d == 0 ? Collections.emptyList() : new ArrayList<>(); for (int i = 0; i < d; i++) { emptyList.add(new ucE(new String(b(ieS, c((InputStream) ieS)), "UTF-8").intern(), new String(b(ieS, c((InputStream) ieS)), "UTF-8").intern())); } return emptyList; } static int d(InputStream inputStream) throws IOException { int read = inputStream.read(); if (read == -1) { throw new EOFException(); } int read2 = inputStream.read(); if (read2 == -1) { throw new EOFException(); } int read3 = inputStream.read(); if (read3 == -1) { throw new EOFException(); } int read4 = inputStream.read(); if (read4 != -1) { return (read4 << 24) | read | (read2 << 8) | (read3 << 16); } throw new EOFException(); } static long c(InputStream inputStream) throws IOException { int read = inputStream.read(); if (read == -1) { throw new EOFException(); } long j = read; int read2 = inputStream.read(); if (read2 == -1) { throw new EOFException(); } long j2 = read2; int read3 = inputStream.read(); if (read3 == -1) { throw new EOFException(); } long j3 = read3; int read4 = inputStream.read(); if (read4 == -1) { throw new EOFException(); } long j4 = read4; int read5 = inputStream.read(); if (read5 == -1) { throw new EOFException(); } long j5 = read5; int read6 = inputStream.read(); if (read6 == -1) { throw new EOFException(); } long j6 = read6; int read7 = inputStream.read(); if (read7 == -1) { throw new EOFException(); } long j7 = (j & 255) | ((j2 & 255) << 8) | ((j3 & 255) << 16) | ((j4 & 255) << 24) | ((j5 & 255) << 32) | ((j6 & 255) << 40) | ((read7 & 255) << 48); int read8 = inputStream.read(); if (read8 != -1) { return ((read8 & 255) << 56) | j7; } throw new EOFException(); } static byte[] b(IeS ieS, long j) throws IOException { long j2 = ieS.a - ieS.d; if (j >= 0 && j <= j2) { int i = (int) j; if (i == j) { byte[] bArr = new byte[i]; new DataInputStream(ieS).readFully(bArr); return bArr; } } StringBuilder sb = new StringBuilder("streamToBytes length="); sb.append(j); sb.append(", maxLength="); sb.append(j2); throw new IOException(sb.toString()); } }