From 787cfb94c52e62bd2e4c8826dec1bcdd0d2141ec Mon Sep 17 00:00:00 2001 From: bachinger Date: Wed, 22 Jul 2020 16:04:50 +0100 Subject: [PATCH] Enable nullness checking for CacheDataSink PiperOrigin-RevId: 322575337 --- .../upstream/cache/CacheDataSink.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSink.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSink.java index 454674f665..abdb840812 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSink.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSink.java @@ -15,6 +15,9 @@ */ 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 com.google.android.exoplayer2.C; import com.google.android.exoplayer2.upstream.DataSink; @@ -99,7 +102,7 @@ public final class CacheDataSink implements DataSink { @Override 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 { + MIN_RECOMMENDED_FRAGMENT_SIZE + ". 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.bufferSize = bufferSize; } @Override public void open(DataSpec dataSpec) throws CacheDataSinkException { + checkNotNull(dataSpec.key); if (dataSpec.length == C.LENGTH_UNSET && dataSpec.isFlagSet(DataSpec.FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN)) { this.dataSpec = null; @@ -183,7 +187,7 @@ public final class CacheDataSink implements DataSink { dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION) ? fragmentSize : Long.MAX_VALUE; dataSpecBytesWritten = 0; try { - openNextOutputStream(); + openNextOutputStream(dataSpec); } catch (IOException e) { throw new CacheDataSinkException(e); } @@ -191,6 +195,7 @@ public final class CacheDataSink implements DataSink { @Override public void write(byte[] buffer, int offset, int length) throws CacheDataSinkException { + @Nullable DataSpec dataSpec = this.dataSpec; if (dataSpec == null) { return; } @@ -199,11 +204,11 @@ public final class CacheDataSink implements DataSink { while (bytesWritten < length) { if (outputStreamBytesWritten == dataSpecFragmentSize) { closeCurrentOutputStream(); - openNextOutputStream(); + openNextOutputStream(dataSpec); } int bytesToWrite = (int) Math.min(length - bytesWritten, dataSpecFragmentSize - outputStreamBytesWritten); - outputStream.write(buffer, offset + bytesWritten, bytesToWrite); + castNonNull(outputStream).write(buffer, offset + bytesWritten, bytesToWrite); bytesWritten += bytesToWrite; outputStreamBytesWritten += bytesToWrite; dataSpecBytesWritten += bytesToWrite; @@ -225,12 +230,14 @@ public final class CacheDataSink implements DataSink { } } - private void openNextOutputStream() throws IOException { + private void openNextOutputStream(DataSpec dataSpec) throws IOException { long length = dataSpec.length == C.LENGTH_UNSET ? C.LENGTH_UNSET : 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); if (bufferSize > 0) { if (bufferedOutputStream == null) { @@ -258,7 +265,7 @@ public final class CacheDataSink implements DataSink { } finally { Util.closeQuietly(outputStream); outputStream = null; - File fileToCommit = file; + File fileToCommit = castNonNull(file); file = null; if (success) { cache.commitFile(fileToCommit, outputStreamBytesWritten);