Commit 2add12d5 by yutingtseng Committed by Oliver Woodman

Update CacheDataSink to optionally use a BufferedOutputStream for writing

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139381958
parent 92d34cd8
...@@ -21,9 +21,11 @@ import com.google.android.exoplayer2.upstream.DataSpec; ...@@ -21,9 +21,11 @@ import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.cache.Cache.CacheException; import com.google.android.exoplayer2.upstream.cache.Cache.CacheException;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
/** /**
* Writes data into a cache. * Writes data into a cache.
...@@ -32,10 +34,12 @@ public final class CacheDataSink implements DataSink { ...@@ -32,10 +34,12 @@ public final class CacheDataSink implements DataSink {
private final Cache cache; private final Cache cache;
private final long maxCacheFileSize; private final long maxCacheFileSize;
private final int bufferSize;
private DataSpec dataSpec; private DataSpec dataSpec;
private File file; private File file;
private FileOutputStream outputStream; private OutputStream outputStream;
private FileOutputStream underlyingFileOutputStream;
private long outputStreamBytesWritten; private long outputStreamBytesWritten;
private long dataSpecBytesWritten; private long dataSpecBytesWritten;
...@@ -57,8 +61,21 @@ public final class CacheDataSink implements DataSink { ...@@ -57,8 +61,21 @@ public final class CacheDataSink implements DataSink {
* multiple cache files. * multiple cache files.
*/ */
public CacheDataSink(Cache cache, long maxCacheFileSize) { public CacheDataSink(Cache cache, long maxCacheFileSize) {
this(cache, maxCacheFileSize, 0);
}
/**
* @param cache The cache into which data should be written.
* @param maxCacheFileSize The maximum size of a cache file, in bytes. If the sink is opened for
* a {@link DataSpec} whose size exceeds this value, then the data will be fragmented into
* multiple cache files.
* @param bufferSize The buffer size in bytes for writing to a cache file. A zero or negative
* value disables buffering.
*/
public CacheDataSink(Cache cache, long maxCacheFileSize, int bufferSize) {
this.cache = Assertions.checkNotNull(cache); this.cache = Assertions.checkNotNull(cache);
this.maxCacheFileSize = maxCacheFileSize; this.maxCacheFileSize = maxCacheFileSize;
this.bufferSize = bufferSize;
} }
@Override @Override
...@@ -114,7 +131,10 @@ public final class CacheDataSink implements DataSink { ...@@ -114,7 +131,10 @@ public final class CacheDataSink implements DataSink {
private void openNextOutputStream() throws IOException { private void openNextOutputStream() throws IOException {
file = cache.startFile(dataSpec.key, dataSpec.absoluteStreamPosition + dataSpecBytesWritten, file = cache.startFile(dataSpec.key, dataSpec.absoluteStreamPosition + dataSpecBytesWritten,
Math.min(dataSpec.length - dataSpecBytesWritten, maxCacheFileSize)); Math.min(dataSpec.length - dataSpecBytesWritten, maxCacheFileSize));
outputStream = new FileOutputStream(file); underlyingFileOutputStream = new FileOutputStream(file);
outputStream = bufferSize > 0
? new BufferedOutputStream(underlyingFileOutputStream, bufferSize)
: underlyingFileOutputStream;
outputStreamBytesWritten = 0; outputStreamBytesWritten = 0;
} }
...@@ -126,7 +146,7 @@ public final class CacheDataSink implements DataSink { ...@@ -126,7 +146,7 @@ public final class CacheDataSink implements DataSink {
boolean success = false; boolean success = false;
try { try {
outputStream.flush(); outputStream.flush();
outputStream.getFD().sync(); underlyingFileOutputStream.getFD().sync();
success = true; success = true;
} finally { } finally {
Util.closeQuietly(outputStream); Util.closeQuietly(outputStream);
......
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