From e84fa5835d0e6595646ded0857d59ba2dab4029c Mon Sep 17 00:00:00 2001 From: eguven Date: Tue, 8 Nov 2016 03:27:52 +0000 Subject: [PATCH] Use ReusableBufferedOutputStream for cache index file write operation ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=139889957 --- .../upstream/cache/CachedContentIndex.java | 14 ++++++++++---- .../util/ReusableBufferedOutputStream.java | 10 ++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java index 64863ac42b..d8224665b9 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java @@ -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()); diff --git a/library/src/main/java/com/google/android/exoplayer2/util/ReusableBufferedOutputStream.java b/library/src/main/java/com/google/android/exoplayer2/util/ReusableBufferedOutputStream.java index 1ae947b610..a3d1d4d02e 100644 --- a/library/src/main/java/com/google/android/exoplayer2/util/ReusableBufferedOutputStream.java +++ b/library/src/main/java/com/google/android/exoplayer2/util/ReusableBufferedOutputStream.java @@ -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; } }