diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index c9eaa33204..067cfe4fcd 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -119,6 +119,13 @@ public class CacheDataSourceTest extends InstrumentationTestCase { C.LENGTH_UNSET, KEY_2))); } + public void testIgnoreCacheForUnsetLengthRequests() throws Exception { + CacheDataSource cacheDataSource = createCacheDataSource(false, true, + CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS); + assertReadData(cacheDataSource, true, 0, C.LENGTH_UNSET); + MoreAsserts.assertEmpty(simpleCache.getKeys()); + } + private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength) throws IOException { // Read all data from upstream and cache @@ -171,6 +178,12 @@ public class CacheDataSourceTest extends InstrumentationTestCase { private CacheDataSource createCacheDataSource(boolean setReadException, boolean simulateUnknownLength) { + return createCacheDataSource(setReadException, simulateUnknownLength, + CacheDataSource.FLAG_BLOCK_ON_CACHE); + } + + private CacheDataSource createCacheDataSource(boolean setReadException, + boolean simulateUnknownLength, @CacheDataSource.Flags int flags) { Builder builder = new Builder(); if (setReadException) { builder.appendReadError(new IOException("Shouldn't read from upstream")); @@ -178,8 +191,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { builder.setSimulateUnknownLength(simulateUnknownLength); builder.appendReadData(TEST_DATA); FakeDataSource upstream = builder.build(); - return new CacheDataSource(simpleCache, upstream, CacheDataSource.FLAG_BLOCK_ON_CACHE, - MAX_CACHE_FILE_SIZE); + return new CacheDataSource(simpleCache, upstream, flags, MAX_CACHE_FILE_SIZE); } } diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 4dc5431b47..9b29984d06 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -330,16 +330,16 @@ public final class CacheDataSource implements DataSource { // bytesRemaining == C.LENGTH_UNSET) and got a resolved length from open() request if (currentRequestUnbounded && currentBytesRemaining != C.LENGTH_UNSET) { bytesRemaining = currentBytesRemaining; - // If writing into cache - if (lockedSpan != null) { - setContentLength(dataSpec.position + bytesRemaining); - } + setContentLength(dataSpec.position + bytesRemaining); } return successful; } private void setContentLength(long length) throws IOException { - cache.setContentLength(key, length); + // If writing into cache + if (currentDataSource == cacheWriteDataSource) { + cache.setContentLength(key, length); + } } private void closeCurrentSource() throws IOException {