diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceException.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceException.java index e6b3ae2707..a45b7db2f2 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceException.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DataSourceException.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.upstream; +import androidx.annotation.Nullable; import java.io.IOException; /** @@ -22,6 +23,24 @@ import java.io.IOException; */ public final class DataSourceException extends IOException { + /** + * Returns whether the given {@link IOException} was caused by a {@link DataSourceException} whose + * {@link #reason} is {@link #POSITION_OUT_OF_RANGE} in its cause stack. + */ + public static boolean isCausedByPositionOutOfRange(IOException e) { + @Nullable Throwable cause = e; + while (cause != null) { + if (cause instanceof DataSourceException) { + int reason = ((DataSourceException) cause).reason; + if (reason == DataSourceException.POSITION_OUT_OF_RANGE) { + return true; + } + } + cause = cause.getCause(); + } + return false; + } + public static final int POSITION_OUT_OF_RANGE = 0; /** diff --git a/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSourceExceptionTest.java b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSourceExceptionTest.java new file mode 100644 index 0000000000..59a4939a68 --- /dev/null +++ b/library/common/src/test/java/com/google/android/exoplayer2/upstream/DataSourceExceptionTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.upstream; + +import static com.google.common.truth.Truth.assertThat; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import java.io.IOException; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Unit tests for {@link DataSourceException}. */ +@RunWith(AndroidJUnit4.class) +public class DataSourceExceptionTest { + + private static final int REASON_OTHER = DataSourceException.POSITION_OUT_OF_RANGE - 1; + + @Test + public void isCausedByPositionOutOfRange_reasonIsPositionOutOfRange_returnsTrue() { + DataSourceException e = new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); + } + + @Test + public void isCausedByPositionOutOfRange_reasonIsOther_returnsFalse() { + DataSourceException e = new DataSourceException(REASON_OTHER); + assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse(); + } + + @Test + public void isCausedByPositionOutOfRange_indirectauseReasonIsPositionOutOfRange_returnsTrue() { + DataSourceException cause = new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + IOException e = new IOException(new IOException(cause)); + assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); + } + + @Test + public void isCausedByPositionOutOfRange_causeReasonIsOther_returnsFalse() { + DataSourceException cause = new DataSourceException(REASON_OTHER); + IOException e = new IOException(new IOException(cause)); + assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse(); + } +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 2a8c4666e1..d20aaa0b63 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -630,7 +630,7 @@ public final class CacheDataSource implements DataSource { } return bytesRead; } catch (IOException e) { - if (currentDataSpecLengthUnset && CacheUtil.isCausedByPositionOutOfRange(e)) { + if (currentDataSpecLengthUnset && DataSourceException.isCausedByPositionOutOfRange(e)) { setNoBytesRemainingAndMaybeStoreLength(); return C.RESULT_END_OF_INPUT; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java index be600a4a87..08622df3ea 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java @@ -273,7 +273,7 @@ public final class CacheUtil { dataSource.open(dataSpec.subrange(positionOffset, endOffset - positionOffset)); isDataSourceOpen = true; } catch (IOException exception) { - if (!isLastBlock || !isCausedByPositionOutOfRange(exception)) { + if (!isLastBlock || !DataSourceException.isCausedByPositionOutOfRange(exception)) { throw exception; } Util.closeQuietly(dataSource); @@ -349,20 +349,6 @@ public final class CacheUtil { } } - /* package */ static boolean isCausedByPositionOutOfRange(IOException e) { - @Nullable Throwable cause = e; - while (cause != null) { - if (cause instanceof DataSourceException) { - int reason = ((DataSourceException) cause).reason; - if (reason == DataSourceException.POSITION_OUT_OF_RANGE) { - return true; - } - } - cause = cause.getCause(); - } - return false; - } - private static String buildCacheKey( DataSpec dataSpec, @Nullable CacheKeyFactory cacheKeyFactory) { return (cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT)