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); }
throw new IllegalStateException("Newly created entry didn't create value for index " + i); if (!fileSystem.exists(entry.dirtyFiles[i])) { editor.abort(); return; File dirty = entry.dirtyFiles[i]; if (success) { if (fileSystem.exists(dirty)) { File clean = entry.cleanFiles[i]; fileSystem.rename(dirty, clean); long oldLength = entry.lengths[i]; long newLength = fileSystem.size(clean); entry.lengths[i] = newLength; size = size - oldLength + newLength; fileSystem.delete(dirty);
/** * 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; } } }
/** * 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; } }
Sink sink; try { sink = fileSystem.sink(dirtyFile); } catch (FileNotFoundException e) { return Okio.blackhole();
throw new IllegalStateException("Newly created entry didn't create value for index " + i); if (!fileSystem.exists(entry.dirtyFiles[i])) { editor.abort(); return; File dirty = entry.dirtyFiles[i]; if (success) { if (fileSystem.exists(dirty)) { File clean = entry.cleanFiles[i]; fileSystem.rename(dirty, clean); long oldLength = entry.lengths[i]; long newLength = fileSystem.size(clean); entry.lengths[i] = newLength; size = size - oldLength + newLength; fileSystem.delete(dirty);
/** * 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; } } }
/** * 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(); } } }
Sink sink; try { sink = fileSystem.sink(dirtyFile); } catch (FileNotFoundException e) { return Okio.blackhole();
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);
throw new IllegalStateException("Newly created entry didn't create value for index " + i); if (!fileSystem.exists(entry.dirtyFiles[i])) { editor.abort(); return; File dirty = entry.dirtyFiles[i]; if (success) { if (fileSystem.exists(dirty)) { File clean = entry.cleanFiles[i]; fileSystem.rename(dirty, clean); long oldLength = entry.lengths[i]; long newLength = fileSystem.size(clean); entry.lengths[i] = newLength; size = size - oldLength + newLength; fileSystem.delete(dirty);
try { for (int i = 0; i < valueCount; i++) { sources[i] = fileSystem.source(cleanFiles[i]);
/** * 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; } }
/** * 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); }
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); }