CacheUtil { public static Pair<Long, Long> getCached( DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory) { String key = buildCacheKey(dataSpec, cacheKeyFactory); long position = dataSpec.absoluteStreamPosition; long requestLength = getRequestLength(dataSpec, cache, key); long bytesAlreadyCached = 0; long bytesLeft = requestLength; while (bytesLeft != 0) { long blockLength = cache.getCachedLength( key, position, bytesLeft != C.LENGTH_UNSET ? bytesLeft : Long.MAX_VALUE); if (blockLength > 0) { bytesAlreadyCached += blockLength; } else { blockLength = -blockLength; if (blockLength == Long.MAX_VALUE) { break; } } position += blockLength; bytesLeft -= bytesLeft == C.LENGTH_UNSET ? 0 : blockLength; } return Pair.create(requestLength, bytesAlreadyCached); } private  CacheUtil(); static String generateKey(Uri uri); static Pair<Long, Long> getCached(
      DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory); @WorkerThread static void cache(
      DataSpec dataSpec,
      Cache cache,
      @Nullable CacheKeyFactory cacheKeyFactory,
      DataSource upstream,
      @Nullable ProgressListener progressListener,
      @Nullable AtomicBoolean isCanceled); @WorkerThread static void cache(
      DataSpec dataSpec,
      Cache cache,
      @Nullable CacheKeyFactory cacheKeyFactory,
      CacheDataSource dataSource,
      byte[] buffer,
      @Nullable PriorityTaskManager priorityTaskManager,
      int priority,
      @Nullable ProgressListener progressListener,
      @Nullable AtomicBoolean isCanceled,
      boolean enableEOFException); @WorkerThread static void remove(
      DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory); @WorkerThread static void remove(Cache cache, String key); static final int DEFAULT_BUFFER_SIZE_BYTES; static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY; }
@Test public void testGetCachedNoData() { Pair<Long, Long> contentLengthAndBytesCached = CacheUtil.getCached( new DataSpec(Uri.parse("test")), mockCache, null); assertThat(contentLengthAndBytesCached.first).isEqualTo(C.LENGTH_UNSET); assertThat(contentLengthAndBytesCached.second).isEqualTo(0); }
@Test public void testGetCachedDataUnknownLength() { mockCache.spansAndGaps = new int[] {100}; Pair<Long, Long> contentLengthAndBytesCached = CacheUtil.getCached( new DataSpec(Uri.parse("test")), mockCache, null); assertThat(contentLengthAndBytesCached.first).isEqualTo(C.LENGTH_UNSET); assertThat(contentLengthAndBytesCached.second).isEqualTo(100); }
@Test public void testGetCachedNoDataKnownLength() { mockCache.contentLength = 1000; Pair<Long, Long> contentLengthAndBytesCached = CacheUtil.getCached( new DataSpec(Uri.parse("test")), mockCache, null); assertThat(contentLengthAndBytesCached.first).isEqualTo(1000); assertThat(contentLengthAndBytesCached.second).isEqualTo(0); }
@Test public void testGetCached() { mockCache.contentLength = 1000; mockCache.spansAndGaps = new int[] {100, 100, 200}; Pair<Long, Long> contentLengthAndBytesCached = CacheUtil.getCached( new DataSpec(Uri.parse("test")), mockCache, null); assertThat(contentLengthAndBytesCached.first).isEqualTo(1000); assertThat(contentLengthAndBytesCached.second).isEqualTo(300); }
@Test public void testGetCachedFromNonZeroPosition() { mockCache.contentLength = 1000; mockCache.spansAndGaps = new int[] {100, 100, 200}; Pair<Long, Long> contentLengthAndBytesCached = CacheUtil.getCached( new DataSpec( Uri.parse("test"), 100, C.LENGTH_UNSET, null), mockCache, null); assertThat(contentLengthAndBytesCached.first).isEqualTo(900); assertThat(contentLengthAndBytesCached.second).isEqualTo(200); }