导航菜单

页面标题

页面副标题

FileWipe Pro v3.0.0 - DiskLruCache.java 源代码

正在查看: FileWipe Pro v3.0.0 应用的 DiskLruCache.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.tp.common;

import ad.f;
import com.startapp.simple.bloomfilter.codec.IOUtils;
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.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
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;

public final class DiskLruCache implements Closeable {
    public static final Pattern o = Pattern.compile("[a-z0-9_-]{1,64}");
    public static final b p = new b();
    public final File a;
    public final File b;
    public final File c;
    public final File d;
    public final int e;
    public long f;
    public final int g;
    public BufferedWriter i;
    public int k;
    public long h = 0;
    public final LinkedHashMap<String, c> j = new LinkedHashMap<>(0, 0.75f, true);
    public long l = 0;
    public final ThreadPoolExecutor m = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue());
    public final a n = new a();

    public final class Editor {
        public final c a;
        public final boolean[] b;
        public boolean c;
        public boolean d;

        public Editor() {
            throw null;
        }

        public void abort() {
            DiskLruCache.a(DiskLruCache.this, this, false);
        }

        public void abortUnlessCommitted() {
            if (this.d) {
                return;
            }
            try {
                abort();
            } catch (IOException unused) {
            }
        }

        public void commit() {
            if (this.c) {
                DiskLruCache.a(DiskLruCache.this, this, false);
                DiskLruCache.this.remove(this.a.a);
            } else {
                DiskLruCache.a(DiskLruCache.this, this, true);
            }
            this.d = true;
        }

        public String getString(int i) {
            InputStream newInputStream = newInputStream(i);
            if (newInputStream == null) {
                return null;
            }
            Pattern pattern = DiskLruCache.o;
            InputStreamReader inputStreamReader = new InputStreamReader(newInputStream, DiskLruCacheUtil.b);
            try {
                StringWriter stringWriter = new StringWriter();
                char[] cArr = new char[1024];
                while (true) {
                    int read = inputStreamReader.read(cArr);
                    if (read == -1) {
                        String stringWriter2 = stringWriter.toString();
                        inputStreamReader.close();
                        return stringWriter2;
                    }
                    stringWriter.write(cArr, 0, read);
                }
            } catch (Throwable th) {
                inputStreamReader.close();
                throw th;
            }
        }

        public InputStream newInputStream(int i) {
            synchronized (DiskLruCache.this) {
                c cVar = this.a;
                if (cVar.d != this) {
                    throw new IllegalStateException();
                }
                if (!cVar.c) {
                    return null;
                }
                try {
                    return new FileInputStream(this.a.a(i));
                } catch (FileNotFoundException unused) {
                    return null;
                }
            }
        }

        public OutputStream newOutputStream(int i) {
            FileOutputStream fileOutputStream;
            a aVar;
            synchronized (DiskLruCache.this) {
                try {
                    c cVar = this.a;
                    if (cVar.d != this) {
                        throw new IllegalStateException();
                    }
                    if (!cVar.c) {
                        this.b[i] = true;
                    }
                    File b = cVar.b(i);
                    try {
                        fileOutputStream = new FileOutputStream(b);
                    } catch (FileNotFoundException unused) {
                        DiskLruCache.this.a.mkdirs();
                        try {
                            fileOutputStream = new FileOutputStream(b);
                        } catch (FileNotFoundException unused2) {
                            return DiskLruCache.p;
                        }
                    }
                    aVar = new a(fileOutputStream);
                } catch (Throwable th) {
                    throw th;
                }
            }
            return aVar;
        }

        public void set(int i, String str) {
            OutputStreamWriter outputStreamWriter = null;
            try {
                OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(newOutputStream(i), DiskLruCacheUtil.b);
                try {
                    outputStreamWriter2.write(str);
                    DiskLruCacheUtil.a(outputStreamWriter2);
                } catch (Throwable th) {
                    th = th;
                    outputStreamWriter = outputStreamWriter2;
                    DiskLruCacheUtil.a(outputStreamWriter);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        }

        public class a extends FilterOutputStream {
            public a(FileOutputStream fileOutputStream) {
                super(fileOutputStream);
            }

            @Override
            public final void close() {
                try {
                    ((FilterOutputStream) this).out.close();
                } catch (IOException unused) {
                    Editor.this.c = true;
                }
            }

            @Override
            public final void flush() {
                try {
                    ((FilterOutputStream) this).out.flush();
                } catch (IOException unused) {
                    Editor.this.c = true;
                }
            }

            @Override
            public final void write(int i) {
                try {
                    ((FilterOutputStream) this).out.write(i);
                } catch (IOException unused) {
                    Editor.this.c = true;
                }
            }

            @Override
            public final void write(byte[] bArr, int i, int i2) {
                try {
                    ((FilterOutputStream) this).out.write(bArr, i, i2);
                } catch (IOException unused) {
                    Editor.this.c = true;
                }
            }
        }

        public Editor(c cVar) {
            this.a = cVar;
            this.b = cVar.c ? null : new boolean[DiskLruCache.this.g];
        }
    }

    public final class Snapshot implements Closeable {
        public final String a;
        public final long b;
        public final InputStream[] c;
        public final long[] d;

        public Snapshot(String str, long j, InputStream[] inputStreamArr, long[] jArr) {
            this.a = str;
            this.b = j;
            this.c = inputStreamArr;
            this.d = jArr;
        }

        @Override
        public void close() {
            for (InputStream inputStream : this.c) {
                DiskLruCacheUtil.a(inputStream);
            }
        }

        public Editor edit() {
            DiskLruCache diskLruCache = DiskLruCache.this;
            String str = this.a;
            long j = this.b;
            Pattern pattern = DiskLruCache.o;
            return diskLruCache.a(str, j);
        }

        public InputStream getInputStream(int i) {
            return this.c[i];
        }

        public long getLength(int i) {
            return this.d[i];
        }

        public String getString(int i) {
            InputStream inputStream = getInputStream(i);
            Pattern pattern = DiskLruCache.o;
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DiskLruCacheUtil.b);
            try {
                StringWriter stringWriter = new StringWriter();
                char[] cArr = new char[1024];
                while (true) {
                    int read = inputStreamReader.read(cArr);
                    if (read == -1) {
                        String stringWriter2 = stringWriter.toString();
                        inputStreamReader.close();
                        return stringWriter2;
                    }
                    stringWriter.write(cArr, 0, read);
                }
            } catch (Throwable th) {
                inputStreamReader.close();
                throw th;
            }
        }
    }

    public class a implements Callable<Void> {
        public a() {
        }

        @Override
        public final Void call() {
            synchronized (DiskLruCache.this) {
                try {
                    DiskLruCache diskLruCache = DiskLruCache.this;
                    if (diskLruCache.i != null) {
                        while (diskLruCache.h > diskLruCache.f) {
                            diskLruCache.remove(diskLruCache.j.entrySet().iterator().next().getKey());
                        }
                        DiskLruCache diskLruCache2 = DiskLruCache.this;
                        int i = diskLruCache2.k;
                        if (i >= 2000 && i >= diskLruCache2.j.size()) {
                            DiskLruCache.this.c();
                            DiskLruCache.this.k = 0;
                        }
                    }
                } finally {
                }
            }
            return null;
        }
    }

    public class b extends OutputStream {
        @Override
        public final void write(int i) {
        }
    }

    public final class c {
        public final String a;
        public final long[] b;
        public boolean c;
        public Editor d;
        public long e;

        public c() {
            throw null;
        }

        public final File b(int i) {
            return new File(DiskLruCache.this.a, this.a + "." + i + ".tmp");
        }

        public c(String str) {
            this.a = str;
            this.b = new long[DiskLruCache.this.g];
        }

        public final File a(int i) {
            return new File(DiskLruCache.this.a, this.a + "." + i);
        }
    }

    public DiskLruCache(File file, int i, int i2, long j) {
        this.a = file;
        this.e = i;
        this.b = new File(file, "journal");
        this.c = new File(file, "journal.tmp");
        this.d = new File(file, "journal.bkp");
        this.g = i2;
        this.f = j;
    }

    public static DiskLruCache open(File file, int i, int i2, long j) {
        if (j <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        if (i2 <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        }
        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();
            }
        }
        DiskLruCache diskLruCache = new DiskLruCache(file, i, i2, j);
        if (diskLruCache.b.exists()) {
            try {
                diskLruCache.b();
                diskLruCache.a();
                diskLruCache.i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(diskLruCache.b, true), DiskLruCacheUtil.a));
                return diskLruCache;
            } catch (IOException e) {
                System.out.println("DiskLruCache " + file + " is corrupt: " + e.getMessage() + ", removing");
                diskLruCache.delete();
            }
        }
        file.mkdirs();
        DiskLruCache diskLruCache2 = new DiskLruCache(file, i, i2, j);
        diskLruCache2.c();
        return diskLruCache2;
    }

    public final void b() {
        f fVar = new f(new FileInputStream(this.b), DiskLruCacheUtil.a);
        try {
            String k = fVar.k();
            String k2 = fVar.k();
            String k3 = fVar.k();
            String k4 = fVar.k();
            String k5 = fVar.k();
            if (!"libcore.io.DiskLruCache".equals(k) || !"1".equals(k2) || !Integer.toString(this.e).equals(k3) || !Integer.toString(this.g).equals(k4) || !"".equals(k5)) {
                throw new IOException("unexpected journal header: [" + k + ", " + k2 + ", " + k4 + ", " + k5 + "]");
            }
            int i = 0;
            while (true) {
                try {
                    a(fVar.k());
                    i++;
                } catch (EOFException unused) {
                    this.k = i - this.j.size();
                    DiskLruCacheUtil.a(fVar);
                    return;
                }
            }
        } catch (Throwable th) {
            DiskLruCacheUtil.a(fVar);
            throw th;
        }
    }

    public final synchronized void c() {
        StringBuilder sb;
        try {
            BufferedWriter bufferedWriter = this.i;
            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
            BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.c), DiskLruCacheUtil.a));
            try {
                bufferedWriter2.write("libcore.io.DiskLruCache");
                bufferedWriter2.write(IOUtils.LINE_SEPARATOR_UNIX);
                bufferedWriter2.write("1");
                bufferedWriter2.write(IOUtils.LINE_SEPARATOR_UNIX);
                bufferedWriter2.write(Integer.toString(this.e));
                bufferedWriter2.write(IOUtils.LINE_SEPARATOR_UNIX);
                bufferedWriter2.write(Integer.toString(this.g));
                bufferedWriter2.write(IOUtils.LINE_SEPARATOR_UNIX);
                bufferedWriter2.write(IOUtils.LINE_SEPARATOR_UNIX);
                for (c cVar : this.j.values()) {
                    if (cVar.d != null) {
                        sb = new StringBuilder();
                        sb.append("DIRTY ");
                        sb.append(cVar.a);
                        sb.append('\n');
                    } else {
                        sb = new StringBuilder();
                        sb.append("CLEAN ");
                        sb.append(cVar.a);
                        StringBuilder sb2 = new StringBuilder();
                        for (long j : cVar.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;
                    if (file2.exists() && !file2.delete()) {
                        throw new IOException();
                    }
                    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), DiskLruCacheUtil.a));
            } catch (Throwable th) {
                bufferedWriter2.close();
                throw th;
            }
        } catch (Throwable th2) {
            throw th2;
        }
    }

    @Override
    public synchronized void close() {
        try {
            if (this.i == null) {
                return;
            }
            Iterator it = new ArrayList(this.j.values()).iterator();
            while (it.hasNext()) {
                Editor editor = ((c) it.next()).d;
                if (editor != null) {
                    editor.abort();
                }
            }
            while (this.h > this.f) {
                remove(this.j.entrySet().iterator().next().getKey());
            }
            this.i.close();
            this.i = null;
        } catch (Throwable th) {
            throw th;
        }
    }

    public void delete() {
        close();
        DiskLruCacheUtil.a(this.a);
    }

    public Editor edit(String str) {
        return a(str, -1L);
    }

    public synchronized void flush() {
        try {
            if (this.i == null) {
                throw new IllegalStateException("cache is closed");
            }
            while (this.h > this.f) {
                remove(this.j.entrySet().iterator().next().getKey());
            }
            this.i.flush();
        } catch (Throwable th) {
            throw th;
        }
    }

    public synchronized Snapshot get(String str) {
        Throwable th;
        InputStream inputStream;
        try {
            try {
                if (this.i == null) {
                    throw new IllegalStateException("cache is closed");
                }
                if (!o.matcher(str).matches()) {
                    throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,64}: \"" + str + "\"");
                }
                c cVar = this.j.get(str);
                if (cVar == null) {
                    return null;
                }
                if (!cVar.c) {
                    return null;
                }
                InputStream[] inputStreamArr = new InputStream[this.g];
                for (int i = 0; i < this.g; i++) {
                    try {
                        try {
                            inputStreamArr[i] = new FileInputStream(cVar.a(i));
                        } catch (Throwable th2) {
                            th = th2;
                            throw th;
                        }
                    } catch (FileNotFoundException unused) {
                        for (int i2 = 0; i2 < this.g && (inputStream = inputStreamArr[i2]) != null; i2++) {
                            DiskLruCacheUtil.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.m.submit(this.n);
                }
                return new Snapshot(str, cVar.e, inputStreamArr, cVar.b);
            } catch (Throwable th3) {
                th = th3;
                th = th;
                throw th;
            }
        } catch (Throwable th4) {
            th = th4;
            th = th;
            throw th;
        }
    }

    public File getDirectory() {
        return this.a;
    }

    public synchronized long getMaxSize() {
        return this.f;
    }

    public synchronized boolean isClosed() {
        return this.i == null;
    }

    public synchronized boolean remove(String str) {
        try {
            if (this.i == null) {
                throw new IllegalStateException("cache is closed");
            }
            if (!o.matcher(str).matches()) {
                throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,64}: \"" + str + "\"");
            }
            c cVar = this.j.get(str);
            if (cVar != null && cVar.d == null) {
                for (int i = 0; i < this.g; i++) {
                    File a2 = cVar.a(i);
                    if (a2.exists() && !a2.delete()) {
                        throw new IOException("failed to delete " + a2);
                    }
                    long j = this.h;
                    long[] jArr = cVar.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.m.submit(this.n);
                }
                return true;
            }
            return false;
        } finally {
        }
    }

    public synchronized void setMaxSize(long j) {
        this.f = j;
        this.m.submit(this.n);
    }

    public synchronized long size() {
        return this.h;
    }

    public static void a(DiskLruCache diskLruCache, Editor editor, boolean z) {
        int i;
        synchronized (diskLruCache) {
            try {
                c cVar = editor.a;
                if (cVar.d != editor) {
                    throw new IllegalStateException();
                }
                if (z && !cVar.c) {
                    for (int i2 = 0; i2 < diskLruCache.g; i2++) {
                        if (!editor.b[i2]) {
                            editor.abort();
                            throw new IllegalStateException("Newly created entry didn't create value for index " + i2);
                        }
                        if (!cVar.b(i2).exists()) {
                            editor.abort();
                            break;
                        }
                    }
                }
                for (int i3 = 0; i3 < diskLruCache.g; i3++) {
                    File b2 = cVar.b(i3);
                    if (z) {
                        if (b2.exists()) {
                            File a2 = cVar.a(i3);
                            b2.renameTo(a2);
                            long j = cVar.b[i3];
                            long length = a2.length();
                            cVar.b[i3] = length;
                            diskLruCache.h = (diskLruCache.h - j) + length;
                        }
                    } else if (b2.exists() && !b2.delete()) {
                        throw new IOException();
                    }
                }
                diskLruCache.k++;
                cVar.d = null;
                if (cVar.c || z) {
                    cVar.c = true;
                    BufferedWriter bufferedWriter = diskLruCache.i;
                    StringBuilder sb = new StringBuilder("CLEAN ");
                    sb.append(cVar.a);
                    StringBuilder sb2 = new StringBuilder();
                    for (long j2 : cVar.b) {
                        sb2.append(' ');
                        sb2.append(j2);
                    }
                    sb.append(sb2.toString());
                    sb.append('\n');
                    bufferedWriter.write(sb.toString());
                    if (z) {
                        long j3 = diskLruCache.l;
                        diskLruCache.l = 1 + j3;
                        cVar.e = j3;
                    }
                } else {
                    diskLruCache.j.remove(cVar.a);
                    diskLruCache.i.write("REMOVE " + cVar.a + '\n');
                }
                diskLruCache.i.flush();
                if (diskLruCache.h > diskLruCache.f || ((i = diskLruCache.k) >= 2000 && i >= diskLruCache.j.size())) {
                    diskLruCache.m.submit(diskLruCache.n);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized Editor a(String str, long j) {
        if (this.i != null) {
            if (o.matcher(str).matches()) {
                c cVar = this.j.get(str);
                if (j != -1 && (cVar == null || cVar.e != j)) {
                    return null;
                }
                if (cVar == null) {
                    cVar = new c(str);
                    this.j.put(str, cVar);
                } else if (cVar.d != null) {
                    return null;
                }
                Editor editor = new Editor(cVar);
                cVar.d = editor;
                this.i.write("DIRTY " + str + '\n');
                this.i.flush();
                return editor;
            }
            throw new IllegalArgumentException("keys must match regex [a-z0-9_-]{1,64}: \"" + str + "\"");
        }
        throw new IllegalStateException("cache is closed");
    }

    public final void a() {
        File file = this.c;
        if (file.exists() && !file.delete()) {
            throw new IOException();
        }
        Iterator<c> it = this.j.values().iterator();
        while (it.hasNext()) {
            c 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) {
                    File a2 = next.a(i);
                    if (a2.exists() && !a2.delete()) {
                        throw new IOException();
                    }
                    File b2 = next.b(i);
                    if (b2.exists() && !b2.delete()) {
                        throw new IOException();
                    }
                    i++;
                }
                it.remove();
            }
        }
    }

    public final void a(String str) {
        String substring;
        int indexOf = str.indexOf(32);
        if (indexOf == -1) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
        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);
        }
        c cVar = this.j.get(substring);
        if (cVar == null) {
            cVar = new c(substring);
            this.j.put(substring, cVar);
        }
        if (indexOf2 != -1 && indexOf == 5 && str.startsWith("CLEAN")) {
            String[] split = str.substring(indexOf2 + 1).split(" ");
            cVar.c = true;
            cVar.d = null;
            if (split.length == DiskLruCache.this.g) {
                for (int i2 = 0; i2 < split.length; i2++) {
                    try {
                        cVar.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")) {
            cVar.d = new Editor(cVar);
        } else if (indexOf2 != -1 || indexOf != 4 || !str.startsWith("READ")) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
    }
}