Skip to content

Commit

Permalink
Merge pull request #23 from square/jwilson/deleted
Browse files Browse the repository at this point in the history
Merge: Tolerate cache files being removed from underneath DiskLruCache
  • Loading branch information
JakeWharton committed Sep 19, 2012
2 parents 1d57bed + 1bc9e2e commit 19d8cf1
Show file tree
Hide file tree
Showing 3 changed files with 842 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/main/java/libcore/io/DiskLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,16 @@ private synchronized void completeEdit(Editor editor, boolean success) throws IO
// if this edit is creating the entry for the first time, every index must have a value
if (success && !entry.readable) {
for (int i = 0; i < 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();
throw new IllegalStateException("edit didn't create file " + i);
Libcore.logW(
"DiskLruCache: Newly created entry doesn't have file for index " + i);
return;
}
}
}
Expand Down Expand Up @@ -654,10 +661,12 @@ public String getString(int index) throws IOException {
*/
public final class Editor {
private final Entry entry;
private final boolean[] written;
private boolean hasErrors;

private Editor(Entry entry) {
this.entry = entry;
this.written = (entry.readable) ? null : new boolean[valueCount];
}

/**
Expand Down Expand Up @@ -697,6 +706,9 @@ public OutputStream newOutputStream(int index) throws IOException {
if (entry.currentEditor != this) {
throw new IllegalStateException();
}
if (!entry.readable) {
written[index] = true;
}
return new FaultHidingOutputStream(new FileOutputStream(entry.getDirtyFile(index)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/libcore/net/http/HttpResponseCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public Entry(URI uri, RawHeaders varyHeaders, OkHttpConnection httpConnection) {
}

public void writeTo(DiskLruCache.Editor editor) throws IOException {
OutputStream out = editor.newOutputStream(0);
OutputStream out = editor.newOutputStream(ENTRY_METADATA);
Writer writer = new BufferedWriter(new OutputStreamWriter(out, Charsets.UTF_8));

writer.write(uri + '\n');
Expand Down
Loading

0 comments on commit 19d8cf1

Please sign in to comment.