c.java 源代码


package s;

import android.os.SystemClock;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import r.b;
import r.s;

public class c implements r.b {

    private final Map<String, b> f26090a;

    private long f26091b;

    private final File f26092c;

    private final int f26093d;

    public static class b {

        public long f26094a;

        public String f26095b;

        public String f26096c;

        public long f26097d;

        public long f26098e;

        public long f26099f;

        public long f26100g;

        public Map<String, String> f26101h;

        private b() {
        }

        public static b a(InputStream inputStream) throws IOException {
            b bVar = new b();
            if (c.h(inputStream) == 538247942) {
                bVar.f26095b = c.j(inputStream);
                String j6 = c.j(inputStream);
                bVar.f26096c = j6;
                if (j6.equals("")) {
                    bVar.f26096c = null;
                }
                bVar.f26097d = c.i(inputStream);
                bVar.f26098e = c.i(inputStream);
                bVar.f26099f = c.i(inputStream);
                bVar.f26100g = c.i(inputStream);
                bVar.f26101h = c.k(inputStream);
                return bVar;
            }
            throw new IOException();
        }

        public b.a b(byte[] bArr) {
            b.a aVar = new b.a();
            aVar.f25810a = bArr;
            aVar.f25811b = this.f26096c;
            aVar.f25812c = this.f26097d;
            aVar.f25813d = this.f26098e;
            aVar.f25814e = this.f26099f;
            aVar.f25815f = this.f26100g;
            aVar.f25816g = this.f26101h;
            return aVar;
        }

        public boolean c(OutputStream outputStream) {
            try {
                c.o(outputStream, 538247942);
                c.q(outputStream, this.f26095b);
                String str = this.f26096c;
                if (str == null) {
                    str = "";
                }
                c.q(outputStream, str);
                c.p(outputStream, this.f26097d);
                c.p(outputStream, this.f26098e);
                c.p(outputStream, this.f26099f);
                c.p(outputStream, this.f26100g);
                c.r(this.f26101h, outputStream);
                outputStream.flush();
                return true;
            } catch (IOException e6) {
                s.b("%s", e6.toString());
                return false;
            }
        }

        public b(String str, b.a aVar) {
            this.f26095b = str;
            this.f26094a = aVar.f25810a.length;
            this.f26096c = aVar.f25811b;
            this.f26097d = aVar.f25812c;
            this.f26098e = aVar.f25813d;
            this.f26099f = aVar.f25814e;
            this.f26100g = aVar.f25815f;
            this.f26101h = aVar.f25816g;
        }
    }

    private static class C0331c extends FilterInputStream {

        private int f26102q;

        @Override
        public int read() throws IOException {
            int read = super.read();
            if (read != -1) {
                this.f26102q++;
            }
            return read;
        }

        private C0331c(InputStream inputStream) {
            super(inputStream);
            this.f26102q = 0;
        }

        @Override
        public int read(byte[] bArr, int i6, int i7) throws IOException {
            int read = super.read(bArr, i6, i7);
            if (read != -1) {
                this.f26102q += read;
            }
            return read;
        }
    }

    public c(File file, int i6) {
        this.f26090a = new LinkedHashMap(16, 0.75f, true);
        this.f26091b = 0L;
        this.f26092c = file;
        this.f26093d = i6;
    }

    private String d(String str) {
        int length = str.length() / 2;
        return String.valueOf(str.substring(0, length).hashCode()) + String.valueOf(str.substring(length).hashCode());
    }

    private void e(int i6) {
        long j6;
        long j7 = i6;
        if (this.f26091b + j7 < this.f26093d) {
            return;
        }
        if (s.f25882b) {
            s.e("Pruning old cache entries.", new Object[0]);
        }
        long j8 = this.f26091b;
        long elapsedRealtime = SystemClock.elapsedRealtime();
        Iterator<Map.Entry<String, b>> it = this.f26090a.entrySet().iterator();
        int i7 = 0;
        while (it.hasNext()) {
            b value = it.next().getValue();
            if (c(value.f26095b).delete()) {
                j6 = j7;
                this.f26091b -= value.f26094a;
            } else {
                j6 = j7;
                String str = value.f26095b;
                s.b("Could not delete cache entry for key=%s, filename=%s", str, d(str));
            }
            it.remove();
            i7++;
            if (((float) (this.f26091b + j6)) < this.f26093d * 0.9f) {
                break;
            } else {
                j7 = j6;
            }
        }
        if (s.f25882b) {
            s.e("pruned %d files, %d bytes, %d ms", Integer.valueOf(i7), Long.valueOf(this.f26091b - j8), Long.valueOf(SystemClock.elapsedRealtime() - elapsedRealtime));
        }
    }

    private void f(String str, b bVar) {
        if (!this.f26090a.containsKey(str)) {
            this.f26091b += bVar.f26094a;
        } else {
            this.f26091b += bVar.f26094a - this.f26090a.get(str).f26094a;
        }
        this.f26090a.put(str, bVar);
    }

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

    static int h(InputStream inputStream) throws IOException {
        return (g(inputStream) << 24) | (g(inputStream) << 0) | 0 | (g(inputStream) << 8) | (g(inputStream) << 16);
    }

    static long i(InputStream inputStream) throws IOException {
        return ((g(inputStream) & 255) << 0) | 0 | ((g(inputStream) & 255) << 8) | ((g(inputStream) & 255) << 16) | ((g(inputStream) & 255) << 24) | ((g(inputStream) & 255) << 32) | ((g(inputStream) & 255) << 40) | ((g(inputStream) & 255) << 48) | ((255 & g(inputStream)) << 56);
    }

    static String j(InputStream inputStream) throws IOException {
        return new String(n(inputStream, (int) i(inputStream)), "UTF-8");
    }

    static Map<String, String> k(InputStream inputStream) throws IOException {
        Map<String, String> hashMap;
        int h6 = h(inputStream);
        if (h6 == 0) {
            hashMap = Collections.emptyMap();
        } else {
            hashMap = new HashMap<>(h6);
        }
        for (int i6 = 0; i6 < h6; i6++) {
            hashMap.put(j(inputStream).intern(), j(inputStream).intern());
        }
        return hashMap;
    }

    private void m(String str) {
        b bVar = this.f26090a.get(str);
        if (bVar != null) {
            this.f26091b -= bVar.f26094a;
            this.f26090a.remove(str);
        }
    }

    private static byte[] n(InputStream inputStream, int i6) throws IOException {
        byte[] bArr = new byte[i6];
        int i7 = 0;
        while (i7 < i6) {
            int read = inputStream.read(bArr, i7, i6 - i7);
            if (read == -1) {
                break;
            }
            i7 += read;
        }
        if (i7 == i6) {
            return bArr;
        }
        throw new IOException("Expected " + i6 + " bytes, read " + i7 + " bytes");
    }

    static void o(OutputStream outputStream, int i6) throws IOException {
        outputStream.write((i6 >> 0) & 255);
        outputStream.write((i6 >> 8) & 255);
        outputStream.write((i6 >> 16) & 255);
        outputStream.write((i6 >> 24) & 255);
    }

    static void p(OutputStream outputStream, long j6) throws IOException {
        outputStream.write((byte) (j6 >>> 0));
        outputStream.write((byte) (j6 >>> 8));
        outputStream.write((byte) (j6 >>> 16));
        outputStream.write((byte) (j6 >>> 24));
        outputStream.write((byte) (j6 >>> 32));
        outputStream.write((byte) (j6 >>> 40));
        outputStream.write((byte) (j6 >>> 48));
        outputStream.write((byte) (j6 >>> 56));
    }

    static void q(OutputStream outputStream, String str) throws IOException {
        byte[] bytes = str.getBytes("UTF-8");
        p(outputStream, bytes.length);
        outputStream.write(bytes, 0, bytes.length);
    }

    static void r(Map<String, String> map, OutputStream outputStream) throws IOException {
        if (map != null) {
            o(outputStream, map.size());
            for (Map.Entry<String, String> entry : map.entrySet()) {
                q(outputStream, entry.getKey());
                q(outputStream, entry.getValue());
            }
            return;
        }
        o(outputStream, 0);
    }

    @Override
    public synchronized void a() {
        BufferedInputStream bufferedInputStream;
        if (!this.f26092c.exists()) {
            if (!this.f26092c.mkdirs()) {
                s.c("Unable to create cache dir %s", this.f26092c.getAbsolutePath());
            }
            return;
        }
        File[] listFiles = this.f26092c.listFiles();
        if (listFiles == null) {
            return;
        }
        for (File file : listFiles) {
            BufferedInputStream bufferedInputStream2 = null;
            try {
                try {
                    bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                } catch (IOException unused) {
                }
            } catch (Throwable th) {
                th = th;
            }
            try {
                b a7 = b.a(bufferedInputStream);
                a7.f26094a = file.length();
                f(a7.f26095b, a7);
                try {
                    bufferedInputStream.close();
                } catch (IOException unused2) {
                }
            } catch (IOException unused3) {
                bufferedInputStream2 = bufferedInputStream;
                if (file != null) {
                    file.delete();
                }
                if (bufferedInputStream2 != null) {
                    bufferedInputStream2.close();
                }
            } catch (Throwable th2) {
                th = th2;
                bufferedInputStream2 = bufferedInputStream;
                if (bufferedInputStream2 != null) {
                    try {
                        bufferedInputStream2.close();
                    } catch (IOException unused4) {
                    }
                }
                throw th;
            }
        }
    }

    @Override
    public synchronized void b(String str, b.a aVar) {
        e(aVar.f25810a.length);
        File c6 = c(str);
        try {
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(c6));
            b bVar = new b(str, aVar);
            if (bVar.c(bufferedOutputStream)) {
                bufferedOutputStream.write(aVar.f25810a);
                bufferedOutputStream.close();
                f(str, bVar);
            } else {
                bufferedOutputStream.close();
                s.b("Failed to write header for %s", c6.getAbsolutePath());
                throw new IOException();
            }
        } catch (IOException unused) {
            if (!c6.delete()) {
                s.b("Could not clean up file %s", c6.getAbsolutePath());
            }
        }
    }

    public File c(String str) {
        return new File(this.f26092c, d(str));
    }

    @Override
    public synchronized b.a get(String str) {
        File c6;
        C0331c c0331c;
        b bVar = this.f26090a.get(str);
        InputStream inputStream = null;
        Object[] objArr = 0;
        if (bVar == null) {
            return null;
        }
        try {
            c6 = c(str);
        } catch (Throwable th) {
            th = th;
        }
        try {
            c0331c = new C0331c(new BufferedInputStream(new FileInputStream(c6)));
            try {
                b.a(c0331c);
                b.a b6 = bVar.b(n(c0331c, (int) (c6.length() - c0331c.f26102q)));
                try {
                    c0331c.close();
                    return b6;
                } catch (IOException unused) {
                    return null;
                }
            } catch (IOException e6) {
                e = e6;
                s.b("%s: %s", c6.getAbsolutePath(), e.toString());
                l(str);
                if (c0331c != null) {
                    try {
                        c0331c.close();
                    } catch (IOException unused2) {
                        return null;
                    }
                }
                return null;
            }
        } catch (IOException e7) {
            e = e7;
            c0331c = null;
        } catch (Throwable th2) {
            th = th2;
            if (0 != 0) {
                try {
                    inputStream.close();
                } catch (IOException unused3) {
                    return null;
                }
            }
            throw th;
        }
    }

    public synchronized void l(String str) {
        boolean delete = c(str).delete();
        m(str);
        if (!delete) {
            s.b("Could not delete cache entry for key=%s, filename=%s", str, d(str));
        }
    }

    public c(File file) {
        this(file, 5242880);
    }
}