Simplify CacheUtil

PiperOrigin-RevId: 250654697
This commit is contained in:
eguven 2019-05-30 10:40:00 +01:00 committed by Toni
parent 42ba6abf5a
commit e2452f8103

View File

@ -79,13 +79,7 @@ public final class CacheUtil {
DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory) { DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory) {
String key = buildCacheKey(dataSpec, cacheKeyFactory); String key = buildCacheKey(dataSpec, cacheKeyFactory);
long position = dataSpec.absoluteStreamPosition; long position = dataSpec.absoluteStreamPosition;
long requestLength; long requestLength = getRequestLength(dataSpec, cache, key);
if (dataSpec.length != C.LENGTH_UNSET) {
requestLength = dataSpec.length;
} else {
long contentLength = ContentMetadata.getContentLength(cache.getContentMetadata(key));
requestLength = contentLength == C.LENGTH_UNSET ? C.LENGTH_UNSET : contentLength - position;
}
long bytesAlreadyCached = 0; long bytesAlreadyCached = 0;
long bytesLeft = requestLength; long bytesLeft = requestLength;
while (bytesLeft != 0) { while (bytesLeft != 0) {
@ -180,22 +174,19 @@ public final class CacheUtil {
Assertions.checkNotNull(dataSource); Assertions.checkNotNull(dataSource);
Assertions.checkNotNull(buffer); Assertions.checkNotNull(buffer);
String key = buildCacheKey(dataSpec, cacheKeyFactory);
long bytesLeft;
ProgressNotifier progressNotifier = null; ProgressNotifier progressNotifier = null;
if (progressListener != null) { if (progressListener != null) {
progressNotifier = new ProgressNotifier(progressListener); progressNotifier = new ProgressNotifier(progressListener);
Pair<Long, Long> lengthAndBytesAlreadyCached = getCached(dataSpec, cache, cacheKeyFactory); Pair<Long, Long> lengthAndBytesAlreadyCached = getCached(dataSpec, cache, cacheKeyFactory);
progressNotifier.init(lengthAndBytesAlreadyCached.first, lengthAndBytesAlreadyCached.second); progressNotifier.init(lengthAndBytesAlreadyCached.first, lengthAndBytesAlreadyCached.second);
bytesLeft = lengthAndBytesAlreadyCached.first;
} else {
bytesLeft = getRequestLength(dataSpec, cache, key);
} }
String key = buildCacheKey(dataSpec, cacheKeyFactory);
long position = dataSpec.absoluteStreamPosition; long position = dataSpec.absoluteStreamPosition;
long bytesLeft;
if (dataSpec.length != C.LENGTH_UNSET) {
bytesLeft = dataSpec.length;
} else {
long contentLength = ContentMetadata.getContentLength(cache.getContentMetadata(key));
bytesLeft = contentLength == C.LENGTH_UNSET ? C.LENGTH_UNSET : contentLength - position;
}
boolean lengthUnset = bytesLeft == C.LENGTH_UNSET; boolean lengthUnset = bytesLeft == C.LENGTH_UNSET;
while (bytesLeft != 0) { while (bytesLeft != 0) {
throwExceptionIfInterruptedOrCancelled(isCanceled); throwExceptionIfInterruptedOrCancelled(isCanceled);
@ -235,6 +226,17 @@ public final class CacheUtil {
} }
} }
private static long getRequestLength(DataSpec dataSpec, Cache cache, String key) {
if (dataSpec.length != C.LENGTH_UNSET) {
return dataSpec.length;
} else {
long contentLength = ContentMetadata.getContentLength(cache.getContentMetadata(key));
return contentLength == C.LENGTH_UNSET
? C.LENGTH_UNSET
: contentLength - dataSpec.absoluteStreamPosition;
}
}
/** /**
* Reads and discards all data specified by the {@code dataSpec}. * Reads and discards all data specified by the {@code dataSpec}.
* *