@Override public Sink sink(File file) throws FileNotFoundException { return sink(file, false); }
/** * Prevents this editor from completing normally. This is necessary either when the edit causes * an I/O error, or if the target entry is evicted while this editor is active. In either case * we delete the editor's created files and prevent new files from being created. Note that once * an editor has been detached it is possible for another editor to edit the entry. */ void detach() { if (entry.currentEditor == this) { for (int i = 0; i < valueCount; i++) { try { fileSystem.delete(entry.dirtyFiles[i]); } catch (IOException e) { // This file is potentially leaked. Not much we can do about that. } } entry.currentEditor = null; } }
private BufferedSink newJournalWriter() throws FileNotFoundException { Sink fileSink = fileSystem.appendingSink(journalFile); Sink faultHidingSink = new FaultHidingSink(fileSink) { @Override protected void onException(IOException e) { assert (Thread.holdsLock(DiskLruCache.this)); hasJournalErrors = true; } }; return Okio.buffer(faultHidingSink); }
try (BufferedSink writer = Okio.buffer(fileSystem.sink(journalFileTmp))) { writer.writeUtf8(MAGIC).writeByte('\n'); writer.writeUtf8(VERSION_1).writeByte('\n'); if (fileSystem.exists(journalFile)) { fileSystem.rename(journalFile, journalFileBackup); fileSystem.rename(journalFileTmp, journalFile); fileSystem.delete(journalFileBackup);
/** * Closes the cache and deletes all of its stored values. This will delete all files in the cache * directory including files that weren't created by the cache. */ public void delete() throws IOException { close(); fileSystem.deleteContents(directory); }
@Override public void rename(File from, File to) throws IOException { delete(to); if (!from.renameTo(to)) { throw new IOException("failed to rename " + from + " to " + to); } }
/** * Returns an unbuffered input stream to read the last committed value, or null if no value has * been committed. */ public Source newSource(int index) { synchronized (DiskLruCache.this) { if (done) { throw new IllegalStateException(); } if (!entry.readable || entry.currentEditor != this) { return null; } try { return fileSystem.source(entry.cleanFiles[index]); } catch (FileNotFoundException e) { return null; } } }
@Override public void deleteContents(File directory) throws IOException { File[] files = directory.listFiles(); if (files == null) { throw new IOException("not a readable directory: " + directory); } for (File file : files) { if (file.isDirectory()) { deleteContents(file); } if (!file.delete()) { throw new IOException("failed to delete " + file); } } } };
Sink sink; try { sink = fileSystem.sink(dirtyFile); } catch (FileNotFoundException e) { return Okio.blackhole();
@Override public void evaluate() throws Throwable { base.evaluate(); ensureResourcesClosed(); } };
/** * Prevents this editor from completing normally. This is necessary either when the edit causes * an I/O error, or if the target entry is evicted while this editor is active. In either case * we delete the editor's created files and prevent new files from being created. Note that once * an editor has been detached it is possible for another editor to edit the entry. */ void detach() { if (entry.currentEditor == this) { for (int i = 0; i < valueCount; i++) { try { fileSystem.delete(entry.dirtyFiles[i]); } catch (IOException e) { // This file is potentially leaked. Not much we can do about that. } } entry.currentEditor = null; } }
private BufferedSink newJournalWriter() throws FileNotFoundException { Sink fileSink = fileSystem.appendingSink(journalFile); Sink faultHidingSink = new FaultHidingSink(fileSink) { @Override protected void onException(IOException e) { assert (Thread.holdsLock(DiskLruCache.this)); hasJournalErrors = true; } }; return Okio.buffer(faultHidingSink); }
/** * Closes the cache and deletes all of its stored values. This will delete all files in the cache * directory including files that weren't created by the cache. */ public void delete() throws IOException { close(); fileSystem.deleteContents(directory); }
@Override public void rename(File from, File to) throws IOException { delete(to); if (!from.renameTo(to)) { throw new IOException("failed to rename " + from + " to " + to); } }
/** * Returns an unbuffered input stream to read the last committed value, or null if no value has * been committed. */ public Source newSource(int index) { synchronized (DiskLruCache.this) { if (done) { throw new IllegalStateException(); } if (!entry.readable || entry.currentEditor != this) { return null; } try { return fileSystem.source(entry.cleanFiles[index]); } catch (FileNotFoundException e) { return null; } } }
@Override public void deleteContents(File directory) throws IOException { File[] files = directory.listFiles(); if (files == null) { throw new IOException("not a readable directory: " + directory); } for (File file : files) { if (file.isDirectory()) { deleteContents(file); } if (!file.delete()) { throw new IOException("failed to delete " + file); } } } };
@Override public Sink appendingSink(File file) throws FileNotFoundException { return sink(file, true); }
/** * Computes the initial size and collects garbage as a part of opening the cache. Dirty entries * are assumed to be inconsistent and will be deleted. */ private void processJournal() throws IOException { fileSystem.delete(journalFileTmp); for (Iterator<Entry> i = lruEntries.values().iterator(); i.hasNext(); ) { Entry entry = i.next(); if (entry.currentEditor == null) { for (int t = 0; t < valueCount; t++) { size += entry.lengths[t]; } } else { entry.currentEditor = null; for (int t = 0; t < valueCount; t++) { fileSystem.delete(entry.cleanFiles[t]); fileSystem.delete(entry.dirtyFiles[t]); } i.remove(); } } }
boolean removeEntry(Entry entry) throws IOException { if (entry.currentEditor != null) { entry.currentEditor.detach(); // Prevent the edit from completing normally. } for (int i = 0; i < valueCount; i++) { fileSystem.delete(entry.cleanFiles[i]); size -= entry.lengths[i]; entry.lengths[i] = 0; } redundantOpCount++; journalWriter.writeUtf8(REMOVE).writeByte(' ').writeUtf8(entry.key).writeByte('\n'); lruEntries.remove(entry.key); if (journalRebuildRequired()) { executor.execute(cleanupRunnable); } return true; }