CacheUtil { @WorkerThread public static void cache( DataSpec dataSpec, Cache cache, @Nullable CacheKeyFactory cacheKeyFactory, DataSource upstream, @Nullable ProgressListener progressListener, @Nullable AtomicBoolean isCanceled) throws IOException, InterruptedException { cache( dataSpec, cache, cacheKeyFactory, new CacheDataSource(cache, upstream), new byte[DEFAULT_BUFFER_SIZE_BYTES], null, 0, progressListener, isCanceled, false); } 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 testCache() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); CachingCounters counters = new CachingCounters(); CacheUtil.cache( new DataSpec(Uri.parse("test_data")), cache, null, dataSource, counters, null); counters.assertValues(0, 100, 100); assertCachedData(cache, fakeDataSet); }
@Test public void testCacheSetOffsetAndLength() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); Uri testUri = Uri.parse("test_data"); DataSpec dataSpec = new DataSpec(testUri, 10, 20, null); CachingCounters counters = new CachingCounters(); CacheUtil.cache( dataSpec, cache, null, dataSource, counters, null); counters.assertValues(0, 20, 20); counters.reset(); CacheUtil.cache( new DataSpec(testUri), cache, null, dataSource, counters, null); counters.assertValues(20, 80, 100); assertCachedData(cache, fakeDataSet); }
@Test public void testCacheUnknownLength() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data") .setSimulateUnknownLength(true) .appendReadData(TestUtil.buildTestData(100)).endData(); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); DataSpec dataSpec = new DataSpec(Uri.parse("test_data")); CachingCounters counters = new CachingCounters(); CacheUtil.cache( dataSpec, cache, null, dataSource, counters, null); counters.assertValues(0, 100, 100); assertCachedData(cache, fakeDataSet); }
@Test public void testCacheUnknownLengthPartialCaching() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data") .setSimulateUnknownLength(true) .appendReadData(TestUtil.buildTestData(100)).endData(); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); Uri testUri = Uri.parse("test_data"); DataSpec dataSpec = new DataSpec(testUri, 10, 20, null); CachingCounters counters = new CachingCounters(); CacheUtil.cache( dataSpec, cache, null, dataSource, counters, null); counters.assertValues(0, 20, 20); counters.reset(); CacheUtil.cache( new DataSpec(testUri), cache, null, dataSource, counters, null); counters.assertValues(20, 80, 100); assertCachedData(cache, fakeDataSet); }
@Test public void testCacheLengthExceedsActualDataLength() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); Uri testUri = Uri.parse("test_data"); DataSpec dataSpec = new DataSpec(testUri, 0, 1000, null); CachingCounters counters = new CachingCounters(); CacheUtil.cache( dataSpec, cache, null, dataSource, counters, null); counters.assertValues(0, 100, 1000); assertCachedData(cache, fakeDataSet); }
@Test public void testCacheThrowEOFException() throws Exception { FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); Uri testUri = Uri.parse("test_data"); DataSpec dataSpec = new DataSpec(testUri, 0, 1000, null); try { CacheUtil.cache( dataSpec, cache, null, new CacheDataSource(cache, dataSource), new byte[CacheUtil.DEFAULT_BUFFER_SIZE_BYTES], null, 0, null, null, true); fail(); } catch (EOFException e) { } }
@Test public void testCachePolling() throws Exception { final CachingCounters counters = new CachingCounters(); FakeDataSet fakeDataSet = new FakeDataSet() .newData("test_data") .appendReadData(TestUtil.buildTestData(100)) .appendReadAction(() -> counters.assertValues(0, 100, 300)) .appendReadData(TestUtil.buildTestData(100)) .appendReadAction(() -> counters.assertValues(0, 200, 300)) .appendReadData(TestUtil.buildTestData(100)) .endData(); FakeDataSource dataSource = new FakeDataSource(fakeDataSet); CacheUtil.cache( new DataSpec(Uri.parse("test_data")), cache, null, dataSource, counters, null); counters.assertValues(0, 300, 300); assertCachedData(cache, fakeDataSet); }