Commit ed658b8e by Oliver Woodman

Attempt to guard against ExoCache corruption.

parent cfcbca6c
...@@ -61,8 +61,11 @@ public final class TeeDataSource implements DataSource { ...@@ -61,8 +61,11 @@ public final class TeeDataSource implements DataSource {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
upstream.close(); try {
dataSink.close(); upstream.close();
} finally {
dataSink.close();
}
} }
} }
...@@ -19,6 +19,7 @@ import com.google.android.exoplayer.C; ...@@ -19,6 +19,7 @@ import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.DataSink; import com.google.android.exoplayer.upstream.DataSink;
import com.google.android.exoplayer.upstream.DataSpec; import com.google.android.exoplayer.upstream.DataSpec;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.Util;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -115,11 +116,23 @@ public class CacheDataSink implements DataSink { ...@@ -115,11 +116,23 @@ public class CacheDataSink implements DataSink {
} }
private void closeCurrentOutputStream() throws IOException { private void closeCurrentOutputStream() throws IOException {
if (outputStream != null) { if (outputStream == null) {
return;
}
boolean success = false;
try {
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.getFD().sync();
success = true;
} finally {
Util.closeQuietly(outputStream);
if (success) {
cache.commitFile(file);
} else {
file.delete();
}
outputStream = null; outputStream = null;
cache.commitFile(file);
file = null; file = null;
} }
} }
......
...@@ -22,6 +22,7 @@ import android.text.TextUtils; ...@@ -22,6 +22,7 @@ import android.text.TextUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
...@@ -130,6 +131,19 @@ public final class Util { ...@@ -130,6 +131,19 @@ public final class Util {
} }
/** /**
* Closes an {@link OutputStream}, suppressing any {@link IOException} that may occur.
*
* @param outputStream The {@link OutputStream} to close.
*/
public static void closeQuietly(OutputStream outputStream) {
try {
outputStream.close();
} catch (IOException e) {
// Ignore.
}
}
/**
* Converts text to lower case using {@link Locale#US}. * Converts text to lower case using {@link Locale#US}.
* *
* @param text The text to convert. * @param text The text to convert.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment