Skip to content

Commit

Permalink
Make CacheDataSink use ReusableBufferedOutputStream
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140010664
  • Loading branch information
erdemguven authored and ojw28 committed Nov 25, 2016
1 parent dbd568d commit 11f2050
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.google.android.exoplayer.upstream.DataSpec;
import com.google.android.exoplayer.upstream.cache.Cache.CacheException;
import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.ReusableBufferedOutputStream;
import com.google.android.exoplayer.util.Util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -42,6 +42,7 @@ public final class CacheDataSink implements DataSink {
private FileOutputStream underlyingFileOutputStream;
private long outputStreamBytesWritten;
private long dataSpecBytesWritten;
private ReusableBufferedOutputStream bufferedOutputStream;

/**
* Thrown when IOException is encountered when writing data into sink.
Expand Down Expand Up @@ -127,9 +128,17 @@ private void openNextOutputStream() throws IOException {
file = cache.startFile(dataSpec.key, dataSpec.absoluteStreamPosition + dataSpecBytesWritten,
Math.min(dataSpec.length - dataSpecBytesWritten, maxCacheFileSize));
underlyingFileOutputStream = new FileOutputStream(file);
outputStream = bufferSize > 0
? new BufferedOutputStream(underlyingFileOutputStream, bufferSize)
: underlyingFileOutputStream;
if (bufferSize > 0) {
if (bufferedOutputStream == null) {
bufferedOutputStream = new ReusableBufferedOutputStream(underlyingFileOutputStream,
bufferSize);
} else {
bufferedOutputStream.reset(underlyingFileOutputStream);
}
outputStream = bufferedOutputStream;
} else {
outputStream = underlyingFileOutputStream;
}
outputStreamBytesWritten = 0;
}

Expand Down

0 comments on commit 11f2050

Please sign in to comment.