package coil.disk;

import A4.d;
import B4.a;
import C4.e;
import C4.j;
import S4.m;
import S4.p;
import S4.w;
import V4.AbstractC0786y;
import V4.C;
import V4.E;
import a.AbstractC0872a;
import coil.util.FileSystems;
import java.io.Closeable;
import java.io.EOFException;
import java.io.Flushable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import n2.AbstractC2613d;
import p5.AbstractC2732b;
import p5.AbstractC2749t;
import p5.C2737g;
import p5.G;
import p5.I;
import p5.InterfaceC2741k;
import p5.J;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
public final class DiskLruCache implements Closeable, Flushable {
    private static final String CLEAN = "CLEAN";
    private static final String DIRTY = "DIRTY";
    public static final String JOURNAL_FILE = "journal";
    public static final String JOURNAL_FILE_BACKUP = "journal.bkp";
    public static final String JOURNAL_FILE_TMP = "journal.tmp";
    public static final String MAGIC = "libcore.io.DiskLruCache";
    private static final String READ = "READ";
    private static final String REMOVE = "REMOVE";
    public static final String VERSION = "1";
    private final int appVersion;
    private final C cleanupScope;
    private boolean closed;
    private final G directory;
    private final DiskLruCache$fileSystem$1 fileSystem;
    private boolean hasJournalErrors;
    private boolean initialized;
    private final G journalFile;
    private final G journalFileBackup;
    private final G journalFileTmp;
    private InterfaceC2741k journalWriter;
    private final LinkedHashMap<String, Entry> lruEntries;
    private final long maxSize;
    private boolean mostRecentRebuildFailed;
    private boolean mostRecentTrimFailed;
    private int operationsSinceRewrite;
    private long size;
    private final int valueCount;
    public static final Companion Companion = new Companion(null);
    private static final m LEGAL_KEY_PATTERN = new m("[a-z0-9_-]{1,120}");

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        public static /* synthetic */ void getJOURNAL_FILE$coil_base_release$annotations() {
        }

        public static /* synthetic */ void getJOURNAL_FILE_BACKUP$coil_base_release$annotations() {
        }

        public static /* synthetic */ void getJOURNAL_FILE_TMP$coil_base_release$annotations() {
        }

        public static /* synthetic */ void getMAGIC$coil_base_release$annotations() {
        }

        public static /* synthetic */ void getVERSION$coil_base_release$annotations() {
        }

        private Companion() {
        }
    }

    public final class Editor {
        private boolean closed;
        private final Entry entry;
        private final boolean[] written;

        public Editor(Entry entry) {
            this.entry = entry;
            this.written = new boolean[DiskLruCache.this.valueCount];
        }

        private final void complete(boolean z6) {
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                try {
                    if (this.closed) {
                        throw new IllegalStateException("editor is closed");
                    }
                    if (o.c(this.entry.getCurrentEditor(), this)) {
                        diskLruCache.completeEdit(this, z6);
                    }
                    this.closed = true;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public final void abort() {
            complete(false);
        }

        public final void commit() {
            complete(true);
        }

        public final Snapshot commitAndGet() {
            Snapshot snapshot;
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                commit();
                snapshot = diskLruCache.get(this.entry.getKey());
            }
            return snapshot;
        }

        public final void detach() {
            if (o.c(this.entry.getCurrentEditor(), this)) {
                this.entry.setZombie(true);
            }
        }

        public final G file(int i6) {
            G g6;
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                if (this.closed) {
                    throw new IllegalStateException("editor is closed");
                }
                this.written[i6] = true;
                G g7 = this.entry.getDirtyFiles().get(i6);
                FileSystems.createFile(diskLruCache.fileSystem, g7);
                g6 = g7;
            }
            return g6;
        }

        public final Entry getEntry() {
            return this.entry;
        }

        public final boolean[] getWritten() {
            return this.written;
        }
    }

    public final class Entry {
        private final ArrayList<G> cleanFiles;
        private Editor currentEditor;
        private final ArrayList<G> dirtyFiles;
        private final String key;
        private final long[] lengths;
        private int lockingSnapshotCount;
        private boolean readable;
        private boolean zombie;

        public Entry(String str) {
            this.key = str;
            this.lengths = new long[DiskLruCache.this.valueCount];
            this.cleanFiles = new ArrayList<>(DiskLruCache.this.valueCount);
            this.dirtyFiles = new ArrayList<>(DiskLruCache.this.valueCount);
            StringBuilder sb = new StringBuilder(str);
            sb.append('.');
            int length = sb.length();
            int i6 = DiskLruCache.this.valueCount;
            for (int i7 = 0; i7 < i6; i7++) {
                sb.append(i7);
                this.cleanFiles.add(DiskLruCache.this.directory.e(sb.toString()));
                sb.append(".tmp");
                this.dirtyFiles.add(DiskLruCache.this.directory.e(sb.toString()));
                sb.setLength(length);
            }
        }

        public final ArrayList<G> getCleanFiles() {
            return this.cleanFiles;
        }

        public final Editor getCurrentEditor() {
            return this.currentEditor;
        }

        public final ArrayList<G> getDirtyFiles() {
            return this.dirtyFiles;
        }

        public final String getKey() {
            return this.key;
        }

        public final long[] getLengths() {
            return this.lengths;
        }

        public final int getLockingSnapshotCount() {
            return this.lockingSnapshotCount;
        }

        public final boolean getReadable() {
            return this.readable;
        }

        public final boolean getZombie() {
            return this.zombie;
        }

        public final void setCurrentEditor(Editor editor) {
            this.currentEditor = editor;
        }

        public final void setLengths(List<String> list) throws IOException {
            if (list.size() != DiskLruCache.this.valueCount) {
                throw new IOException("unexpected journal line: " + list);
            }
            try {
                int size = list.size();
                for (int i6 = 0; i6 < size; i6++) {
                    this.lengths[i6] = Long.parseLong(list.get(i6));
                }
            } catch (NumberFormatException unused) {
                throw new IOException("unexpected journal line: " + list);
            }
        }

        public final void setLockingSnapshotCount(int i6) {
            this.lockingSnapshotCount = i6;
        }

        public final void setReadable(boolean z6) {
            this.readable = z6;
        }

        public final void setZombie(boolean z6) {
            this.zombie = z6;
        }

        public final Snapshot snapshot() {
            if (!this.readable || this.currentEditor != null || this.zombie) {
                return null;
            }
            ArrayList<G> arrayList = this.cleanFiles;
            DiskLruCache diskLruCache = DiskLruCache.this;
            int size = arrayList.size();
            for (int i6 = 0; i6 < size; i6++) {
                if (!diskLruCache.fileSystem.exists(arrayList.get(i6))) {
                    try {
                        diskLruCache.removeEntry(this);
                    } catch (IOException unused) {
                    }
                    return null;
                }
            }
            this.lockingSnapshotCount++;
            return DiskLruCache.this.new Snapshot(this);
        }

        public final void writeLengths(InterfaceC2741k interfaceC2741k) {
            for (long j4 : this.lengths) {
                interfaceC2741k.h(32).w(j4);
            }
        }
    }

    public final class Snapshot implements Closeable {
        private boolean closed;
        private final Entry entry;

        public Snapshot(Entry entry) {
            this.entry = entry;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.closed) {
                return;
            }
            this.closed = true;
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                this.entry.setLockingSnapshotCount(r1.getLockingSnapshotCount() - 1);
                if (this.entry.getLockingSnapshotCount() == 0 && this.entry.getZombie()) {
                    diskLruCache.removeEntry(this.entry);
                }
            }
        }

        public final Editor closeAndEdit() {
            Editor editorEdit;
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                close();
                editorEdit = diskLruCache.edit(this.entry.getKey());
            }
            return editorEdit;
        }

        public final G file(int i6) {
            if (this.closed) {
                throw new IllegalStateException("snapshot is closed");
            }
            return this.entry.getCleanFiles().get(i6);
        }

        public final Entry getEntry() {
            return this.entry;
        }
    }

    /* JADX INFO: renamed from: coil.disk.DiskLruCache$launchCleanup$1, reason: invalid class name */
    @e(c = "coil.disk.DiskLruCache$launchCleanup$1", f = "DiskLruCache.kt", l = {}, m = "invokeSuspend")
    public static final class AnonymousClass1 extends j implements J4.e {
        int label;

        public AnonymousClass1(d dVar) {
            super(2, dVar);
        }

        @Override // C4.a
        public final d create(Object obj, d dVar) {
            return DiskLruCache.this.new AnonymousClass1(dVar);
        }

        @Override // J4.e
        public final Object invoke(C c6, d dVar) {
            return ((AnonymousClass1) create(c6, dVar)).invokeSuspend(C3137A.f25453a);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            a aVar = a.f492a;
            if (this.label != 0) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            AbstractC2467b.l(obj);
            DiskLruCache diskLruCache = DiskLruCache.this;
            synchronized (diskLruCache) {
                if (!diskLruCache.initialized || diskLruCache.closed) {
                    return C3137A.f25453a;
                }
                try {
                    diskLruCache.trimToSize();
                } catch (IOException unused) {
                    diskLruCache.mostRecentTrimFailed = true;
                }
                try {
                    if (diskLruCache.journalRewriteRequired()) {
                        diskLruCache.writeJournal();
                    }
                } catch (IOException unused2) {
                    diskLruCache.mostRecentRebuildFailed = true;
                    diskLruCache.journalWriter = AbstractC2732b.b(new C2737g());
                }
                return C3137A.f25453a;
            }
        }
    }

    public DiskLruCache(AbstractC2749t abstractC2749t, G g6, AbstractC0786y abstractC0786y, long j4, int i6, int i7) {
        this.directory = g6;
        this.maxSize = j4;
        this.appVersion = i6;
        this.valueCount = i7;
        if (j4 <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        if (i7 <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        }
        this.journalFile = g6.e(JOURNAL_FILE);
        this.journalFileTmp = g6.e(JOURNAL_FILE_TMP);
        this.journalFileBackup = g6.e(JOURNAL_FILE_BACKUP);
        this.lruEntries = new LinkedHashMap<>(0, 0.75f, true);
        this.cleanupScope = E.c(AbstractC0872a.a0(E.d(), abstractC0786y.i(1)));
        this.fileSystem = new DiskLruCache$fileSystem$1(abstractC2749t);
    }

    private final void checkNotClosed() {
        if (this.closed) {
            throw new IllegalStateException("cache is closed");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final synchronized void completeEdit(Editor editor, boolean z6) {
        Entry entry = editor.getEntry();
        if (!o.c(entry.getCurrentEditor(), editor)) {
            throw new IllegalStateException("Check failed.");
        }
        int i6 = 0;
        if (!z6 || entry.getZombie()) {
            int i7 = this.valueCount;
            while (i6 < i7) {
                this.fileSystem.delete(entry.getDirtyFiles().get(i6));
                i6++;
            }
        } else {
            int i8 = this.valueCount;
            for (int i9 = 0; i9 < i8; i9++) {
                if (editor.getWritten()[i9] && !this.fileSystem.exists(entry.getDirtyFiles().get(i9))) {
                    editor.abort();
                    return;
                }
            }
            int i10 = this.valueCount;
            while (i6 < i10) {
                G g6 = entry.getDirtyFiles().get(i6);
                G g7 = entry.getCleanFiles().get(i6);
                if (this.fileSystem.exists(g6)) {
                    this.fileSystem.atomicMove(g6, g7);
                } else {
                    FileSystems.createFile(this.fileSystem, entry.getCleanFiles().get(i6));
                }
                long j4 = entry.getLengths()[i6];
                Long l = this.fileSystem.metadata(g7).f23118d;
                long jLongValue = l != null ? l.longValue() : 0L;
                entry.getLengths()[i6] = jLongValue;
                this.size = (this.size - j4) + jLongValue;
                i6++;
            }
        }
        entry.setCurrentEditor(null);
        if (entry.getZombie()) {
            removeEntry(entry);
            return;
        }
        this.operationsSinceRewrite++;
        InterfaceC2741k interfaceC2741k = this.journalWriter;
        o.e(interfaceC2741k);
        if (z6 || entry.getReadable()) {
            entry.setReadable(true);
            interfaceC2741k.n(CLEAN);
            interfaceC2741k.h(32);
            interfaceC2741k.n(entry.getKey());
            entry.writeLengths(interfaceC2741k);
            interfaceC2741k.h(10);
        } else {
            this.lruEntries.remove(entry.getKey());
            interfaceC2741k.n(REMOVE);
            interfaceC2741k.h(32);
            interfaceC2741k.n(entry.getKey());
            interfaceC2741k.h(10);
        }
        interfaceC2741k.flush();
        if (this.size > this.maxSize || journalRewriteRequired()) {
            launchCleanup();
        }
    }

    private final void delete() {
        close();
        FileSystems.deleteContents(this.fileSystem, this.directory);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean journalRewriteRequired() {
        return this.operationsSinceRewrite >= 2000;
    }

    private final void launchCleanup() {
        E.y(this.cleanupScope, null, null, new AnonymousClass1(null), 3);
    }

    private final InterfaceC2741k newJournalWriter() {
        return AbstractC2732b.b(new FaultHidingSink(this.fileSystem.appendingSink(this.journalFile), new androidx.compose.runtime.snapshots.e(this, 2)));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final C3137A newJournalWriter$lambda$3(DiskLruCache diskLruCache, IOException iOException) {
        diskLruCache.hasJournalErrors = true;
        return C3137A.f25453a;
    }

    private final void processJournal() {
        Iterator<Entry> it = this.lruEntries.values().iterator();
        long j4 = 0;
        while (it.hasNext()) {
            Entry next = it.next();
            int i6 = 0;
            if (next.getCurrentEditor() == null) {
                int i7 = this.valueCount;
                while (i6 < i7) {
                    j4 += next.getLengths()[i6];
                    i6++;
                }
            } else {
                next.setCurrentEditor(null);
                int i8 = this.valueCount;
                while (i6 < i8) {
                    this.fileSystem.delete(next.getCleanFiles().get(i6));
                    this.fileSystem.delete(next.getDirtyFiles().get(i6));
                    i6++;
                }
                it.remove();
            }
        }
        this.size = j4;
    }

    private final void readJournal() throws Throwable {
        J jC = AbstractC2732b.c(this.fileSystem.source(this.journalFile));
        try {
            String strZ = jC.z(Long.MAX_VALUE);
            String strZ2 = jC.z(Long.MAX_VALUE);
            String strZ3 = jC.z(Long.MAX_VALUE);
            String strZ4 = jC.z(Long.MAX_VALUE);
            String strZ5 = jC.z(Long.MAX_VALUE);
            if (!MAGIC.equals(strZ) || !"1".equals(strZ2) || !o.c(String.valueOf(this.appVersion), strZ3) || !o.c(String.valueOf(this.valueCount), strZ4) || strZ5.length() > 0) {
                throw new IOException("unexpected journal header: [" + strZ + ", " + strZ2 + ", " + strZ3 + ", " + strZ4 + ", " + strZ5 + ']');
            }
            int i6 = 0;
            while (true) {
                try {
                    readJournalLine(jC.z(Long.MAX_VALUE));
                    i6++;
                } catch (EOFException unused) {
                    this.operationsSinceRewrite = i6 - this.lruEntries.size();
                    if (jC.a()) {
                        this.journalWriter = newJournalWriter();
                    } else {
                        writeJournal();
                    }
                    try {
                        jC.close();
                        th = null;
                    } catch (Throwable th) {
                        th = th;
                    }
                }
            }
        } catch (Throwable th2) {
            th = th2;
            try {
                jC.close();
            } catch (Throwable th3) {
                AbstractC2613d.b(th, th3);
            }
        }
        if (th != null) {
            throw th;
        }
    }

    private final void readJournalLine(String str) throws IOException {
        String strSubstring;
        int iN0 = p.N0(str, ' ', 0, 6);
        if (iN0 == -1) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
        int i6 = iN0 + 1;
        int iN02 = p.N0(str, ' ', i6, 4);
        if (iN02 == -1) {
            strSubstring = str.substring(i6);
            o.g(strSubstring, "substring(...)");
            if (iN0 == 6 && w.A0(str, REMOVE, false)) {
                this.lruEntries.remove(strSubstring);
                return;
            }
        } else {
            strSubstring = str.substring(i6, iN02);
            o.g(strSubstring, "substring(...)");
        }
        LinkedHashMap<String, Entry> linkedHashMap = this.lruEntries;
        Entry entry = linkedHashMap.get(strSubstring);
        if (entry == null) {
            entry = new Entry(strSubstring);
            linkedHashMap.put(strSubstring, entry);
        }
        Entry entry2 = entry;
        if (iN02 != -1 && iN0 == 5 && w.A0(str, CLEAN, false)) {
            String strSubstring2 = str.substring(iN02 + 1);
            o.g(strSubstring2, "substring(...)");
            List<String> listC1 = p.c1(strSubstring2, new char[]{' '}, 6);
            entry2.setReadable(true);
            entry2.setCurrentEditor(null);
            entry2.setLengths(listC1);
            return;
        }
        if (iN02 == -1 && iN0 == 5 && w.A0(str, DIRTY, false)) {
            entry2.setCurrentEditor(new Editor(entry2));
        } else if (iN02 != -1 || iN0 != 4 || !w.A0(str, READ, false)) {
            throw new IOException("unexpected journal line: ".concat(str));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean removeEntry(Entry entry) {
        InterfaceC2741k interfaceC2741k;
        if (entry.getLockingSnapshotCount() > 0 && (interfaceC2741k = this.journalWriter) != null) {
            interfaceC2741k.n(DIRTY);
            interfaceC2741k.h(32);
            interfaceC2741k.n(entry.getKey());
            interfaceC2741k.h(10);
            interfaceC2741k.flush();
        }
        if (entry.getLockingSnapshotCount() > 0 || entry.getCurrentEditor() != null) {
            entry.setZombie(true);
            return true;
        }
        int i6 = this.valueCount;
        for (int i7 = 0; i7 < i6; i7++) {
            this.fileSystem.delete(entry.getCleanFiles().get(i7));
            this.size -= entry.getLengths()[i7];
            entry.getLengths()[i7] = 0;
        }
        this.operationsSinceRewrite++;
        InterfaceC2741k interfaceC2741k2 = this.journalWriter;
        if (interfaceC2741k2 != null) {
            interfaceC2741k2.n(REMOVE);
            interfaceC2741k2.h(32);
            interfaceC2741k2.n(entry.getKey());
            interfaceC2741k2.h(10);
        }
        this.lruEntries.remove(entry.getKey());
        if (journalRewriteRequired()) {
            launchCleanup();
        }
        return true;
    }

    private final boolean removeOldestEntry() {
        for (Entry entry : this.lruEntries.values()) {
            if (!entry.getZombie()) {
                removeEntry(entry);
                return true;
            }
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void trimToSize() {
        while (this.size > this.maxSize) {
            if (!removeOldestEntry()) {
                return;
            }
        }
        this.mostRecentTrimFailed = false;
    }

    private final void validateKey(String str) {
        if (!LEGAL_KEY_PATTERN.d(str)) {
            throw new IllegalArgumentException(A0.a.k('\"', "keys must match regex [a-z0-9_-]{1,120}: \"", str).toString());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final synchronized void writeJournal() {
        Throwable th;
        try {
            InterfaceC2741k interfaceC2741k = this.journalWriter;
            if (interfaceC2741k != null) {
                interfaceC2741k.close();
            }
            I iB = AbstractC2732b.b(this.fileSystem.sink(this.journalFileTmp, false));
            try {
                iB.n(MAGIC);
                iB.h(10);
                iB.n("1");
                iB.h(10);
                iB.w(this.appVersion);
                iB.h(10);
                iB.w(this.valueCount);
                iB.h(10);
                iB.h(10);
                for (Entry entry : this.lruEntries.values()) {
                    if (entry.getCurrentEditor() != null) {
                        iB.n(DIRTY);
                        iB.h(32);
                        iB.n(entry.getKey());
                        iB.h(10);
                    } else {
                        iB.n(CLEAN);
                        iB.h(32);
                        iB.n(entry.getKey());
                        entry.writeLengths(iB);
                        iB.h(10);
                    }
                }
                try {
                    iB.close();
                    th = null;
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Throwable th3) {
                try {
                    iB.close();
                } catch (Throwable th4) {
                    AbstractC2613d.b(th3, th4);
                }
                th = th3;
            }
            if (th != null) {
                throw th;
            }
            if (this.fileSystem.exists(this.journalFile)) {
                this.fileSystem.atomicMove(this.journalFile, this.journalFileBackup);
                this.fileSystem.atomicMove(this.journalFileTmp, this.journalFile);
                this.fileSystem.delete(this.journalFileBackup);
            } else {
                this.fileSystem.atomicMove(this.journalFileTmp, this.journalFile);
            }
            this.journalWriter = newJournalWriter();
            this.operationsSinceRewrite = 0;
            this.hasJournalErrors = false;
            this.mostRecentRebuildFailed = false;
        } catch (Throwable th5) {
            throw th5;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() {
        try {
            if (this.initialized && !this.closed) {
                for (Entry entry : (Entry[]) this.lruEntries.values().toArray(new Entry[0])) {
                    Editor currentEditor = entry.getCurrentEditor();
                    if (currentEditor != null) {
                        currentEditor.detach();
                    }
                }
                trimToSize();
                E.i(this.cleanupScope, null);
                InterfaceC2741k interfaceC2741k = this.journalWriter;
                o.e(interfaceC2741k);
                interfaceC2741k.close();
                this.journalWriter = null;
                this.closed = true;
                return;
            }
            this.closed = true;
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized Editor edit(String str) {
        checkNotClosed();
        validateKey(str);
        initialize();
        Entry entry = this.lruEntries.get(str);
        if ((entry != null ? entry.getCurrentEditor() : null) != null) {
            return null;
        }
        if (entry != null && entry.getLockingSnapshotCount() != 0) {
            return null;
        }
        if (!this.mostRecentTrimFailed && !this.mostRecentRebuildFailed) {
            InterfaceC2741k interfaceC2741k = this.journalWriter;
            o.e(interfaceC2741k);
            interfaceC2741k.n(DIRTY);
            interfaceC2741k.h(32);
            interfaceC2741k.n(str);
            interfaceC2741k.h(10);
            interfaceC2741k.flush();
            if (this.hasJournalErrors) {
                return null;
            }
            if (entry == null) {
                entry = new Entry(str);
                this.lruEntries.put(str, entry);
            }
            Editor editor = new Editor(entry);
            entry.setCurrentEditor(editor);
            return editor;
        }
        launchCleanup();
        return null;
    }

    public final synchronized void evictAll() {
        try {
            initialize();
            for (Entry entry : (Entry[]) this.lruEntries.values().toArray(new Entry[0])) {
                removeEntry(entry);
            }
            this.mostRecentTrimFailed = false;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.Flushable
    public synchronized void flush() {
        if (this.initialized) {
            checkNotClosed();
            trimToSize();
            InterfaceC2741k interfaceC2741k = this.journalWriter;
            o.e(interfaceC2741k);
            interfaceC2741k.flush();
        }
    }

    public final synchronized Snapshot get(String str) {
        Snapshot snapshot;
        checkNotClosed();
        validateKey(str);
        initialize();
        Entry entry = this.lruEntries.get(str);
        if (entry != null && (snapshot = entry.snapshot()) != null) {
            this.operationsSinceRewrite++;
            InterfaceC2741k interfaceC2741k = this.journalWriter;
            o.e(interfaceC2741k);
            interfaceC2741k.n(READ);
            interfaceC2741k.h(32);
            interfaceC2741k.n(str);
            interfaceC2741k.h(10);
            if (journalRewriteRequired()) {
                launchCleanup();
            }
            return snapshot;
        }
        return null;
    }

    public final synchronized void initialize() {
        try {
            if (this.initialized) {
                return;
            }
            this.fileSystem.delete(this.journalFileTmp);
            if (this.fileSystem.exists(this.journalFileBackup)) {
                if (this.fileSystem.exists(this.journalFile)) {
                    this.fileSystem.delete(this.journalFileBackup);
                } else {
                    this.fileSystem.atomicMove(this.journalFileBackup, this.journalFile);
                }
            }
            if (this.fileSystem.exists(this.journalFile)) {
                try {
                    readJournal();
                    processJournal();
                    this.initialized = true;
                    return;
                } catch (IOException unused) {
                    try {
                        delete();
                        this.closed = false;
                        writeJournal();
                        this.initialized = true;
                    } catch (Throwable th) {
                        this.closed = false;
                        throw th;
                    }
                }
            }
            writeJournal();
            this.initialized = true;
        } catch (Throwable th2) {
            throw th2;
        }
    }

    public final synchronized boolean remove(String str) {
        checkNotClosed();
        validateKey(str);
        initialize();
        Entry entry = this.lruEntries.get(str);
        if (entry == null) {
            return false;
        }
        boolean zRemoveEntry = removeEntry(entry);
        if (zRemoveEntry && this.size <= this.maxSize) {
            this.mostRecentTrimFailed = false;
        }
        return zRemoveEntry;
    }

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