diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java index fe7d34850b..781b5619d0 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java @@ -272,7 +272,8 @@ public interface Cache { * * @param key The cache key of the resource. * @param position The starting position of the data in the resource. - * @param length The maximum length of the data or hole to be returned. + * @param length The maximum length of the data or hole to be returned. {@link C#LENGTH_UNSET} is + * permitted, and is equivalent to passing {@link Long#MAX_VALUE}. * @return The length of the continuously cached data, or {@code -holeLength} if {@code position} * isn't cached. */ diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java index 47b61780d8..57c9ecbf55 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java @@ -74,9 +74,7 @@ public final class CacheUtil { long bytesAlreadyCached = 0; long bytesLeft = requestLength; while (bytesLeft != 0) { - long blockLength = - cache.getCachedLength( - key, position, bytesLeft != C.LENGTH_UNSET ? bytesLeft : Long.MAX_VALUE); + long blockLength = cache.getCachedLength(key, position, bytesLeft); if (blockLength > 0) { bytesAlreadyCached += blockLength; } else { @@ -173,11 +171,9 @@ public final class CacheUtil { } long position = dataSpec.position; - boolean lengthUnset = bytesLeft == C.LENGTH_UNSET; while (bytesLeft != 0) { throwExceptionIfCanceled(isCanceled); - long blockLength = - cache.getCachedLength(key, position, lengthUnset ? Long.MAX_VALUE : bytesLeft); + long blockLength = cache.getCachedLength(key, position, bytesLeft); if (blockLength > 0) { // Skip already cached data. } else { @@ -197,14 +193,14 @@ public final class CacheUtil { temporaryBuffer); if (read < blockLength) { // Reached to the end of the data. - if (enableEOFException && !lengthUnset) { + if (enableEOFException && bytesLeft != C.LENGTH_UNSET) { throw new EOFException(); } break; } } position += blockLength; - if (!lengthUnset) { + if (bytesLeft != C.LENGTH_UNSET) { bytesLeft -= blockLength; } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index 0cb379b241..c71d8e2714 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -494,6 +494,9 @@ public final class SimpleCache implements Cache { @Override public synchronized long getCachedLength(String key, long position, long length) { Assertions.checkState(!released); + if (length == C.LENGTH_UNSET) { + length = Long.MAX_VALUE; + } @Nullable CachedContent cachedContent = contentIndex.get(key); return cachedContent != null ? cachedContent.getCachedBytesLength(position, length) : -length; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java index c4115cbc28..eba664648b 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java @@ -63,6 +63,9 @@ public final class CacheUtilTest { @Override public long getCachedLength(String key, long position, long length) { + if (length == C.LENGTH_UNSET) { + length = Long.MAX_VALUE; + } for (int i = 0; i < spansAndGaps.length; i++) { int spanOrGap = spansAndGaps[i]; if (position < spanOrGap) { diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java index c61a3533d6..73cde3dee4 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java @@ -307,10 +307,14 @@ public class SimpleCacheTest { .isEqualTo(-100); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE)) .isEqualTo(-Long.MAX_VALUE); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET)) + .isEqualTo(-Long.MAX_VALUE); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100)) .isEqualTo(-100); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE)) .isEqualTo(-Long.MAX_VALUE); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET)) + .isEqualTo(-Long.MAX_VALUE); } @Test @@ -324,10 +328,14 @@ public class SimpleCacheTest { .isEqualTo(-50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE)) .isEqualTo(-50); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET)) + .isEqualTo(-50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100)) .isEqualTo(-30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE)) .isEqualTo(-30); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET)) + .isEqualTo(-30); } @Test @@ -341,10 +349,14 @@ public class SimpleCacheTest { .isEqualTo(50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE)) .isEqualTo(50); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET)) + .isEqualTo(50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100)) .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE)) .isEqualTo(30); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET)) + .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15)) .isEqualTo(15); } @@ -361,10 +373,14 @@ public class SimpleCacheTest { .isEqualTo(50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE)) .isEqualTo(50); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET)) + .isEqualTo(50); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100)) .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE)) .isEqualTo(30); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET)) + .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15)) .isEqualTo(15); } @@ -381,10 +397,14 @@ public class SimpleCacheTest { .isEqualTo(10); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE)) .isEqualTo(10); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET)) + .isEqualTo(10); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100)) .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE)) .isEqualTo(30); + assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET)) + .isEqualTo(30); assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15)) .isEqualTo(15); }