alexandroid/decompiled/jadx/sources/com/bumptech/glide/disklrucache/DiskLruCache.java
KaKi87 a47fad5135 📦 Add decompiled Alexa 2.2.730155.0 (jadx, apktool, Hermes strings)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 00:04:16 +01:00

734 lines
27 KiB
Java

package com.bumptech.glide.disklrucache;
import android.annotation.TargetApi;
import android.os.StrictMode;
import com.amazon.alexa.ra.core.ui.markdown.parser.formatter.MarkdownConstants;
import com.facebook.cache.disk.DefaultDiskStorage;
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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import okio.Buffer$$ExternalSyntheticBUOutline0;
import rx.Single$$ExternalSyntheticBUOutline0;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
/* loaded from: classes6.dex */
public final class DiskLruCache implements Closeable {
static final long ANY_SEQUENCE_NUMBER = -1;
private static final String CLEAN = "CLEAN";
private static final String DIRTY = "DIRTY";
static final String JOURNAL_FILE = "journal";
static final String JOURNAL_FILE_BACKUP = "journal.bkp";
static final String JOURNAL_FILE_TEMP = "journal.tmp";
static final String MAGIC = "libcore.io.DiskLruCache";
private static final String READ = "READ";
private static final String REMOVE = "REMOVE";
static final String VERSION_1 = "1";
private final int appVersion;
private final File directory;
private final File journalFile;
private final File journalFileBackup;
private final File journalFileTmp;
private Writer journalWriter;
private long maxSize;
private int redundantOpCount;
private final int valueCount;
private long size = 0;
private final LinkedHashMap<String, Entry> lruEntries = new LinkedHashMap<>(0, 0.75f, true);
private long nextSequenceNumber = 0;
final ThreadPoolExecutor executorService = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new DiskLruCacheThreadFactory());
private final Callable<Void> cleanupCallable = new Callable<Void>() { // from class: com.bumptech.glide.disklrucache.DiskLruCache.1
@Override // java.util.concurrent.Callable
public Void call() throws Exception {
synchronized (DiskLruCache.this) {
try {
if (DiskLruCache.this.journalWriter == null) {
return null;
}
DiskLruCache.this.trimToSize();
if (DiskLruCache.this.journalRebuildRequired()) {
DiskLruCache.this.rebuildJournal();
DiskLruCache.this.redundantOpCount = 0;
}
return null;
} catch (Throwable th) {
throw th;
}
}
}
};
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static final class DiskLruCacheThreadFactory implements ThreadFactory {
private DiskLruCacheThreadFactory() {
}
@Override // java.util.concurrent.ThreadFactory
public synchronized Thread newThread(Runnable runnable) {
Thread thread;
thread = new Thread(runnable, "glide-disk-lru-cache-thread");
thread.setPriority(1);
return thread;
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public final class Editor {
private boolean committed;
private final Entry entry;
private final boolean[] written;
private Editor(Entry entry) {
this.entry = entry;
this.written = entry.readable ? null : new boolean[DiskLruCache.this.valueCount];
}
private InputStream newInputStream(int i) throws IOException {
synchronized (DiskLruCache.this) {
if (this.entry.currentEditor != this) {
throw new IllegalStateException();
}
if (!this.entry.readable) {
return null;
}
try {
return new FileInputStream(this.entry.getCleanFile(i));
} catch (FileNotFoundException unused) {
return null;
}
}
}
public void abort() throws IOException {
DiskLruCache.this.completeEdit(this, false);
}
public void abortUnlessCommitted() {
if (this.committed) {
return;
}
try {
abort();
} catch (IOException unused) {
}
}
public void commit() throws IOException {
DiskLruCache.this.completeEdit(this, true);
this.committed = true;
}
public File getFile(int i) throws IOException {
File dirtyFile;
synchronized (DiskLruCache.this) {
try {
if (this.entry.currentEditor != this) {
throw new IllegalStateException();
}
if (!this.entry.readable) {
this.written[i] = true;
}
dirtyFile = this.entry.getDirtyFile(i);
DiskLruCache.this.directory.mkdirs();
} catch (Throwable th) {
throw th;
}
}
return dirtyFile;
}
public String getString(int i) throws IOException {
InputStream newInputStream = newInputStream(i);
if (newInputStream != null) {
return DiskLruCache.inputStreamToString(newInputStream);
}
return null;
}
public void set(int i, String str) throws IOException {
OutputStreamWriter outputStreamWriter = null;
try {
OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(new FileOutputStream(getFile(i)), Util.UTF_8);
try {
outputStreamWriter2.write(str);
Util.closeQuietly(outputStreamWriter2);
} catch (Throwable th) {
th = th;
outputStreamWriter = outputStreamWriter2;
Util.closeQuietly(outputStreamWriter);
throw th;
}
} catch (Throwable th2) {
th = th2;
}
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public final class Entry {
File[] cleanFiles;
private Editor currentEditor;
File[] dirtyFiles;
private final String key;
private final long[] lengths;
private boolean readable;
private long sequenceNumber;
private Entry(String str) {
this.key = str;
this.lengths = new long[DiskLruCache.this.valueCount];
this.cleanFiles = new File[DiskLruCache.this.valueCount];
this.dirtyFiles = new File[DiskLruCache.this.valueCount];
StringBuilder sb = new StringBuilder(str);
sb.append('.');
int length = sb.length();
for (int i = 0; i < DiskLruCache.this.valueCount; i++) {
sb.append(i);
this.cleanFiles[i] = new File(DiskLruCache.this.directory, sb.toString());
sb.append(DefaultDiskStorage.FileType.TEMP);
this.dirtyFiles[i] = new File(DiskLruCache.this.directory, sb.toString());
sb.setLength(length);
}
}
private IOException invalidLengths(String[] strArr) throws IOException {
throw new IOException("unexpected journal line: " + Arrays.toString(strArr));
}
/* JADX INFO: Access modifiers changed from: private */
public void setLengths(String[] strArr) throws IOException {
if (strArr.length != DiskLruCache.this.valueCount) {
throw invalidLengths(strArr);
}
for (int i = 0; i < strArr.length; i++) {
try {
this.lengths[i] = Long.parseLong(strArr[i]);
} catch (NumberFormatException unused) {
throw invalidLengths(strArr);
}
}
}
public File getCleanFile(int i) {
return this.cleanFiles[i];
}
public File getDirtyFile(int i) {
return this.dirtyFiles[i];
}
public String getLengths() throws IOException {
StringBuilder sb = new StringBuilder();
for (long j : this.lengths) {
sb.append(' ');
sb.append(j);
}
return sb.toString();
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public final class Value {
private final File[] files;
private final String key;
private final long[] lengths;
private final long sequenceNumber;
private Value(String str, long j, File[] fileArr, long[] jArr) {
this.key = str;
this.sequenceNumber = j;
this.files = fileArr;
this.lengths = jArr;
}
public Editor edit() throws IOException {
return DiskLruCache.this.edit(this.key, this.sequenceNumber);
}
public File getFile(int i) {
return this.files[i];
}
public long getLength(int i) {
return this.lengths[i];
}
public String getString(int i) throws IOException {
return DiskLruCache.inputStreamToString(new FileInputStream(this.files[i]));
}
}
private DiskLruCache(File file, int i, int i2, long j) {
this.directory = file;
this.appVersion = i;
this.journalFile = new File(file, JOURNAL_FILE);
this.journalFileTmp = new File(file, JOURNAL_FILE_TEMP);
this.journalFileBackup = new File(file, JOURNAL_FILE_BACKUP);
this.valueCount = i2;
this.maxSize = j;
}
private void checkNotClosed() {
if (this.journalWriter != null) {
return;
}
Single$$ExternalSyntheticBUOutline0.m$2("cache is closed");
}
@TargetApi(26)
private static void closeWriter(Writer writer) throws IOException {
StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
try {
writer.close();
} finally {
StrictMode.setThreadPolicy(threadPolicy);
}
}
/* JADX INFO: Access modifiers changed from: private */
public synchronized void completeEdit(Editor editor, boolean z) throws IOException {
Entry entry = editor.entry;
if (entry.currentEditor != editor) {
throw new IllegalStateException();
}
if (z && !entry.readable) {
for (int i = 0; i < this.valueCount; i++) {
if (!editor.written[i]) {
editor.abort();
throw new IllegalStateException("Newly created entry didn't create value for index " + i);
}
if (!entry.getDirtyFile(i).exists()) {
editor.abort();
return;
}
}
}
for (int i2 = 0; i2 < this.valueCount; i2++) {
File dirtyFile = entry.getDirtyFile(i2);
if (!z) {
deleteIfExists(dirtyFile);
} else if (dirtyFile.exists()) {
File cleanFile = entry.getCleanFile(i2);
dirtyFile.renameTo(cleanFile);
long j = entry.lengths[i2];
long length = cleanFile.length();
entry.lengths[i2] = length;
this.size = (this.size - j) + length;
}
}
this.redundantOpCount++;
entry.currentEditor = null;
if (entry.readable || z) {
entry.readable = true;
this.journalWriter.append((CharSequence) CLEAN);
this.journalWriter.append(' ');
this.journalWriter.append((CharSequence) entry.key);
this.journalWriter.append((CharSequence) entry.getLengths());
this.journalWriter.append('\n');
if (z) {
long j2 = this.nextSequenceNumber;
this.nextSequenceNumber = 1 + j2;
entry.sequenceNumber = j2;
}
} else {
this.lruEntries.remove(entry.key);
this.journalWriter.append((CharSequence) "REMOVE");
this.journalWriter.append(' ');
this.journalWriter.append((CharSequence) entry.key);
this.journalWriter.append('\n');
}
flushWriter(this.journalWriter);
if (this.size > this.maxSize || journalRebuildRequired()) {
this.executorService.submit(this.cleanupCallable);
}
}
private static void deleteIfExists(File file) throws IOException {
if (file.exists() && !file.delete()) {
throw new IOException();
}
}
/* JADX INFO: Access modifiers changed from: private */
public synchronized Editor edit(String str, long j) throws IOException {
checkNotClosed();
Entry entry = this.lruEntries.get(str);
if (j != -1 && (entry == null || entry.sequenceNumber != j)) {
return null;
}
if (entry == null) {
entry = new Entry(str);
this.lruEntries.put(str, entry);
} else if (entry.currentEditor != null) {
return null;
}
Editor editor = new Editor(entry);
entry.currentEditor = editor;
this.journalWriter.append((CharSequence) DIRTY);
this.journalWriter.append(' ');
this.journalWriter.append((CharSequence) str);
this.journalWriter.append('\n');
flushWriter(this.journalWriter);
return editor;
}
@TargetApi(26)
private static void flushWriter(Writer writer) throws IOException {
StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(threadPolicy).permitUnbufferedIo().build());
try {
writer.flush();
} finally {
StrictMode.setThreadPolicy(threadPolicy);
}
}
/* JADX INFO: Access modifiers changed from: private */
public static String inputStreamToString(InputStream inputStream) throws IOException {
return Util.readFully(new InputStreamReader(inputStream, Util.UTF_8));
}
/* JADX INFO: Access modifiers changed from: private */
public boolean journalRebuildRequired() {
int i = this.redundantOpCount;
return i >= 2000 && i >= this.lruEntries.size();
}
public static DiskLruCache open(File file, int i, int i2, long j) throws IOException {
if (j <= 0) {
Single$$ExternalSyntheticBUOutline0.m$3("maxSize <= 0");
return null;
}
if (i2 <= 0) {
Single$$ExternalSyntheticBUOutline0.m$3("valueCount <= 0");
return null;
}
File file2 = new File(file, JOURNAL_FILE_BACKUP);
if (file2.exists()) {
File file3 = new File(file, JOURNAL_FILE);
if (file3.exists()) {
file2.delete();
} else {
renameTo(file2, file3, false);
}
}
DiskLruCache diskLruCache = new DiskLruCache(file, i, i2, j);
if (diskLruCache.journalFile.exists()) {
try {
diskLruCache.readJournal();
diskLruCache.processJournal();
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.rebuildJournal();
return diskLruCache2;
}
private void processJournal() throws IOException {
deleteIfExists(this.journalFileTmp);
Iterator<Entry> it2 = this.lruEntries.values().iterator();
while (it2.hasNext()) {
Entry next = it2.next();
int i = 0;
if (next.currentEditor == null) {
while (i < this.valueCount) {
this.size += next.lengths[i];
i++;
}
} else {
next.currentEditor = null;
while (i < this.valueCount) {
deleteIfExists(next.getCleanFile(i));
deleteIfExists(next.getDirtyFile(i));
i++;
}
it2.remove();
}
}
}
private void readJournal() throws IOException {
StrictLineReader strictLineReader = new StrictLineReader(new FileInputStream(this.journalFile), Util.US_ASCII);
try {
String readLine = strictLineReader.readLine();
String readLine2 = strictLineReader.readLine();
String readLine3 = strictLineReader.readLine();
String readLine4 = strictLineReader.readLine();
String readLine5 = strictLineReader.readLine();
if (!MAGIC.equals(readLine) || !"1".equals(readLine2) || !Integer.toString(this.appVersion).equals(readLine3) || !Integer.toString(this.valueCount).equals(readLine4) || !"".equals(readLine5)) {
throw new IOException("unexpected journal header: [" + readLine + ", " + readLine2 + ", " + readLine4 + ", " + readLine5 + MarkdownConstants.RIGHT_BRACKET);
}
int i = 0;
while (true) {
try {
readJournalLine(strictLineReader.readLine());
i++;
} catch (EOFException unused) {
this.redundantOpCount = i - this.lruEntries.size();
if (strictLineReader.hasUnterminatedLine()) {
rebuildJournal();
} else {
this.journalWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.journalFile, true), Util.US_ASCII));
}
Util.closeQuietly(strictLineReader);
return;
}
}
} catch (Throwable th) {
Util.closeQuietly(strictLineReader);
throw th;
}
}
private void readJournalLine(String str) throws IOException {
String substring;
int indexOf = str.indexOf(32);
if (indexOf == -1) {
Buffer$$ExternalSyntheticBUOutline0.m$2("unexpected journal line: ".concat(str));
return;
}
int i = indexOf + 1;
int indexOf2 = str.indexOf(32, i);
if (indexOf2 == -1) {
substring = str.substring(i);
if (indexOf == 6 && str.startsWith("REMOVE")) {
this.lruEntries.remove(substring);
return;
}
} else {
substring = str.substring(i, indexOf2);
}
Entry entry = this.lruEntries.get(substring);
if (entry == null) {
entry = new Entry(substring);
this.lruEntries.put(substring, entry);
}
if (indexOf2 != -1 && indexOf == 5 && str.startsWith(CLEAN)) {
String[] split = str.substring(indexOf2 + 1).split(" ");
entry.readable = true;
entry.currentEditor = null;
entry.setLengths(split);
return;
}
if (indexOf2 == -1 && indexOf == 5 && str.startsWith(DIRTY)) {
entry.currentEditor = new Editor(entry);
} else {
if (indexOf2 == -1 && indexOf == 4 && str.startsWith("READ")) {
return;
}
Buffer$$ExternalSyntheticBUOutline0.m$2("unexpected journal line: ".concat(str));
}
}
/* JADX INFO: Access modifiers changed from: private */
public synchronized void rebuildJournal() throws IOException {
try {
Writer writer = this.journalWriter;
if (writer != null) {
closeWriter(writer);
}
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.journalFileTmp), Util.US_ASCII));
try {
bufferedWriter.write(MAGIC);
bufferedWriter.write("\n");
bufferedWriter.write("1");
bufferedWriter.write("\n");
bufferedWriter.write(Integer.toString(this.appVersion));
bufferedWriter.write("\n");
bufferedWriter.write(Integer.toString(this.valueCount));
bufferedWriter.write("\n");
bufferedWriter.write("\n");
for (Entry entry : this.lruEntries.values()) {
if (entry.currentEditor != null) {
bufferedWriter.write("DIRTY " + entry.key + '\n');
} else {
bufferedWriter.write("CLEAN " + entry.key + entry.getLengths() + '\n');
}
}
closeWriter(bufferedWriter);
if (this.journalFile.exists()) {
renameTo(this.journalFile, this.journalFileBackup, true);
}
renameTo(this.journalFileTmp, this.journalFile, false);
this.journalFileBackup.delete();
this.journalWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.journalFile, true), Util.US_ASCII));
} catch (Throwable th) {
closeWriter(bufferedWriter);
throw th;
}
} catch (Throwable th2) {
throw th2;
}
}
private static void renameTo(File file, File file2, boolean z) throws IOException {
if (z) {
deleteIfExists(file2);
}
if (!file.renameTo(file2)) {
throw new IOException();
}
}
/* JADX INFO: Access modifiers changed from: private */
public void trimToSize() throws IOException {
while (this.size > this.maxSize) {
remove(this.lruEntries.entrySet().iterator().next().getKey());
}
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public synchronized void close() throws IOException {
try {
if (this.journalWriter == null) {
return;
}
Iterator it2 = new ArrayList(this.lruEntries.values()).iterator();
while (it2.hasNext()) {
Entry entry = (Entry) it2.next();
if (entry.currentEditor != null) {
entry.currentEditor.abort();
}
}
trimToSize();
closeWriter(this.journalWriter);
this.journalWriter = null;
} catch (Throwable th) {
throw th;
}
}
public void delete() throws IOException {
close();
Util.deleteContents(this.directory);
}
public synchronized void flush() throws IOException {
checkNotClosed();
trimToSize();
flushWriter(this.journalWriter);
}
public synchronized Value get(String str) throws IOException {
DiskLruCache diskLruCache;
Throwable th;
try {
try {
checkNotClosed();
Entry entry = this.lruEntries.get(str);
if (entry == null) {
return null;
}
if (!entry.readable) {
return null;
}
for (File file : entry.cleanFiles) {
try {
if (!file.exists()) {
return null;
}
} catch (Throwable th2) {
th = th2;
diskLruCache = this;
throw th;
}
}
this.redundantOpCount++;
this.journalWriter.append((CharSequence) "READ");
this.journalWriter.append(' ');
this.journalWriter.append((CharSequence) str);
this.journalWriter.append('\n');
if (journalRebuildRequired()) {
this.executorService.submit(this.cleanupCallable);
}
return new Value(str, entry.sequenceNumber, entry.cleanFiles, entry.lengths);
} catch (Throwable th3) {
th = th3;
th = th;
throw th;
}
} catch (Throwable th4) {
th = th4;
diskLruCache = this;
th = th;
throw th;
}
}
public File getDirectory() {
return this.directory;
}
public synchronized long getMaxSize() {
return this.maxSize;
}
public synchronized boolean isClosed() {
return this.journalWriter == null;
}
public synchronized boolean remove(String str) throws IOException {
try {
checkNotClosed();
Entry entry = this.lruEntries.get(str);
if (entry != null && entry.currentEditor == null) {
for (int i = 0; i < this.valueCount; i++) {
File cleanFile = entry.getCleanFile(i);
if (cleanFile.exists() && !cleanFile.delete()) {
throw new IOException("failed to delete " + cleanFile);
}
this.size -= entry.lengths[i];
entry.lengths[i] = 0;
}
this.redundantOpCount++;
this.journalWriter.append((CharSequence) "REMOVE");
this.journalWriter.append(' ');
this.journalWriter.append((CharSequence) str);
this.journalWriter.append('\n');
this.lruEntries.remove(str);
if (journalRebuildRequired()) {
this.executorService.submit(this.cleanupCallable);
}
return true;
}
return false;
} finally {
}
}
public synchronized void setMaxSize(long j) {
this.maxSize = j;
this.executorService.submit(this.cleanupCallable);
}
public synchronized long size() {
return this.size;
}
public Editor edit(String str) throws IOException {
return edit(str, -1L);
}
}