package com.google.android.gms.internal.ads;

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.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes2.dex */
public final class vc implements zb {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public long f17591a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f17592b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public Object f17593c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Object f17594d;

    public vc(long j10, String str, String str2, int i10) {
        this.f17591a = j10;
        this.f17593c = str;
        this.f17594d = str2;
        this.f17592b = i10;
    }

    public static byte[] d(it1 it1Var, long j10) throws IOException {
        long j11 = it1Var.f12632b - it1Var.f12633c;
        if (j10 >= 0 && j10 <= j11) {
            int i10 = (int) j10;
            if (i10 == j10) {
                byte[] bArr = new byte[i10];
                new DataInputStream(it1Var).readFully(bArr);
                return bArr;
            }
        }
        StringBuilder sb = new StringBuilder(String.valueOf(j10).length() + 33 + String.valueOf(j11).length());
        defpackage.e.B(sb, "streamToBytes length=", j10, ", maxLength=");
        sb.append(j11);
        throw new IOException(sb.toString());
    }

    public static void e(BufferedOutputStream bufferedOutputStream, int i10) throws IOException {
        bufferedOutputStream.write(i10 & 255);
        bufferedOutputStream.write((i10 >> 8) & 255);
        bufferedOutputStream.write((i10 >> 16) & 255);
        bufferedOutputStream.write((i10 >> 24) & 255);
    }

    public static int f(InputStream inputStream) {
        return (l(inputStream) << 24) | l(inputStream) | (l(inputStream) << 8) | (l(inputStream) << 16);
    }

    public static void g(BufferedOutputStream bufferedOutputStream, long j10) throws IOException {
        bufferedOutputStream.write((byte) j10);
        bufferedOutputStream.write((byte) (j10 >>> 8));
        bufferedOutputStream.write((byte) (j10 >>> 16));
        bufferedOutputStream.write((byte) (j10 >>> 24));
        bufferedOutputStream.write((byte) (j10 >>> 32));
        bufferedOutputStream.write((byte) (j10 >>> 40));
        bufferedOutputStream.write((byte) (j10 >>> 48));
        bufferedOutputStream.write((byte) (j10 >>> 56));
    }

    public static long h(InputStream inputStream) {
        return (((long) l(inputStream)) & 255) | ((((long) l(inputStream)) & 255) << 8) | ((((long) l(inputStream)) & 255) << 16) | ((((long) l(inputStream)) & 255) << 24) | ((((long) l(inputStream)) & 255) << 32) | ((((long) l(inputStream)) & 255) << 40) | ((((long) l(inputStream)) & 255) << 48) | ((((long) l(inputStream)) & 255) << 56);
    }

    public static void i(BufferedOutputStream bufferedOutputStream, String str) throws IOException {
        byte[] bytes = str.getBytes("UTF-8");
        int length = bytes.length;
        g(bufferedOutputStream, length);
        bufferedOutputStream.write(bytes, 0, length);
    }

    public static String j(it1 it1Var) {
        return new String(d(it1Var, h(it1Var)), "UTF-8");
    }

    public static int l(InputStream inputStream) throws IOException {
        int i10 = inputStream.read();
        if (i10 != -1) {
            return i10;
        }
        throw new EOFException();
    }

    public static final String m(String str) {
        int length = str.length() >> 1;
        return String.valueOf(String.valueOf(str.substring(0, length).hashCode())).concat(String.valueOf(String.valueOf(str.substring(length).hashCode())));
    }

    public final synchronized void a(String str, yb ybVar) {
        long j10;
        try {
            long j11 = this.f17591a;
            int length = ybVar.f18779a.length;
            long j12 = j11 + ((long) length);
            int i10 = this.f17592b;
            if (j12 <= i10 || length <= i10 * 0.9f) {
                File fileC = c(str);
                try {
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileC));
                    tc tcVar = new tc(str, ybVar);
                    try {
                        e(bufferedOutputStream, 538247942);
                        i(bufferedOutputStream, str);
                        String str2 = tcVar.f16843c;
                        if (str2 == null) {
                            str2 = "";
                        }
                        i(bufferedOutputStream, str2);
                        g(bufferedOutputStream, tcVar.f16844d);
                        g(bufferedOutputStream, tcVar.f16845e);
                        g(bufferedOutputStream, tcVar.f16846f);
                        g(bufferedOutputStream, tcVar.f16847g);
                        List<cc> list = tcVar.f16848h;
                        if (list != null) {
                            e(bufferedOutputStream, list.size());
                            for (cc ccVar : list) {
                                i(bufferedOutputStream, ccVar.f9861a);
                                i(bufferedOutputStream, ccVar.f9862b);
                            }
                        } else {
                            e(bufferedOutputStream, 0);
                        }
                        bufferedOutputStream.flush();
                        bufferedOutputStream.write(ybVar.f18779a);
                        bufferedOutputStream.close();
                        tcVar.f16841a = fileC.length();
                        k(str, tcVar);
                        long j13 = this.f17591a;
                        int i11 = this.f17592b;
                        if (j13 >= i11) {
                            boolean z10 = oc.f15004a;
                            if (z10) {
                                oc.a("Pruning old cache entries.", new Object[0]);
                            }
                            long j14 = this.f17591a;
                            long jElapsedRealtime = SystemClock.elapsedRealtime();
                            Iterator it = ((Map) this.f17593c).entrySet().iterator();
                            int i12 = 0;
                            while (true) {
                                if (!it.hasNext()) {
                                    j10 = j14;
                                    break;
                                }
                                tc tcVar2 = (tc) ((Map.Entry) it.next()).getValue();
                                String str3 = tcVar2.f16842b;
                                if (c(str3).delete()) {
                                    j10 = j14;
                                    this.f17591a -= tcVar2.f16841a;
                                } else {
                                    j10 = j14;
                                    oc.b("Could not delete cache entry for key=%s, filename=%s", str3, m(str3));
                                }
                                it.remove();
                                i12++;
                                if (this.f17591a < i11 * 0.9f) {
                                    break;
                                } else {
                                    j14 = j10;
                                }
                            }
                            if (z10) {
                                oc.a("pruned %d files, %d bytes, %d ms", Integer.valueOf(i12), Long.valueOf(this.f17591a - j10), Long.valueOf(SystemClock.elapsedRealtime() - jElapsedRealtime));
                            }
                        }
                    } catch (IOException e10) {
                        oc.b("%s", e10.toString());
                        bufferedOutputStream.close();
                        oc.b("Failed to write header for %s", fileC.getAbsolutePath());
                        throw new IOException();
                    }
                } catch (IOException unused) {
                    if (!fileC.delete()) {
                        oc.b("Could not clean up file %s", fileC.getAbsolutePath());
                    }
                    if (!((uc) this.f17594d).zza().exists()) {
                        oc.b("Re-initializing cache after external clearing.", new Object[0]);
                        ((Map) this.f17593c).clear();
                        this.f17591a = 0L;
                        b();
                    }
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void b() {
        long length;
        it1 it1Var;
        File fileZza = ((uc) this.f17594d).zza();
        if (fileZza.exists()) {
            File[] fileArrListFiles = fileZza.listFiles();
            if (fileArrListFiles != null) {
                for (File file : fileArrListFiles) {
                    try {
                        length = file.length();
                        it1Var = new it1(new BufferedInputStream(new FileInputStream(file)), length, 1);
                    } catch (IOException unused) {
                        file.delete();
                    }
                    try {
                        tc tcVarA = tc.a(it1Var);
                        tcVarA.f16841a = length;
                        k(tcVarA.f16842b, tcVarA);
                        it1Var.close();
                    } catch (Throwable th) {
                        it1Var.close();
                        throw th;
                    }
                }
            }
        } else if (!fileZza.mkdirs()) {
            oc.b("Unable to create cache dir %s", fileZza.getAbsolutePath());
        }
    }

    public final File c(String str) {
        return new File(((uc) this.f17594d).zza(), m(str));
    }

    public final void k(String str, tc tcVar) {
        Map map = (Map) this.f17593c;
        if (map.containsKey(str)) {
            this.f17591a = (tcVar.f16841a - ((tc) map.get(str)).f16841a) + this.f17591a;
        } else {
            this.f17591a += tcVar.f16841a;
        }
        map.put(str, tcVar);
    }

    @Override // com.google.android.gms.internal.ads.zb
    public final synchronized yb zza(String str) {
        tc tcVar = (tc) ((Map) this.f17593c).get(str);
        if (tcVar == null) {
            return null;
        }
        File fileC = c(str);
        try {
            it1 it1Var = new it1(new BufferedInputStream(new FileInputStream(fileC)), fileC.length(), 1);
            try {
                String str2 = tc.a(it1Var).f16842b;
                if (!TextUtils.equals(str, str2)) {
                    oc.b("%s: key=%s, found=%s", fileC.getAbsolutePath(), str, str2);
                    tc tcVar2 = (tc) ((Map) this.f17593c).remove(str);
                    if (tcVar2 != null) {
                        this.f17591a -= tcVar2.f16841a;
                    }
                    return null;
                }
                byte[] bArrD = d(it1Var, it1Var.f12632b - it1Var.f12633c);
                yb ybVar = new yb();
                ybVar.f18779a = bArrD;
                ybVar.f18780b = tcVar.f16843c;
                ybVar.f18781c = tcVar.f16844d;
                ybVar.f18782d = tcVar.f16845e;
                ybVar.f18783e = tcVar.f16846f;
                ybVar.f18784f = tcVar.f16847g;
                List<cc> list = tcVar.f16848h;
                TreeMap treeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
                for (cc ccVar : list) {
                    treeMap.put(ccVar.f9861a, ccVar.f9862b);
                }
                ybVar.f18785g = treeMap;
                ybVar.f18786h = Collections.unmodifiableList(list);
                return ybVar;
            } finally {
                it1Var.close();
            }
        } catch (IOException e10) {
            oc.b("%s: %s", fileC.getAbsolutePath(), e10.toString());
            synchronized (this) {
                boolean zDelete = c(str).delete();
                tc tcVar3 = (tc) ((Map) this.f17593c).remove(str);
                if (tcVar3 != null) {
                    this.f17591a -= tcVar3.f16841a;
                }
                if (!zDelete) {
                    oc.b("Could not delete cache entry for key=%s, filename=%s", str, m(str));
                }
                return null;
            }
        }
    }

    public vc(zv1 zv1Var) {
        this.f17593c = new LinkedHashMap(16, 0.75f, true);
        this.f17591a = 0L;
        this.f17594d = zv1Var;
        this.f17592b = 5242880;
    }

    public vc(File file) {
        this.f17593c = new LinkedHashMap(16, 0.75f, true);
        this.f17591a = 0L;
        this.f17594d = new sc(this, file);
        this.f17592b = 20971520;
    }

    public vc(com.google.android.gms.internal.pal.w wVar) {
        wVar.getClass();
        this.f17594d = wVar;
    }
}
