Allow passing C.LENGTH_UNSET to getCachedLength
The caller will often have C.LENGTH_UNSET already, and it's awkward to force them to do the conversion themselves. PiperOrigin-RevId: 312276810
This commit is contained in:
parent
1be295ab0c
commit
a42a1f49ed
@ -272,7 +272,8 @@ public interface Cache {
|
|||||||
*
|
*
|
||||||
* @param key The cache key of the resource.
|
* @param key The cache key of the resource.
|
||||||
* @param position The starting position of the data in 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}
|
* @return The length of the continuously cached data, or {@code -holeLength} if {@code position}
|
||||||
* isn't cached.
|
* isn't cached.
|
||||||
*/
|
*/
|
||||||
|
@ -74,9 +74,7 @@ public final class CacheUtil {
|
|||||||
long bytesAlreadyCached = 0;
|
long bytesAlreadyCached = 0;
|
||||||
long bytesLeft = requestLength;
|
long bytesLeft = requestLength;
|
||||||
while (bytesLeft != 0) {
|
while (bytesLeft != 0) {
|
||||||
long blockLength =
|
long blockLength = cache.getCachedLength(key, position, bytesLeft);
|
||||||
cache.getCachedLength(
|
|
||||||
key, position, bytesLeft != C.LENGTH_UNSET ? bytesLeft : Long.MAX_VALUE);
|
|
||||||
if (blockLength > 0) {
|
if (blockLength > 0) {
|
||||||
bytesAlreadyCached += blockLength;
|
bytesAlreadyCached += blockLength;
|
||||||
} else {
|
} else {
|
||||||
@ -173,11 +171,9 @@ public final class CacheUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long position = dataSpec.position;
|
long position = dataSpec.position;
|
||||||
boolean lengthUnset = bytesLeft == C.LENGTH_UNSET;
|
|
||||||
while (bytesLeft != 0) {
|
while (bytesLeft != 0) {
|
||||||
throwExceptionIfCanceled(isCanceled);
|
throwExceptionIfCanceled(isCanceled);
|
||||||
long blockLength =
|
long blockLength = cache.getCachedLength(key, position, bytesLeft);
|
||||||
cache.getCachedLength(key, position, lengthUnset ? Long.MAX_VALUE : bytesLeft);
|
|
||||||
if (blockLength > 0) {
|
if (blockLength > 0) {
|
||||||
// Skip already cached data.
|
// Skip already cached data.
|
||||||
} else {
|
} else {
|
||||||
@ -197,14 +193,14 @@ public final class CacheUtil {
|
|||||||
temporaryBuffer);
|
temporaryBuffer);
|
||||||
if (read < blockLength) {
|
if (read < blockLength) {
|
||||||
// Reached to the end of the data.
|
// Reached to the end of the data.
|
||||||
if (enableEOFException && !lengthUnset) {
|
if (enableEOFException && bytesLeft != C.LENGTH_UNSET) {
|
||||||
throw new EOFException();
|
throw new EOFException();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
position += blockLength;
|
position += blockLength;
|
||||||
if (!lengthUnset) {
|
if (bytesLeft != C.LENGTH_UNSET) {
|
||||||
bytesLeft -= blockLength;
|
bytesLeft -= blockLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -494,6 +494,9 @@ public final class SimpleCache implements Cache {
|
|||||||
@Override
|
@Override
|
||||||
public synchronized long getCachedLength(String key, long position, long length) {
|
public synchronized long getCachedLength(String key, long position, long length) {
|
||||||
Assertions.checkState(!released);
|
Assertions.checkState(!released);
|
||||||
|
if (length == C.LENGTH_UNSET) {
|
||||||
|
length = Long.MAX_VALUE;
|
||||||
|
}
|
||||||
@Nullable CachedContent cachedContent = contentIndex.get(key);
|
@Nullable CachedContent cachedContent = contentIndex.get(key);
|
||||||
return cachedContent != null ? cachedContent.getCachedBytesLength(position, length) : -length;
|
return cachedContent != null ? cachedContent.getCachedBytesLength(position, length) : -length;
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,9 @@ public final class CacheUtilTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getCachedLength(String key, long position, long length) {
|
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++) {
|
for (int i = 0; i < spansAndGaps.length; i++) {
|
||||||
int spanOrGap = spansAndGaps[i];
|
int spanOrGap = spansAndGaps[i];
|
||||||
if (position < spanOrGap) {
|
if (position < spanOrGap) {
|
||||||
|
@ -307,10 +307,14 @@ public class SimpleCacheTest {
|
|||||||
.isEqualTo(-100);
|
.isEqualTo(-100);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(-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))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
||||||
.isEqualTo(-100);
|
.isEqualTo(-100);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(-Long.MAX_VALUE);
|
.isEqualTo(-Long.MAX_VALUE);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(-Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -324,10 +328,14 @@ public class SimpleCacheTest {
|
|||||||
.isEqualTo(-50);
|
.isEqualTo(-50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(-50);
|
.isEqualTo(-50);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(-50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
||||||
.isEqualTo(-30);
|
.isEqualTo(-30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(-30);
|
.isEqualTo(-30);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(-30);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -341,10 +349,14 @@ public class SimpleCacheTest {
|
|||||||
.isEqualTo(50);
|
.isEqualTo(50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(50);
|
.isEqualTo(50);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
||||||
.isEqualTo(15);
|
.isEqualTo(15);
|
||||||
}
|
}
|
||||||
@ -361,10 +373,14 @@ public class SimpleCacheTest {
|
|||||||
.isEqualTo(50);
|
.isEqualTo(50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(50);
|
.isEqualTo(50);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(50);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
||||||
.isEqualTo(15);
|
.isEqualTo(15);
|
||||||
}
|
}
|
||||||
@ -381,10 +397,14 @@ public class SimpleCacheTest {
|
|||||||
.isEqualTo(10);
|
.isEqualTo(10);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(10);
|
.isEqualTo(10);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 0, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(10);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 100))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ Long.MAX_VALUE))
|
||||||
.isEqualTo(30);
|
.isEqualTo(30);
|
||||||
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ LENGTH_UNSET))
|
||||||
|
.isEqualTo(30);
|
||||||
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
assertThat(simpleCache.getCachedLength(KEY_1, /* position= */ 20, /* length= */ 15))
|
||||||
.isEqualTo(15);
|
.isEqualTo(15);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user