Commit 787cfb94 by bachinger Committed by Oliver Woodman

Enable nullness checking for CacheDataSink

PiperOrigin-RevId: 322575337
parent d33ba74a
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
*/ */
package com.google.android.exoplayer2.upstream.cache; package com.google.android.exoplayer2.upstream.cache;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Util.castNonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSink; import com.google.android.exoplayer2.upstream.DataSink;
...@@ -99,7 +102,7 @@ public final class CacheDataSink implements DataSink { ...@@ -99,7 +102,7 @@ public final class CacheDataSink implements DataSink {
@Override @Override
public DataSink createDataSink() { public DataSink createDataSink() {
return new CacheDataSink(Assertions.checkNotNull(cache), fragmentSize, bufferSize); return new CacheDataSink(checkNotNull(cache), fragmentSize, bufferSize);
} }
} }
...@@ -166,13 +169,14 @@ public final class CacheDataSink implements DataSink { ...@@ -166,13 +169,14 @@ public final class CacheDataSink implements DataSink {
+ MIN_RECOMMENDED_FRAGMENT_SIZE + MIN_RECOMMENDED_FRAGMENT_SIZE
+ ". This may cause poor cache performance."); + ". This may cause poor cache performance.");
} }
this.cache = Assertions.checkNotNull(cache); this.cache = checkNotNull(cache);
this.fragmentSize = fragmentSize == C.LENGTH_UNSET ? Long.MAX_VALUE : fragmentSize; this.fragmentSize = fragmentSize == C.LENGTH_UNSET ? Long.MAX_VALUE : fragmentSize;
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
} }
@Override @Override
public void open(DataSpec dataSpec) throws CacheDataSinkException { public void open(DataSpec dataSpec) throws CacheDataSinkException {
checkNotNull(dataSpec.key);
if (dataSpec.length == C.LENGTH_UNSET if (dataSpec.length == C.LENGTH_UNSET
&& dataSpec.isFlagSet(DataSpec.FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN)) { && dataSpec.isFlagSet(DataSpec.FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN)) {
this.dataSpec = null; this.dataSpec = null;
...@@ -183,7 +187,7 @@ public final class CacheDataSink implements DataSink { ...@@ -183,7 +187,7 @@ public final class CacheDataSink implements DataSink {
dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION) ? fragmentSize : Long.MAX_VALUE; dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION) ? fragmentSize : Long.MAX_VALUE;
dataSpecBytesWritten = 0; dataSpecBytesWritten = 0;
try { try {
openNextOutputStream(); openNextOutputStream(dataSpec);
} catch (IOException e) { } catch (IOException e) {
throw new CacheDataSinkException(e); throw new CacheDataSinkException(e);
} }
...@@ -191,6 +195,7 @@ public final class CacheDataSink implements DataSink { ...@@ -191,6 +195,7 @@ public final class CacheDataSink implements DataSink {
@Override @Override
public void write(byte[] buffer, int offset, int length) throws CacheDataSinkException { public void write(byte[] buffer, int offset, int length) throws CacheDataSinkException {
@Nullable DataSpec dataSpec = this.dataSpec;
if (dataSpec == null) { if (dataSpec == null) {
return; return;
} }
...@@ -199,11 +204,11 @@ public final class CacheDataSink implements DataSink { ...@@ -199,11 +204,11 @@ public final class CacheDataSink implements DataSink {
while (bytesWritten < length) { while (bytesWritten < length) {
if (outputStreamBytesWritten == dataSpecFragmentSize) { if (outputStreamBytesWritten == dataSpecFragmentSize) {
closeCurrentOutputStream(); closeCurrentOutputStream();
openNextOutputStream(); openNextOutputStream(dataSpec);
} }
int bytesToWrite = int bytesToWrite =
(int) Math.min(length - bytesWritten, dataSpecFragmentSize - outputStreamBytesWritten); (int) Math.min(length - bytesWritten, dataSpecFragmentSize - outputStreamBytesWritten);
outputStream.write(buffer, offset + bytesWritten, bytesToWrite); castNonNull(outputStream).write(buffer, offset + bytesWritten, bytesToWrite);
bytesWritten += bytesToWrite; bytesWritten += bytesToWrite;
outputStreamBytesWritten += bytesToWrite; outputStreamBytesWritten += bytesToWrite;
dataSpecBytesWritten += bytesToWrite; dataSpecBytesWritten += bytesToWrite;
...@@ -225,12 +230,14 @@ public final class CacheDataSink implements DataSink { ...@@ -225,12 +230,14 @@ public final class CacheDataSink implements DataSink {
} }
} }
private void openNextOutputStream() throws IOException { private void openNextOutputStream(DataSpec dataSpec) throws IOException {
long length = long length =
dataSpec.length == C.LENGTH_UNSET dataSpec.length == C.LENGTH_UNSET
? C.LENGTH_UNSET ? C.LENGTH_UNSET
: Math.min(dataSpec.length - dataSpecBytesWritten, dataSpecFragmentSize); : Math.min(dataSpec.length - dataSpecBytesWritten, dataSpecFragmentSize);
file = cache.startFile(dataSpec.key, dataSpec.position + dataSpecBytesWritten, length); file =
cache.startFile(
castNonNull(dataSpec.key), dataSpec.position + dataSpecBytesWritten, length);
FileOutputStream underlyingFileOutputStream = new FileOutputStream(file); FileOutputStream underlyingFileOutputStream = new FileOutputStream(file);
if (bufferSize > 0) { if (bufferSize > 0) {
if (bufferedOutputStream == null) { if (bufferedOutputStream == null) {
...@@ -258,7 +265,7 @@ public final class CacheDataSink implements DataSink { ...@@ -258,7 +265,7 @@ public final class CacheDataSink implements DataSink {
} finally { } finally {
Util.closeQuietly(outputStream); Util.closeQuietly(outputStream);
outputStream = null; outputStream = null;
File fileToCommit = file; File fileToCommit = castNonNull(file);
file = null; file = null;
if (success) { if (success) {
cache.commitFile(fileToCommit, outputStreamBytesWritten); cache.commitFile(fileToCommit, outputStreamBytesWritten);
......
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