diff --git a/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java b/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java index 1f21c75211..0278a888ff 100644 --- a/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java +++ b/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java @@ -24,6 +24,7 @@ import android.text.TextUtils; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.upstream.BaseDataSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSourceException; @@ -675,7 +676,10 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { dataSpec, responseBody); if (responseCode == 416) { - exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE)); + exception.initCause( + new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ)); } throw exception; } @@ -717,7 +721,9 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } catch (IOException e) { throw new OpenException(e, dataSpec, Status.READING_RESPONSE); diff --git a/extensions/okhttp/src/main/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSource.java b/extensions/okhttp/src/main/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSource.java index 3e3c75ffee..ab7cb29e3b 100644 --- a/extensions/okhttp/src/main/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSource.java +++ b/extensions/okhttp/src/main/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSource.java @@ -23,6 +23,7 @@ import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlayerLibraryInfo; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.upstream.BaseDataSource; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSourceException; @@ -323,7 +324,10 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { new InvalidResponseCodeException( responseCode, response.message(), headers, dataSpec, errorResponseBody); if (responseCode == 416) { - exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE)); + exception.initCause( + new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ)); } throw exception; } @@ -354,7 +358,9 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } catch (IOException e) { closeConnectionQuietly(); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/PlaybackException.java b/library/common/src/main/java/com/google/android/exoplayer2/PlaybackException.java index 1a5eaecc75..4978294e9d 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/PlaybackException.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/PlaybackException.java @@ -57,6 +57,7 @@ public class PlaybackException extends Exception implements Bundleable { ERROR_CODE_IO_FILE_NOT_FOUND, ERROR_CODE_IO_NO_PERMISSION, ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED, + ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, ERROR_CODE_PARSING_CONTAINER_MALFORMED, ERROR_CODE_PARSING_MANIFEST_MALFORMED, ERROR_CODE_PARSING_CONTAINER_UNSUPPORTED, @@ -124,6 +125,8 @@ public class PlaybackException extends Exception implements Bundleable { * troubleshooting topic. */ public static final int ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED = 2009; + /** Caused by reading data out of the data bound. */ + public static final int ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE = 2010; // Content parsing errors (3xxx). @@ -224,6 +227,8 @@ public class PlaybackException extends Exception implements Bundleable { return "ERROR_CODE_IO_NO_PERMISSION"; case ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED: return "ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED"; + case ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE: + return "ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE"; case ERROR_CODE_PARSING_CONTAINER_MALFORMED: return "ERROR_CODE_PARSING_CONTAINER_MALFORMED"; case ERROR_CODE_PARSING_MANIFEST_MALFORMED: 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 a4932fc283..1a9392d90c 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 @@ -17,6 +17,7 @@ package com.google.android.exoplayer2.upstream; import androidx.annotation.IntDef; import androidx.annotation.Nullable; +import com.google.android.exoplayer2.PlaybackException; import java.io.IOException; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -43,14 +44,15 @@ public 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. + * {@link #reason} is {@link PlaybackException#ERROR_CODE_IO_READ_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) { + if (reason == PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE) { return true; } } @@ -62,17 +64,18 @@ public class DataSourceException extends IOException { /** * Indicates that the {@link DataSpec#position starting position} of the request was outside the * bounds of the data. + * + * @deprecated Use {@link PlaybackException#ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE}. */ - public static final int POSITION_OUT_OF_RANGE = 0; - - /** Indicates that the error reason is unknown. */ - public static final int REASON_UNKNOWN = 1; + @Deprecated + public static final int POSITION_OUT_OF_RANGE = + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE; /** - * The reason of this {@link DataSourceException}. It can only be {@link #POSITION_OUT_OF_RANGE}, - * or {@link #REASON_UNKNOWN}. + * The reason of this {@link DataSourceException}, should be one of the {@code ERROR_CODE_IO_*} in + * {@link PlaybackException.ErrorCode}. */ - public final int reason; + @PlaybackException.ErrorCode public final int reason; /** The {@link Type} of the operation that caused the playback failure. */ @Type public final int type; @@ -81,8 +84,7 @@ public class DataSourceException extends IOException { * Constructs a DataSourceException with type {@link #TYPE_READ}. * * @deprecated Use the constructor {@link #DataSourceException(String, Throwable, int, int)}. - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link - * #REASON_UNKNOWN}. + * @param reason Reason of the error. It should only be {@link #POSITION_OUT_OF_RANGE}. */ @Deprecated public DataSourceException(int reason) { @@ -95,11 +97,12 @@ public class DataSourceException extends IOException { * * @param message The error message. * @param cause The error cause. - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link - * #REASON_UNKNOWN}. + * @param reason Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. * @param type See {@link Type}. */ - public DataSourceException(String message, Throwable cause, int reason, @Type int type) { + public DataSourceException( + String message, Throwable cause, @PlaybackException.ErrorCode int reason, @Type int type) { super(message, cause); this.reason = reason; this.type = type; @@ -109,11 +112,12 @@ public class DataSourceException extends IOException { * Constructs a DataSourceException. * * @param cause The error cause. - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link - * #REASON_UNKNOWN}. + * @param reason Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. * @param type See {@link Type}. */ - public DataSourceException(Throwable cause, int reason, @Type int type) { + public DataSourceException( + Throwable cause, @PlaybackException.ErrorCode int reason, @Type int type) { super(cause); this.reason = reason; this.type = type; @@ -123,11 +127,12 @@ public class DataSourceException extends IOException { * Constructs a DataSourceException. * * @param message The error message. - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link - * #REASON_UNKNOWN}. + * @param reason Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. * @param type See {@link Type}. */ - public DataSourceException(String message, int reason, @Type int type) { + public DataSourceException( + String message, @PlaybackException.ErrorCode int reason, @Type int type) { super(message); this.reason = reason; this.type = type; @@ -136,11 +141,11 @@ public class DataSourceException extends IOException { /** * Constructs a DataSourceException. * - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link - * #REASON_UNKNOWN}. + * @param reason Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. * @param type See {@link Type}. */ - public DataSourceException(int reason, @Type int type) { + public DataSourceException(@PlaybackException.ErrorCode int reason, @Type int type) { this.reason = reason; this.type = type; } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java index b4504e7c19..2917f8d67d 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java @@ -24,6 +24,7 @@ import android.net.Uri; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.upstream.DataSpec.HttpMethod; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; @@ -395,7 +396,10 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou new InvalidResponseCodeException( responseCode, responseMessage, headers, dataSpec, errorResponseBody); if (responseCode == 416) { - exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE)); + exception.initCause( + new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ)); } throw exception; } @@ -447,7 +451,9 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } catch (IOException e) { closeConnectionQuietly(); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/upstream/HttpDataSource.java b/library/common/src/main/java/com/google/android/exoplayer2/upstream/HttpDataSource.java index af23520c16..ef128f3255 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/upstream/HttpDataSource.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/upstream/HttpDataSource.java @@ -17,6 +17,7 @@ package com.google.android.exoplayer2.upstream; import android.text.TextUtils; import androidx.annotation.Nullable; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Util; import com.google.common.base.Ascii; import com.google.common.base.Predicate; @@ -190,23 +191,23 @@ public interface HttpDataSource extends DataSource { public final DataSpec dataSpec; public HttpDataSourceException(DataSpec dataSpec, @Type int type) { - super(REASON_UNKNOWN, type); + super(PlaybackException.ERROR_CODE_IO_UNSPECIFIED, type); this.dataSpec = dataSpec; } public HttpDataSourceException(String message, DataSpec dataSpec, @Type int type) { - super(message, REASON_UNKNOWN, type); + super(message, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, type); this.dataSpec = dataSpec; } public HttpDataSourceException(IOException cause, DataSpec dataSpec, @Type int type) { - super(cause, REASON_UNKNOWN, type); + super(cause, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, type); this.dataSpec = dataSpec; } public HttpDataSourceException( String message, IOException cause, DataSpec dataSpec, @Type int type) { - super(message, cause, REASON_UNKNOWN, type); + super(message, cause, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, type); this.dataSpec = dataSpec; } } 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 index 59a4939a68..1296be8d79 100644 --- 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 @@ -18,6 +18,7 @@ package com.google.android.exoplayer2.upstream; import static com.google.common.truth.Truth.assertThat; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.PlaybackException; import java.io.IOException; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,30 +27,38 @@ import org.junit.runner.RunWith; @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); + DataSourceException e = + new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); } @Test public void isCausedByPositionOutOfRange_reasonIsOther_returnsFalse() { - DataSourceException e = new DataSourceException(REASON_OTHER); + DataSourceException e = + new DataSourceException( + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, DataSourceException.TYPE_READ); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse(); } @Test public void isCausedByPositionOutOfRange_indirectauseReasonIsPositionOutOfRange_returnsTrue() { - DataSourceException cause = new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + DataSourceException cause = + new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); IOException e = new IOException(new IOException(cause)); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); } @Test public void isCausedByPositionOutOfRange_causeReasonIsOther_returnsFalse() { - DataSourceException cause = new DataSourceException(REASON_OTHER); + DataSourceException cause = + new DataSourceException( + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, DataSourceException.TYPE_READ); 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/AssetDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java index 8651f85ed7..ec7f160f93 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/AssetDataSource.java @@ -23,6 +23,7 @@ import android.content.res.AssetManager; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Assertions; import java.io.IOException; import java.io.InputStream; @@ -67,7 +68,9 @@ public final class AssetDataSource extends BaseDataSource { if (skipped < dataSpec.position) { // assetManager.open() returns an AssetInputStream, whose skip() implementation only skips // fewer bytes than requested if the skip is beyond the end of the asset's data. - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } if (dataSpec.length != C.LENGTH_UNSET) { bytesRemaining = dataSpec.length; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ByteArrayDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ByteArrayDataSource.java index 331c3affb5..1f19145c78 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ByteArrayDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ByteArrayDataSource.java @@ -20,6 +20,7 @@ import static java.lang.Math.min; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Assertions; import java.io.IOException; @@ -46,7 +47,9 @@ public final class ByteArrayDataSource extends BaseDataSource { uri = dataSpec.uri; transferInitializing(dataSpec); if (dataSpec.position > data.length) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } readPosition = (int) dataSpec.position; bytesRemaining = data.length - (int) dataSpec.position; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java index 21fa3a8031..6ef20976d4 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java @@ -24,6 +24,7 @@ import android.content.res.AssetFileDescriptor; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -79,7 +80,9 @@ public final class ContentDataSource extends BaseDataSource { // file. if (assetFileDescriptorLength != AssetFileDescriptor.UNKNOWN_LENGTH && dataSpec.position > assetFileDescriptorLength) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } long assetFileDescriptorOffset = assetFileDescriptor.getStartOffset(); long skipped = @@ -88,7 +91,9 @@ public final class ContentDataSource extends BaseDataSource { if (skipped != dataSpec.position) { // We expect the skip to be satisfied in full. If it isn't then we're probably trying to // read beyond the end of the last resource in the file. - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) { // The asset must extend to the end of the file. We can try and resolve the length with @@ -101,13 +106,17 @@ public final class ContentDataSource extends BaseDataSource { bytesRemaining = channelSize - channel.position(); if (bytesRemaining < 0) { // The skip above was satisfied in full, but skipped beyond the end of the file. - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } } else { bytesRemaining = assetFileDescriptorLength - skipped; if (bytesRemaining < 0) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } } catch (IOException e) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DataSchemeDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DataSchemeDataSource.java index 134af0c29c..6e5cae7f74 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DataSchemeDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DataSchemeDataSource.java @@ -23,6 +23,7 @@ import android.util.Base64; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ParserException; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import com.google.common.base.Charsets; @@ -69,7 +70,9 @@ public final class DataSchemeDataSource extends BaseDataSource { } if (dataSpec.position > data.length) { data = null; - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } readPosition = (int) dataSpec.position; bytesRemaining = data.length - readPosition; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java index 282ea02dc4..6d759d55da 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/FileDataSource.java @@ -22,6 +22,7 @@ import android.net.Uri; import android.text.TextUtils; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Assertions; import java.io.FileNotFoundException; import java.io.IOException; @@ -90,7 +91,9 @@ public final class FileDataSource extends BaseDataSource { bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position : dataSpec.length; if (bytesRemaining < 0) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } catch (FileDataSourceException e) { throw e; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java index 00ad798bb4..3edb6e021c 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java @@ -26,6 +26,7 @@ import android.net.Uri; import android.text.TextUtils; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Assertions; import java.io.EOFException; import java.io.FileInputStream; @@ -156,7 +157,9 @@ public final class RawResourceDataSource extends BaseDataSource { // extends to the end of the file. if (assetFileDescriptorLength != AssetFileDescriptor.UNKNOWN_LENGTH && dataSpec.position > assetFileDescriptorLength) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } long assetFileDescriptorOffset = assetFileDescriptor.getStartOffset(); long skipped = @@ -165,7 +168,9 @@ public final class RawResourceDataSource extends BaseDataSource { if (skipped != dataSpec.position) { // We expect the skip to be satisfied in full. If it isn't then we're probably trying to // read beyond the end of the last resource in the file. - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) { // The asset must extend to the end of the file. We can try and resolve the length with @@ -177,13 +182,17 @@ public final class RawResourceDataSource extends BaseDataSource { bytesRemaining = channel.size() - channel.position(); if (bytesRemaining < 0) { // The skip above was satisfied in full, but skipped beyond the end of the file. - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } } else { bytesRemaining = assetFileDescriptorLength - skipped; if (bytesRemaining < 0) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } } catch (IOException e) { 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 e26dfd5c67..c7dec428fa 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 @@ -23,6 +23,7 @@ import android.net.Uri; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.upstream.DataSink; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSourceException; @@ -573,7 +574,9 @@ public final class CacheDataSource implements DataSource { if (bytesRemaining != C.LENGTH_UNSET) { bytesRemaining -= dataSpec.position; if (bytesRemaining < 0) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java index 1894dca414..297ae69e0b 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/DataSchemeDataSourceTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.fail; import android.net.Uri; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.util.Util; import java.io.IOException; import org.junit.Before; @@ -103,7 +104,7 @@ public final class DataSchemeDataSourceTest { buildDataSpec(DATA_SCHEME_URI, /* position= */ 108, /* length= */ C.LENGTH_UNSET)); fail(); } catch (DataSourceException e) { - assertThat(e.reason).isEqualTo(DataSourceException.POSITION_OUT_OF_RANGE); + assertThat(e.reason).isEqualTo(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java index 0ae6627e8e..78f9f554e1 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java @@ -21,6 +21,7 @@ import static java.lang.Math.min; import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.testutil.FakeDataSet.FakeData; import com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment; import com.google.android.exoplayer2.upstream.BaseDataSource; @@ -116,7 +117,9 @@ public class FakeDataSource extends BaseDataSource { } if (dataSpec.position > totalLength) { - throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE); + throw new DataSourceException( + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, + DataSourceException.TYPE_READ); } // Scan through the segments, configuring them for the current read.