mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
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 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.
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user