From b6089758ffa7caf37749183119e0ddfc98e65dd3 Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 7 Sep 2021 14:27:02 +0100 Subject: [PATCH] Fix incorrect assertion in CacheDataSource #minor-release #exofixit PiperOrigin-RevId: 395233639 --- RELEASENOTES.md | 3 +++ .../upstream/cache/CacheDataSource.java | 4 ++-- .../upstream/cache/CacheDataSourceTest.java | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index e72169a16e..fe7021b519 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -14,6 +14,9 @@ * Remove `ExoPlayerLibraryInfo.GL_ASSERTIONS_ENABLED`. Use `GlUtil.glAssertionsEnabled` instead. * Fix `FlagSet#equals` on API levels below 24. + * Fix `NullPointerException` being thrown from `CacheDataSource` when + reading a fully cached resource with `DataSpec.position` equal to the + resource length. * Extractors: * Support TS packets without PTS flag ([#9294](https://github.com/google/ExoPlayer/issues/9294)). diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 931cdf4552..338d9cd2ac 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -597,14 +597,14 @@ public final class CacheDataSource implements DataSource { @Override public int read(byte[] buffer, int offset, int length) throws IOException { - DataSpec requestDataSpec = checkNotNull(this.requestDataSpec); - DataSpec currentDataSpec = checkNotNull(this.currentDataSpec); if (length == 0) { return 0; } if (bytesRemaining == 0) { return C.RESULT_END_OF_INPUT; } + DataSpec requestDataSpec = checkNotNull(this.requestDataSpec); + DataSpec currentDataSpec = checkNotNull(this.currentDataSpec); try { if (readPosition >= checkCachePosition) { openNextSource(requestDataSpec, true); diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index 705188f88e..eefc4eb8c4 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -124,6 +124,28 @@ public final class CacheDataSourceTest { assertCacheAndRead(boundedDataSpec, /* unknownLength= */ false); } + @Test + public void cacheAndReadFromLength_readsZeroBytes() throws Exception { + // Read and cache all data from upstream. + CacheDataSource cacheDataSource = + createCacheDataSource( + /* setReadException= */ false, /* unknownLength= */ false, /* cacheKeyFactory= */ null); + assertReadDataContentLength( + cacheDataSource, + unboundedDataSpec, + /* unknownLength= */ false, + /* customCacheKey= */ false); + + // Read from position == length. + cacheDataSource = + createCacheDataSource( + /* setReadException= */ true, /* unknownLength= */ false, /* cacheKeyFactory= */ null); + assertReadData( + cacheDataSource, + unboundedDataSpec.buildUpon().setPosition(TEST_DATA.length).build(), + /* unknownLength= */ false); + } + @Test public void propagatesHttpHeadersUpstream() throws Exception { CacheDataSource cacheDataSource =