Skip to content

Commit

Permalink
Enable nullness checking for CacheDataSource
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 322763032
  • Loading branch information
marcbaechinger authored and ojw28 committed Jul 24, 2020
1 parent ab73c04 commit d251788
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 android.net.Uri;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -275,7 +278,7 @@ public CacheDataSource createDataSourceForRemovingDownload() {

private CacheDataSource createDataSourceInternal(
@Nullable DataSource upstreamDataSource, @Flags int flags, int upstreamPriority) {
Cache cache = Assertions.checkNotNull(this.cache);
Cache cache = checkNotNull(this.cache);
@Nullable DataSink cacheWriteDataSink;
if (cacheIsReadOnly || upstreamDataSource == null) {
cacheWriteDataSink = null;
Expand Down Expand Up @@ -541,7 +544,7 @@ public CacheKeyFactory getCacheKeyFactory() {

@Override
public void addTransferListener(TransferListener transferListener) {
Assertions.checkNotNull(transferListener);
checkNotNull(transferListener);
cacheReadDataSource.addTransferListener(transferListener);
upstreamDataSource.addTransferListener(transferListener);
}
Expand All @@ -550,7 +553,8 @@ public void addTransferListener(TransferListener transferListener) {
public long open(DataSpec dataSpec) throws IOException {
try {
String key = cacheKeyFactory.buildCacheKey(dataSpec);
requestDataSpec = dataSpec.buildUpon().setKey(key).build();
DataSpec requestDataSpec = dataSpec.buildUpon().setKey(key).build();
this.requestDataSpec = requestDataSpec;
actualUri = getRedirectedUriOrDefault(cache, key, /* defaultUri= */ requestDataSpec.uri);
readPosition = dataSpec.position;

Expand All @@ -571,7 +575,7 @@ public long open(DataSpec dataSpec) throws IOException {
}
}
}
openNextSource(false);
openNextSource(requestDataSpec, false);
return bytesRemaining;
} catch (Throwable e) {
handleBeforeThrow(e);
Expand All @@ -581,6 +585,7 @@ public long open(DataSpec dataSpec) throws IOException {

@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
DataSpec requestDataSpec = checkNotNull(this.requestDataSpec);
if (readLength == 0) {
return 0;
}
Expand All @@ -589,9 +594,9 @@ public int read(byte[] buffer, int offset, int readLength) throws IOException {
}
try {
if (readPosition >= checkCachePosition) {
openNextSource(true);
openNextSource(requestDataSpec, true);
}
int bytesRead = currentDataSource.read(buffer, offset, readLength);
int bytesRead = checkNotNull(currentDataSource).read(buffer, offset, readLength);
if (bytesRead != C.RESULT_END_OF_INPUT) {
if (isReadingFromCache()) {
totalCachedBytesRead += bytesRead;
Expand All @@ -601,16 +606,16 @@ public int read(byte[] buffer, int offset, int readLength) throws IOException {
bytesRemaining -= bytesRead;
}
} else if (currentDataSpecLengthUnset) {
setNoBytesRemainingAndMaybeStoreLength();
setNoBytesRemainingAndMaybeStoreLength(castNonNull(requestDataSpec.key));
} else if (bytesRemaining > 0 || bytesRemaining == C.LENGTH_UNSET) {
closeCurrentSource();
openNextSource(false);
openNextSource(requestDataSpec, false);
return read(buffer, offset, readLength);
}
return bytesRead;
} catch (IOException e) {
if (currentDataSpecLengthUnset && DataSourceException.isCausedByPositionOutOfRange(e)) {
setNoBytesRemainingAndMaybeStoreLength();
setNoBytesRemainingAndMaybeStoreLength(castNonNull(requestDataSpec.key));
return C.RESULT_END_OF_INPUT;
}
handleBeforeThrow(e);
Expand Down Expand Up @@ -660,12 +665,13 @@ public void close() throws IOException {
* opened if it's possible to switch to reading from or writing to the cache. If a switch isn't
* possible then the current source is left unchanged.
*
* @param requestDataSpec The original {@link DataSpec} to build upon for the next source.
* @param checkCache If true tries to switch to reading from or writing to cache instead of
* reading from {@link #upstreamDataSource}, which is the currently open source.
*/
private void openNextSource(boolean checkCache) throws IOException {
private void openNextSource(DataSpec requestDataSpec, boolean checkCache) throws IOException {
@Nullable CacheSpan nextSpan;
String key = requestDataSpec.key;
String key = castNonNull(requestDataSpec.key);
if (currentRequestIgnoresCache) {
nextSpan = null;
} else if (blockOnCache) {
Expand All @@ -689,7 +695,7 @@ private void openNextSource(boolean checkCache) throws IOException {
requestDataSpec.buildUpon().setPosition(readPosition).setLength(bytesRemaining).build();
} else if (nextSpan.isCached) {
// Data is cached in a span file starting at nextSpan.position.
Uri fileUri = Uri.fromFile(nextSpan.file);
Uri fileUri = Uri.fromFile(castNonNull(nextSpan.file));
long filePositionOffset = nextSpan.position;
long positionInFile = readPosition - filePositionOffset;
long length = nextSpan.length - positionInFile;
Expand Down Expand Up @@ -741,7 +747,7 @@ private void openNextSource(boolean checkCache) throws IOException {
try {
closeCurrentSource();
} catch (Throwable e) {
if (nextSpan.isHoleSpan()) {
if (castNonNull(nextSpan).isHoleSpan()) {
// Release the hole span before throwing, else we'll hold it forever.
cache.releaseHoleSpan(nextSpan);
}
Expand All @@ -763,7 +769,7 @@ private void openNextSource(boolean checkCache) throws IOException {
ContentMetadataMutations.setContentLength(mutations, readPosition + bytesRemaining);
}
if (isReadingFromUpstream()) {
actualUri = currentDataSource.getUri();
actualUri = nextDataSource.getUri();
boolean isRedirected = !requestDataSpec.uri.equals(actualUri);
ContentMetadataMutations.setRedirectedUri(mutations, isRedirected ? actualUri : null);
}
Expand All @@ -772,12 +778,12 @@ private void openNextSource(boolean checkCache) throws IOException {
}
}

private void setNoBytesRemainingAndMaybeStoreLength() throws IOException {
private void setNoBytesRemainingAndMaybeStoreLength(String key) throws IOException {
bytesRemaining = 0;
if (isWritingToCache()) {
ContentMetadataMutations mutations = new ContentMetadataMutations();
ContentMetadataMutations.setContentLength(mutations, readPosition);
cache.applyContentMetadataMutations(requestDataSpec.key, mutations);
cache.applyContentMetadataMutations(key, mutations);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class CacheSpan implements Comparable<CacheSpan> {
* The length of the {@link CacheSpan}, or {@link C#LENGTH_UNSET} if this is an open-ended hole.
*/
public final long length;
/**
* Whether the {@link CacheSpan} is cached.
*/
/** Whether the {@link CacheSpan} is cached. */
public final boolean isCached;
/** The file corresponding to this {@link CacheSpan}, or null if {@link #isCached} is false. */
@Nullable public final File file;
Expand Down

0 comments on commit d251788

Please sign in to comment.