Commit e84fa583 by eguven Committed by Oliver Woodman

Use ReusableBufferedOutputStream for cache index file write operation

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139889957
parent 42fadfe0
......@@ -20,9 +20,9 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.AtomicFile;
import com.google.android.exoplayer2.util.ReusableBufferedOutputStream;
import com.google.android.exoplayer2.util.Util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
......@@ -61,6 +61,7 @@ import javax.crypto.spec.SecretKeySpec;
private final Cipher cipher;
private final SecretKeySpec secretKeySpec;
private boolean changed;
private ReusableBufferedOutputStream bufferedOutputStream;
/** Creates a CachedContentIndex which works on the index file in the given cacheDir. */
public CachedContentIndex(File cacheDir) {
......@@ -256,8 +257,13 @@ import javax.crypto.spec.SecretKeySpec;
private void writeFile() throws CacheException {
DataOutputStream output = null;
try {
OutputStream outputStream = new BufferedOutputStream(atomicFile.startWrite());
output = new DataOutputStream(outputStream);
OutputStream outputStream = atomicFile.startWrite();
if (bufferedOutputStream == null) {
bufferedOutputStream = new ReusableBufferedOutputStream(outputStream);
} else {
bufferedOutputStream.reset(outputStream);
}
output = new DataOutputStream(bufferedOutputStream);
output.writeInt(VERSION);
int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
......@@ -274,7 +280,7 @@ import javax.crypto.spec.SecretKeySpec;
throw new IllegalStateException(e); // Should never happen.
}
output.flush();
output = new DataOutputStream(new CipherOutputStream(outputStream, cipher));
output = new DataOutputStream(new CipherOutputStream(bufferedOutputStream, cipher));
}
output.writeInt(keyToContent.size());
......
......@@ -25,6 +25,8 @@ import java.io.OutputStream;
*/
public final class ReusableBufferedOutputStream extends BufferedOutputStream {
private boolean closed;
public ReusableBufferedOutputStream(OutputStream out) {
super(out);
}
......@@ -35,13 +37,14 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
@Override
public void close() throws IOException {
closed = true;
Throwable thrown = null;
try {
flush();
} catch (Throwable e) {
thrown = e;
}
try {
out.close();
} catch (Throwable e) {
......@@ -49,8 +52,6 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
thrown = e;
}
}
out = null;
if (thrown != null) {
Util.sneakyThrow(thrown);
}
......@@ -64,7 +65,8 @@ public final class ReusableBufferedOutputStream extends BufferedOutputStream {
* @throws IllegalStateException If the stream isn't closed.
*/
public void reset(OutputStream out) {
Assertions.checkState(this.out == null);
Assertions.checkState(closed);
this.out = out;
closed = false;
}
}
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