use of androidx.media3.datasource.DataSink in project media by androidx.
the class CacheDataSourceTest2 method buildCacheDataSource.
private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException {
File cacheDir = context.getExternalCacheDir();
Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
emptyCache(cache);
// Source and cipher
final String secretKey = "testKey:12345678";
DataSource file = new FileDataSource();
DataSource cacheReadDataSource = useAesEncryption ? new AesCipherDataSource(Util.getUtf8Bytes(secretKey), file) : file;
// Sink and cipher
CacheDataSink cacheSink = new CacheDataSink(cache, EXO_CACHE_MAX_FILESIZE);
byte[] scratch = new byte[3897];
DataSink cacheWriteDataSink = useAesEncryption ? new AesCipherDataSink(Util.getUtf8Bytes(secretKey), cacheSink, scratch) : cacheSink;
return new CacheDataSource(cache, upstreamSource, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, // eventListener
null);
}
use of androidx.media3.datasource.DataSink in project media by androidx.
the class CacheWriterTest method cache_afterFailureOnClose_succeeds.
@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
true);
FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
0, /* eventListener= */
null);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
null, counters);
// DataSink.close failing must cause the operation to fail rather than succeed.
assertThrows(IOException.class, cacheWriter::cache);
// Since all of the bytes were read through the DataSource chain successfully before the sink
// was closed, the progress listener will have seen all of the bytes being cached, even though
// this may not really be the case.
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
failOnClose.set(false);
// The bytes will be downloaded again, but cached successfully this time.
cacheWriter.cache();
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
assertCachedData(cache, fakeDataSet);
}
Aggregations