MD5 校验值:d7f913ab91d7343430e85d5ce2e5aff0
DiskLruCache.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.mopub.common; 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.io.Writer; 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; import ne.g; public final class DiskLruCache implements Closeable { public static final Pattern f18562o = Pattern.compile("[a-z0-9_-]{1,64}"); public static final OutputStream f18563p = new b(); public final File f18564a; public final File f18565b; public final File f18566c; public final File f18567d; public final int f18568e; public long f18569f; public final int f18570g; public Writer f18572i; public int f18574k; public long f18571h = 0; public final LinkedHashMap<String, c> f18573j = new LinkedHashMap<>(0, 0.75f, true); public long f18575l = 0; public final ThreadPoolExecutor f18576m = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue()); public final Callable<Void> f18577n = new a(); public final class Editor { public final c f18578a; public final boolean[] f18579b; public boolean f18580c; public boolean f18581d; public class a extends FilterOutputStream { public a(OutputStream outputStream, a aVar) { super(outputStream); } @Override public void close() { try { ((FilterOutputStream) this).out.close(); } catch (IOException unused) { Editor.this.f18580c = true; } } @Override public void flush() { try { ((FilterOutputStream) this).out.flush(); } catch (IOException unused) { Editor.this.f18580c = true; } } @Override public void write(int i10) { try { ((FilterOutputStream) this).out.write(i10); } catch (IOException unused) { Editor.this.f18580c = true; } } @Override public void write(byte[] bArr, int i10, int i11) { try { ((FilterOutputStream) this).out.write(bArr, i10, i11); } catch (IOException unused) { Editor.this.f18580c = true; } } } public Editor(c cVar, a aVar) { this.f18578a = cVar; this.f18579b = cVar.f18592c ? null : new boolean[DiskLruCache.this.f18570g]; } public void abort() { DiskLruCache.b(DiskLruCache.this, this, false); } public void abortUnlessCommitted() { if (this.f18581d) { return; } try { abort(); } catch (IOException unused) { } } public void commit() { if (this.f18580c) { DiskLruCache.b(DiskLruCache.this, this, false); DiskLruCache.this.remove(this.f18578a.f18590a); } else { DiskLruCache.b(DiskLruCache.this, this, true); } this.f18581d = true; } public String getString(int i10) { InputStream newInputStream = newInputStream(i10); if (newInputStream != null) { return DiskLruCache.a(newInputStream); } return null; } public InputStream newInputStream(int i10) { synchronized (DiskLruCache.this) { c cVar = this.f18578a; if (cVar.f18593d != this) { throw new IllegalStateException(); } if (!cVar.f18592c) { return null; } try { return new FileInputStream(this.f18578a.getCleanFile(i10)); } catch (FileNotFoundException unused) { return null; } } } public OutputStream newOutputStream(int i10) { FileOutputStream fileOutputStream; a aVar; synchronized (DiskLruCache.this) { c cVar = this.f18578a; if (cVar.f18593d != this) { throw new IllegalStateException(); } if (!cVar.f18592c) { this.f18579b[i10] = true; } File dirtyFile = cVar.getDirtyFile(i10); try { fileOutputStream = new FileOutputStream(dirtyFile); } catch (FileNotFoundException unused) { DiskLruCache.this.f18564a.mkdirs(); try { fileOutputStream = new FileOutputStream(dirtyFile); } catch (FileNotFoundException unused2) { return DiskLruCache.f18563p; } } aVar = new a(fileOutputStream, null); } return aVar; } public void set(int i10, String str) { OutputStreamWriter outputStreamWriter = null; try { OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(newOutputStream(i10), DiskLruCacheUtil.f18597b); try { outputStreamWriter2.write(str); DiskLruCacheUtil.a(outputStreamWriter2); } catch (Throwable th2) { th = th2; outputStreamWriter = outputStreamWriter2; DiskLruCacheUtil.a(outputStreamWriter); throw th; } } catch (Throwable th3) { th = th3; } } } public final class Snapshot implements Closeable { public final String f18584a; public final long f18585b; public final InputStream[] f18586c; public final long[] f18587d; public Snapshot(String str, long j10, InputStream[] inputStreamArr, long[] jArr, a aVar) { this.f18584a = str; this.f18585b = j10; this.f18586c = inputStreamArr; this.f18587d = jArr; } @Override public void close() { for (InputStream inputStream : this.f18586c) { DiskLruCacheUtil.a(inputStream); } } public Editor edit() { DiskLruCache diskLruCache = DiskLruCache.this; String str = this.f18584a; long j10 = this.f18585b; Pattern pattern = DiskLruCache.f18562o; return diskLruCache.g(str, j10); } public InputStream getInputStream(int i10) { return this.f18586c[i10]; } public long getLength(int i10) { return this.f18587d[i10]; } public String getString(int i10) { return DiskLruCache.a(getInputStream(i10)); } } public class a implements Callable<Void> { public a() { } @Override public Void call() { synchronized (DiskLruCache.this) { DiskLruCache diskLruCache = DiskLruCache.this; if (diskLruCache.f18572i == null) { return null; } diskLruCache.v(); if (DiskLruCache.this.h()) { DiskLruCache.this.l(); DiskLruCache.this.f18574k = 0; } return null; } } } public static class b extends OutputStream { @Override public void write(int i10) { } } public final class c { public final String f18590a; public final long[] f18591b; public boolean f18592c; public Editor f18593d; public long f18594e; public c(String str, a aVar) { this.f18590a = str; this.f18591b = new long[DiskLruCache.this.f18570g]; } public final IOException a(String[] strArr) { StringBuilder a10 = android.support.v4.media.b.a("unexpected journal line: "); a10.append(Arrays.toString(strArr)); throw new IOException(a10.toString()); } public File getCleanFile(int i10) { return new File(DiskLruCache.this.f18564a, this.f18590a + "." + i10); } public File getDirtyFile(int i10) { return new File(DiskLruCache.this.f18564a, this.f18590a + "." + i10 + ".tmp"); } public String getLengths() { StringBuilder sb2 = new StringBuilder(); for (long j10 : this.f18591b) { sb2.append(' '); sb2.append(j10); } return sb2.toString(); } } public DiskLruCache(File file, int i10, int i11, long j10) { this.f18564a = file; this.f18568e = i10; this.f18565b = new File(file, "journal"); this.f18566c = new File(file, "journal.tmp"); this.f18567d = new File(file, "journal.bkp"); this.f18570g = i11; this.f18569f = j10; } public static String a(InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DiskLruCacheUtil.f18597b); try { StringWriter stringWriter = new StringWriter(); char[] cArr = new char[1024]; while (true) { int read = inputStreamReader.read(cArr); if (read == -1) { return stringWriter.toString(); } stringWriter.write(cArr, 0, read); } } finally { inputStreamReader.close(); } } public static void b(DiskLruCache diskLruCache, Editor editor, boolean z10) { synchronized (diskLruCache) { c cVar = editor.f18578a; if (cVar.f18593d != editor) { throw new IllegalStateException(); } if (z10 && !cVar.f18592c) { for (int i10 = 0; i10 < diskLruCache.f18570g; i10++) { if (!editor.f18579b[i10]) { editor.abort(); throw new IllegalStateException("Newly created entry didn't create value for index " + i10); } if (!cVar.getDirtyFile(i10).exists()) { editor.abort(); return; } } } for (int i11 = 0; i11 < diskLruCache.f18570g; i11++) { File dirtyFile = cVar.getDirtyFile(i11); if (!z10) { d(dirtyFile); } else if (dirtyFile.exists()) { File cleanFile = cVar.getCleanFile(i11); dirtyFile.renameTo(cleanFile); long j10 = cVar.f18591b[i11]; long length = cleanFile.length(); cVar.f18591b[i11] = length; diskLruCache.f18571h = (diskLruCache.f18571h - j10) + length; } } diskLruCache.f18574k++; cVar.f18593d = null; if (cVar.f18592c || z10) { cVar.f18592c = true; diskLruCache.f18572i.write("CLEAN " + cVar.f18590a + cVar.getLengths() + '\n'); if (z10) { long j11 = diskLruCache.f18575l; diskLruCache.f18575l = 1 + j11; cVar.f18594e = j11; } } else { diskLruCache.f18573j.remove(cVar.f18590a); diskLruCache.f18572i.write("REMOVE " + cVar.f18590a + '\n'); } diskLruCache.f18572i.flush(); if (diskLruCache.f18571h > diskLruCache.f18569f || diskLruCache.h()) { diskLruCache.f18576m.submit(diskLruCache.f18577n); } } } public static void d(File file) { if (file.exists() && !file.delete()) { throw new IOException(); } } public static DiskLruCache open(File file, int i10, int i11, long j10) { if (j10 <= 0) { throw new IllegalArgumentException("maxSize <= 0"); } if (i11 <= 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 { u(file2, file3, false); } } DiskLruCache diskLruCache = new DiskLruCache(file, i10, i11, j10); if (diskLruCache.f18565b.exists()) { try { diskLruCache.j(); diskLruCache.i(); diskLruCache.f18572i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(diskLruCache.f18565b, true), DiskLruCacheUtil.f18596a)); return diskLruCache; } catch (IOException e10) { System.out.println("DiskLruCache " + file + " is corrupt: " + e10.getMessage() + ", removing"); diskLruCache.delete(); } } file.mkdirs(); DiskLruCache diskLruCache2 = new DiskLruCache(file, i10, i11, j10); diskLruCache2.l(); return diskLruCache2; } public static void u(File file, File file2, boolean z10) { if (z10) { d(file2); } if (!file.renameTo(file2)) { throw new IOException(); } } public final void c() { if (this.f18572i == null) { throw new IllegalStateException("cache is closed"); } } @Override public synchronized void close() { if (this.f18572i == null) { return; } Iterator it = new ArrayList(this.f18573j.values()).iterator(); while (it.hasNext()) { Editor editor = ((c) it.next()).f18593d; if (editor != null) { editor.abort(); } } v(); this.f18572i.close(); this.f18572i = null; } public void delete() { close(); DiskLruCacheUtil.b(this.f18564a); } public Editor edit(String str) { return g(str, -1L); } public synchronized void flush() { c(); v(); this.f18572i.flush(); } public final synchronized Editor g(String str, long j10) { c(); x(str); c cVar = this.f18573j.get(str); if (j10 != -1 && (cVar == null || cVar.f18594e != j10)) { return null; } if (cVar == null) { cVar = new c(str, null); this.f18573j.put(str, cVar); } else if (cVar.f18593d != null) { return null; } Editor editor = new Editor(cVar, null); cVar.f18593d = editor; this.f18572i.write("DIRTY " + str + '\n'); this.f18572i.flush(); return editor; } public synchronized Snapshot get(String str) { c(); x(str); c cVar = this.f18573j.get(str); if (cVar == null) { return null; } if (!cVar.f18592c) { return null; } InputStream[] inputStreamArr = new InputStream[this.f18570g]; for (int i10 = 0; i10 < this.f18570g; i10++) { try { inputStreamArr[i10] = new FileInputStream(cVar.getCleanFile(i10)); } catch (FileNotFoundException unused) { for (int i11 = 0; i11 < this.f18570g && inputStreamArr[i11] != null; i11++) { DiskLruCacheUtil.a(inputStreamArr[i11]); } return null; } } this.f18574k++; this.f18572i.append((CharSequence) ("READ " + str + '\n')); if (h()) { this.f18576m.submit(this.f18577n); } return new Snapshot(str, cVar.f18594e, inputStreamArr, cVar.f18591b, null); } public File getDirectory() { return this.f18564a; } public synchronized long getMaxSize() { return this.f18569f; } public final boolean h() { int i10 = this.f18574k; return i10 >= 2000 && i10 >= this.f18573j.size(); } public final void i() { d(this.f18566c); Iterator<c> it = this.f18573j.values().iterator(); while (it.hasNext()) { c next = it.next(); int i10 = 0; if (next.f18593d == null) { while (i10 < this.f18570g) { this.f18571h += next.f18591b[i10]; i10++; } } else { next.f18593d = null; while (i10 < this.f18570g) { d(next.getCleanFile(i10)); d(next.getDirtyFile(i10)); i10++; } it.remove(); } } } public synchronized boolean isClosed() { return this.f18572i == null; } public final void j() { g gVar = new g(new FileInputStream(this.f18565b), DiskLruCacheUtil.f18596a); try { String readLine = gVar.readLine(); String readLine2 = gVar.readLine(); String readLine3 = gVar.readLine(); String readLine4 = gVar.readLine(); String readLine5 = gVar.readLine(); if (!"libcore.io.DiskLruCache".equals(readLine) || !"1".equals(readLine2) || !Integer.toString(this.f18568e).equals(readLine3) || !Integer.toString(this.f18570g).equals(readLine4) || !"".equals(readLine5)) { throw new IOException("unexpected journal header: [" + readLine + ", " + readLine2 + ", " + readLine4 + ", " + readLine5 + "]"); } int i10 = 0; while (true) { try { k(gVar.readLine()); i10++; } catch (EOFException unused) { this.f18574k = i10 - this.f18573j.size(); DiskLruCacheUtil.a(gVar); return; } } } catch (Throwable th2) { DiskLruCacheUtil.a(gVar); throw th2; } } public final void k(String str) { String substring; int indexOf = str.indexOf(32); if (indexOf == -1) { throw new IOException(i.c.a("unexpected journal line: ", str)); } int i10 = indexOf + 1; int indexOf2 = str.indexOf(32, i10); if (indexOf2 == -1) { substring = str.substring(i10); if (indexOf == 6 && str.startsWith("REMOVE")) { this.f18573j.remove(substring); return; } } else { substring = str.substring(i10, indexOf2); } c cVar = this.f18573j.get(substring); if (cVar == null) { cVar = new c(substring, null); this.f18573j.put(substring, cVar); } if (indexOf2 == -1 || indexOf != 5 || !str.startsWith("CLEAN")) { if (indexOf2 == -1 && indexOf == 5 && str.startsWith("DIRTY")) { cVar.f18593d = new Editor(cVar, null); return; } else { if (indexOf2 != -1 || indexOf != 4 || !str.startsWith("READ")) { throw new IOException(i.c.a("unexpected journal line: ", str)); } return; } } String[] split = str.substring(indexOf2 + 1).split(" "); cVar.f18592c = true; cVar.f18593d = null; if (split.length != DiskLruCache.this.f18570g) { cVar.a(split); throw null; } for (int i11 = 0; i11 < split.length; i11++) { try { cVar.f18591b[i11] = Long.parseLong(split[i11]); } catch (NumberFormatException unused) { cVar.a(split); throw null; } } } public final synchronized void l() { Writer writer = this.f18572i; if (writer != null) { writer.close(); } BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f18566c), DiskLruCacheUtil.f18596a)); try { bufferedWriter.write("libcore.io.DiskLruCache"); bufferedWriter.write("\n"); bufferedWriter.write("1"); bufferedWriter.write("\n"); bufferedWriter.write(Integer.toString(this.f18568e)); bufferedWriter.write("\n"); bufferedWriter.write(Integer.toString(this.f18570g)); bufferedWriter.write("\n"); bufferedWriter.write("\n"); for (c cVar : this.f18573j.values()) { if (cVar.f18593d != null) { bufferedWriter.write("DIRTY " + cVar.f18590a + '\n'); } else { bufferedWriter.write("CLEAN " + cVar.f18590a + cVar.getLengths() + '\n'); } } bufferedWriter.close(); if (this.f18565b.exists()) { u(this.f18565b, this.f18567d, true); } u(this.f18566c, this.f18565b, false); this.f18567d.delete(); this.f18572i = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.f18565b, true), DiskLruCacheUtil.f18596a)); } catch (Throwable th2) { bufferedWriter.close(); throw th2; } } public synchronized boolean remove(String str) { c(); x(str); c cVar = this.f18573j.get(str); if (cVar != null && cVar.f18593d == null) { for (int i10 = 0; i10 < this.f18570g; i10++) { File cleanFile = cVar.getCleanFile(i10); if (cleanFile.exists() && !cleanFile.delete()) { throw new IOException("failed to delete " + cleanFile); } long j10 = this.f18571h; long[] jArr = cVar.f18591b; this.f18571h = j10 - jArr[i10]; jArr[i10] = 0; } this.f18574k++; this.f18572i.append((CharSequence) ("REMOVE " + str + '\n')); this.f18573j.remove(str); if (h()) { this.f18576m.submit(this.f18577n); } return true; } return false; } public synchronized void setMaxSize(long j10) { this.f18569f = j10; this.f18576m.submit(this.f18577n); } public synchronized long size() { return this.f18571h; } public final void v() { while (this.f18571h > this.f18569f) { remove(this.f18573j.entrySet().iterator().next().getKey()); } } public final void x(String str) { if (!f18562o.matcher(str).matches()) { throw new IllegalArgumentException(android.support.v4.media.c.a("keys must match regex [a-z0-9_-]{1,64}: \"", str, "\"")); } } }