Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge: Tolerate cache files being removed from underneath DiskLruCache #23

Merged
merged 1 commit into from
Sep 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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