use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.
the class CacheAsserts method assertDataCached.
/**
* Asserts that the cache contains the given data for {@code dataSpec}.
*
* @throws IOException If an error occurred reading from the Cache.
*/
public static void assertDataCached(Cache cache, DataSpec dataSpec, byte[] expected) throws IOException {
DataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0);
byte[] bytes;
try {
dataSource.open(dataSpec);
bytes = DataSourceUtil.readToEnd(dataSource);
} catch (IOException e) {
throw new IOException("Opening/reading cache failed: " + dataSpec, e);
} finally {
dataSource.close();
}
assertWithMessage("Cached data doesn't match expected for '" + dataSpec.uri + "',").that(bytes).isEqualTo(expected);
}
use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.
the class CacheDataSourceTest method boundedRead_doesNotSetContentLength.
@Test
public void boundedRead_doesNotSetContentLength() throws Exception {
DataSpec dataSpec = buildDataSpec(0, TEST_DATA.length);
// Read up to the end of the data, but since the DataSpec is bounded, the read doesn't see the
// EOS, and so the content length remains unknown.
CacheDataSource cacheDataSource = createCacheDataSource(false, true);
assertReadData(cacheDataSource, dataSpec, true);
assertThat(ContentMetadata.getContentLength(cache.getContentMetadata(defaultCacheKey))).isEqualTo(C.LENGTH_UNSET);
}
use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.
the class CacheDataSourceTest method switchToCacheSourceWithReadOnlyCacheDataSource.
@Test
public void switchToCacheSourceWithReadOnlyCacheDataSource() throws Exception {
// Create a fake data source with a 1 MB default data.
FakeDataSource upstream = new FakeDataSource();
FakeData fakeData = upstream.getDataSet().newDefaultData().appendReadData(1024 * 1024 - 1);
// Insert an action just before the end of the data to fail the test if reading from upstream
// reaches end of the data.
fakeData.appendReadAction(() -> fail("Read from upstream shouldn't reach to the end of the data.")).appendReadData(1);
// Create cache read-only CacheDataSource.
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, new FileDataSource(), null, 0, null);
// Open source and read some data from upstream as the data hasn't cached yet.
cacheDataSource.open(unboundedDataSpec);
byte[] buffer = new byte[1024];
cacheDataSource.read(buffer, 0, buffer.length);
// Cache the data. Although we use another FakeDataSource instance, it shouldn't matter.
FakeDataSource upstream2 = new FakeDataSource(new FakeDataSource().getDataSet().newDefaultData().appendReadData(1024 * 1024).endData());
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream2), unboundedDataSpec, /* temporaryBuffer= */
null, /* progressListener= */
null);
cacheWriter.cache();
// Read the rest of the data.
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.
the class CacheDataSourceTest method unknownLengthContentReadInOneConnectionAndLengthIsResolved.
@Test
public void unknownLengthContentReadInOneConnectionAndLengthIsResolved() throws Exception {
FakeDataSource upstream = new FakeDataSource();
upstream.getDataSet().newData(testDataUri).appendReadData(TEST_DATA).setSimulateUnknownLength(true);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
cacheDataSource.open(unboundedDataSpec);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
assertThat(upstream.getAndClearOpenedDataSpecs()).hasLength(1);
assertThat(ContentMetadata.getContentLength(cache.getContentMetadata(defaultCacheKey))).isEqualTo(TEST_DATA.length);
}
use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.
the class CacheDataSourceTest method deleteCachedWhileReadingFromUpstreamWithBlockingCacheDataSourceDoesNotBlock.
@Test
public void deleteCachedWhileReadingFromUpstreamWithBlockingCacheDataSourceDoesNotBlock() throws Exception {
// Create a fake data source with a 1 KB default data.
FakeDataSource upstream = new FakeDataSource();
int dataLength = 1024;
upstream.getDataSet().newDefaultData().appendReadData(dataLength).endData();
// Cache the latter half of the data.
int halfDataLength = 512;
DataSpec dataSpec = buildDataSpec(/* position= */
0, halfDataLength);
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream), dataSpec, /* temporaryBuffer= */
null, /* progressListener= */
null);
cacheWriter.cache();
// Create blocking CacheDataSource.
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, CacheDataSource.FLAG_BLOCK_ON_CACHE);
cacheDataSource.open(unboundedDataSpec);
// Read the first half from upstream as it hasn't cached yet.
DataSourceUtil.readExactly(cacheDataSource, halfDataLength);
// Delete the cached latter half.
NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(defaultCacheKey);
for (CacheSpan cachedSpan : cachedSpans) {
if (cachedSpan.position >= halfDataLength) {
cache.removeSpan(cachedSpan);
}
}
// Read the rest of the data.
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
Aggregations