use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class CacheDataSourceTest method deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash.
@Test
public void deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash() 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(halfDataLength, C.LENGTH_UNSET);
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream), dataSpec, /* temporaryBuffer= */
null, /* progressListener= */
null);
cacheWriter.cache();
// 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);
DataSourceUtil.readExactly(cacheDataSource, 100);
// Delete cached data.
cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
assertCacheEmpty(cache);
// Read the rest of the data.
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
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);
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class CacheWriterTest method cacheUnknownLengthPartialCaching.
@Test
public void cacheUnknownLengthPartialCaching() 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, /* position= */
10, /* length= */
20);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(0, 20, 20);
counters.reset();
cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(testUri), /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(20, 80, 100);
assertCachedData(cache, fakeDataSet);
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
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();
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class PsExtractorSeekTest method readInputLength.
// Internal methods
private long readInputLength() throws IOException {
DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + PS_FILE_PATH));
long totalInputLength = dataSource.open(dataSpec);
DataSourceUtil.closeQuietly(dataSource);
return totalInputLength;
}
Aggregations