From ca28d6a041eeda9d002d480c1fed83d0e77fc40b Mon Sep 17 00:00:00 2001 From: claincly Date: Mon, 12 Jul 2021 14:25:12 +0100 Subject: [PATCH] Move TYPE definition out from DataSourceException. The types (open/close/read) does not provide extra information about the thrown playback exception, and they are not utilized at higher levels. PiperOrigin-RevId: 384219870 --- .../ext/cronet/CronetDataSource.java | 22 +++--- .../ext/okhttp/OkHttpDataSource.java | 32 ++++++--- .../upstream/DataSourceException.java | 59 ++------------- .../upstream/DefaultHttpDataSource.java | 44 ++++++++---- .../exoplayer2/upstream/HttpDataSource.java | 34 +++++++-- .../upstream/DataSourceExceptionTest.java | 17 ++--- .../exoplayer2/upstream/AssetDataSource.java | 26 ++++--- .../upstream/ByteArrayDataSource.java | 4 +- .../upstream/ContentDataSource.java | 40 ++++++----- .../upstream/DataSchemeDataSource.java | 4 +- .../exoplayer2/upstream/FileDataSource.java | 4 +- .../upstream/RawResourceDataSource.java | 72 ++++++++++++------- .../upstream/cache/CacheDataSource.java | 3 +- .../exoplayer2/testutil/FakeDataSource.java | 4 +- 14 files changed, 201 insertions(+), 164 deletions(-) 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 0278a888ff..06bb078fee 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 @@ -351,12 +351,12 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { public final int cronetConnectionStatus; public OpenException(IOException cause, DataSpec dataSpec, int cronetConnectionStatus) { - super(cause, dataSpec, TYPE_OPEN); + super(cause, dataSpec, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, TYPE_OPEN); this.cronetConnectionStatus = cronetConnectionStatus; } public OpenException(String errorMessage, DataSpec dataSpec, int cronetConnectionStatus) { - super(errorMessage, dataSpec, TYPE_OPEN); + super(errorMessage, dataSpec, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, TYPE_OPEN); this.cronetConnectionStatus = cronetConnectionStatus; } } @@ -677,9 +677,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { responseBody); if (responseCode == 416) { exception.initCause( - new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ)); + new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE)); } throw exception; } @@ -721,9 +719,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } catch (IOException e) { throw new OpenException(e, dataSpec, Status.READING_RESPONSE); @@ -751,7 +747,10 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { readInternal(readBuffer); } catch (IOException e) { throw new HttpDataSourceException( - e, castNonNull(currentDataSpec), HttpDataSourceException.TYPE_READ); + e, + castNonNull(currentDataSpec), + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_READ); } if (finished) { @@ -838,7 +837,10 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { readInternal(buffer); } catch (IOException e) { throw new HttpDataSourceException( - e, castNonNull(currentDataSpec), HttpDataSourceException.TYPE_READ); + e, + castNonNull(currentDataSpec), + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_READ); } if (finished) { 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 ab7cb29e3b..bd1a936776 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 @@ -295,7 +295,11 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { throw new CleartextNotPermittedException(e, dataSpec); } throw new HttpDataSourceException( - "Unable to connect", e, dataSpec, HttpDataSourceException.TYPE_OPEN); + "Unable to connect", + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, + HttpDataSourceException.TYPE_OPEN); } int responseCode = response.code(); @@ -325,9 +329,7 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { responseCode, response.message(), headers, dataSpec, errorResponseBody); if (responseCode == 416) { exception.initCause( - new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ)); + new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE)); } throw exception; } @@ -358,13 +360,15 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } catch (IOException e) { closeConnectionQuietly(); - throw new HttpDataSourceException(e, dataSpec, HttpDataSourceException.TYPE_OPEN); + throw new HttpDataSourceException( + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_OPEN); } return bytesToRead; @@ -376,12 +380,15 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { return readInternal(buffer, offset, readLength); } catch (IOException e) { throw new HttpDataSourceException( - e, Assertions.checkNotNull(dataSpec), HttpDataSourceException.TYPE_READ); + e, + Assertions.checkNotNull(dataSpec), + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_READ); } } @Override - public void close() throws HttpDataSourceException { + public void close() { if (opened) { opened = false; transferEnded(); @@ -397,7 +404,10 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource { @Nullable HttpUrl url = HttpUrl.parse(dataSpec.uri.toString()); if (url == null) { throw new HttpDataSourceException( - "Malformed URL", dataSpec, HttpDataSourceException.TYPE_OPEN); + "Malformed URL", + dataSpec, + PlaybackException.ERROR_CODE_IO_BAD_HTTP_REQUEST, + HttpDataSourceException.TYPE_OPEN); } Request.Builder builder = new Request.Builder().url(url); 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 1a9392d90c..bd4f9fba22 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,33 +15,13 @@ */ 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; -import java.lang.annotation.RetentionPolicy; /** Used to specify reason of a DataSource error. */ public class DataSourceException extends IOException { - /** - * The type of operation that produced the error. One of {@link #TYPE_READ}, {@link #TYPE_OPEN} - * {@link #TYPE_CLOSE}. - */ - @Documented - @Retention(RetentionPolicy.SOURCE) - @IntDef({TYPE_OPEN, TYPE_READ, TYPE_CLOSE}) - public @interface Type {} - - /** The error occurred reading data from a {@link DataSource}. */ - public static final int TYPE_OPEN = 1; - /** The error occurred in opening a {@link DataSource}. */ - public static final int TYPE_READ = 2; - /** The error occurred in closing a {@link DataSource}. */ - public static final int TYPE_CLOSE = 3; - /** * Returns whether the given {@link IOException} was caused by a {@link DataSourceException} whose * {@link #reason} is {@link PlaybackException#ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE} in its @@ -77,19 +57,14 @@ public class DataSourceException extends IOException { */ @PlaybackException.ErrorCode public final int reason; - /** The {@link Type} of the operation that caused the playback failure. */ - @Type public final int type; - /** - * Constructs a DataSourceException with type {@link #TYPE_READ}. + * Constructs a DataSourceException. * - * @deprecated Use the constructor {@link #DataSourceException(String, Throwable, int, int)}. - * @param reason Reason of the error. It should only be {@link #POSITION_OUT_OF_RANGE}. + * @param reason Reason of the error, should be one of the {@code ERROR_CODE_IO_*} in {@link + * PlaybackException.ErrorCode}. */ - @Deprecated - public DataSourceException(int reason) { + public DataSourceException(@PlaybackException.ErrorCode int reason) { this.reason = reason; - this.type = TYPE_READ; } /** @@ -99,13 +74,11 @@ public class DataSourceException extends IOException { * @param cause The error cause. * @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, @PlaybackException.ErrorCode int reason, @Type int type) { + String message, Throwable cause, @PlaybackException.ErrorCode int reason) { super(message, cause); this.reason = reason; - this.type = type; } /** @@ -114,13 +87,10 @@ public class DataSourceException extends IOException { * @param cause The error cause. * @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, @PlaybackException.ErrorCode int reason, @Type int type) { + public DataSourceException(Throwable cause, @PlaybackException.ErrorCode int reason) { super(cause); this.reason = reason; - this.type = type; } /** @@ -129,24 +99,9 @@ public class DataSourceException extends IOException { * @param message The error message. * @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, @PlaybackException.ErrorCode int reason, @Type int type) { + public DataSourceException(String message, @PlaybackException.ErrorCode int reason) { super(message); this.reason = reason; - this.type = type; - } - - /** - * Constructs a DataSourceException. - * - * @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(@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 2917f8d67d..421937e3ce 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 @@ -356,7 +356,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou throw new CleartextNotPermittedException(e, dataSpec); } throw new HttpDataSourceException( - "Unable to connect", e, dataSpec, HttpDataSourceException.TYPE_OPEN); + "Unable to connect", + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, + HttpDataSourceException.TYPE_OPEN); } HttpURLConnection connection = this.connection; @@ -367,7 +371,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou } catch (IOException e) { closeConnectionQuietly(); throw new HttpDataSourceException( - "Unable to connect", e, dataSpec, HttpDataSourceException.TYPE_OPEN); + "Unable to connect", + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, + HttpDataSourceException.TYPE_OPEN); } // Check for a valid response code. @@ -397,9 +405,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou responseCode, responseMessage, headers, dataSpec, errorResponseBody); if (responseCode == 416) { exception.initCause( - new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ)); + new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE)); } throw exception; } @@ -443,7 +449,11 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou } } catch (IOException e) { closeConnectionQuietly(); - throw new HttpDataSourceException(e, dataSpec, HttpDataSourceException.TYPE_OPEN); + throw new HttpDataSourceException( + e, + dataSpec, + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_OPEN); } opened = true; @@ -451,13 +461,17 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou try { if (!skipFully(bytesToSkip)) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } catch (IOException e) { closeConnectionQuietly(); - throw new HttpDataSourceException(e, dataSpec, HttpDataSourceException.TYPE_OPEN); + + @PlaybackException.ErrorCode int errorCode = PlaybackException.ERROR_CODE_IO_UNSPECIFIED; + if (e instanceof DataSourceException) { + errorCode = ((DataSourceException) e).reason; + } + + throw new HttpDataSourceException(e, dataSpec, errorCode, HttpDataSourceException.TYPE_OPEN); } return bytesToRead; @@ -469,7 +483,10 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou return readInternal(buffer, offset, readLength); } catch (IOException e) { throw new HttpDataSourceException( - e, castNonNull(dataSpec), HttpDataSourceException.TYPE_READ); + e, + castNonNull(dataSpec), + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_READ); } } @@ -485,7 +502,10 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou inputStream.close(); } catch (IOException e) { throw new HttpDataSourceException( - e, castNonNull(dataSpec), HttpDataSourceException.TYPE_CLOSE); + e, + castNonNull(dataSpec), + PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + HttpDataSourceException.TYPE_CLOSE); } } } finally { 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 87c82316ea..57a17e022a 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 @@ -16,12 +16,16 @@ package com.google.android.exoplayer2.upstream; import android.text.TextUtils; +import androidx.annotation.IntDef; 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; import java.io.IOException; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -187,9 +191,27 @@ public interface HttpDataSource extends DataSource { /** Thrown when an error is encountered when trying to read from a {@link HttpDataSource}. */ class HttpDataSourceException extends DataSourceException { + /** + * The type of operation that produced the error. One of {@link #TYPE_READ}, {@link #TYPE_OPEN} + * {@link #TYPE_CLOSE}. + */ + @Documented + @Retention(RetentionPolicy.SOURCE) + @IntDef({TYPE_OPEN, TYPE_READ, TYPE_CLOSE}) + public @interface Type {} + + /** The error occurred reading data from a {@code HttpDataSource}. */ + public static final int TYPE_OPEN = 1; + /** The error occurred in opening a {@code HttpDataSource}. */ + public static final int TYPE_READ = 2; + /** The error occurred in closing a {@code HttpDataSource}. */ + public static final int TYPE_CLOSE = 3; + /** The {@link DataSpec} associated with the current connection. */ public final DataSpec dataSpec; + @Type public final int type; + /** * @deprecated Use {@link #HttpDataSourceException(DataSpec, int, int) * HttpDataSourceException(DataSpec, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, int)}. @@ -209,8 +231,9 @@ public interface HttpDataSource extends DataSource { */ public HttpDataSourceException( DataSpec dataSpec, @PlaybackException.ErrorCode int errorCode, @Type int type) { - super(errorCode, type); + super(errorCode); this.dataSpec = dataSpec; + this.type = type; } /** @@ -237,8 +260,9 @@ public interface HttpDataSource extends DataSource { DataSpec dataSpec, @PlaybackException.ErrorCode int errorCode, @Type int type) { - super(message, errorCode, type); + super(message, errorCode); this.dataSpec = dataSpec; + this.type = type; } /** @@ -265,8 +289,9 @@ public interface HttpDataSource extends DataSource { DataSpec dataSpec, @PlaybackException.ErrorCode int errorCode, @Type int type) { - super(cause, errorCode, type); + super(cause, errorCode); this.dataSpec = dataSpec; + this.type = type; } /** @@ -296,8 +321,9 @@ public interface HttpDataSource extends DataSource { DataSpec dataSpec, @PlaybackException.ErrorCode int errorCode, @Type int type) { - super(message, cause, errorCode, type); + super(message, cause, errorCode); this.dataSpec = dataSpec; + this.type = type; } } 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 1296be8d79..432333f09f 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 @@ -30,26 +30,20 @@ public class DataSourceExceptionTest { @Test public void isCausedByPositionOutOfRange_reasonIsPositionOutOfRange_returnsTrue() { DataSourceException e = - new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); } @Test public void isCausedByPositionOutOfRange_reasonIsOther_returnsFalse() { - DataSourceException e = - new DataSourceException( - PlaybackException.ERROR_CODE_IO_UNSPECIFIED, DataSourceException.TYPE_READ); + DataSourceException e = new DataSourceException(PlaybackException.ERROR_CODE_IO_UNSPECIFIED); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse(); } @Test - public void isCausedByPositionOutOfRange_indirectauseReasonIsPositionOutOfRange_returnsTrue() { + public void isCausedByPositionOutOfRange_indirectCauseReasonIsPositionOutOfRange_returnsTrue() { DataSourceException cause = - new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); IOException e = new IOException(new IOException(cause)); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isTrue(); } @@ -57,8 +51,7 @@ public class DataSourceExceptionTest { @Test public void isCausedByPositionOutOfRange_causeReasonIsOther_returnsFalse() { DataSourceException cause = - new DataSourceException( - PlaybackException.ERROR_CODE_IO_UNSPECIFIED, DataSourceException.TYPE_READ); + new DataSourceException(PlaybackException.ERROR_CODE_IO_UNSPECIFIED); 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 ec7f160f93..24df5f8132 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 @@ -32,10 +32,22 @@ import java.io.InputStream; public final class AssetDataSource extends BaseDataSource { /** Thrown when an {@link IOException} is encountered reading a local asset. */ - public static final class AssetDataSourceException extends IOException { + public static final class AssetDataSourceException extends DataSourceException { + /** @deprecated Use {@link #AssetDataSourceException(Throwable, int)}. */ + @Deprecated public AssetDataSourceException(IOException cause) { - super(cause); + super(cause, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); + } + + /** + * Creates a new instance. + * + * @param cause The error cause. + * @param errorCode See {@link PlaybackException.ErrorCode}. + */ + public AssetDataSourceException(Throwable cause, @PlaybackException.ErrorCode int errorCode) { + super(cause, errorCode); } } @@ -68,9 +80,7 @@ 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( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } if (dataSpec.length != C.LENGTH_UNSET) { bytesRemaining = dataSpec.length; @@ -84,7 +94,7 @@ public final class AssetDataSource extends BaseDataSource { } } } catch (IOException e) { - throw new AssetDataSourceException(e); + throw new AssetDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } opened = true; @@ -106,7 +116,7 @@ public final class AssetDataSource extends BaseDataSource { bytesRemaining == C.LENGTH_UNSET ? readLength : (int) min(bytesRemaining, readLength); bytesRead = castNonNull(inputStream).read(buffer, offset, bytesToRead); } catch (IOException e) { - throw new AssetDataSourceException(e); + throw new AssetDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } if (bytesRead == -1) { @@ -133,7 +143,7 @@ public final class AssetDataSource extends BaseDataSource { inputStream.close(); } } catch (IOException e) { - throw new AssetDataSourceException(e); + throw new AssetDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } finally { inputStream = null; if (opened) { 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 1f19145c78..68f87cdd2d 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 @@ -47,9 +47,7 @@ public final class ByteArrayDataSource extends BaseDataSource { uri = dataSpec.uri; transferInitializing(dataSpec); if (dataSpec.position > data.length) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } 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 6ef20976d4..ca7158fa63 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 @@ -34,10 +34,23 @@ import java.nio.channels.FileChannel; public final class ContentDataSource extends BaseDataSource { /** Thrown when an {@link IOException} is encountered reading from a content URI. */ - public static class ContentDataSourceException extends IOException { + public static class ContentDataSourceException extends DataSourceException { + /** @deprecated Use {@link #ContentDataSourceException(IOException, int)}. */ + @Deprecated public ContentDataSourceException(IOException cause) { - super(cause); + super(cause, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); + } + + /** + * Creates a new instance. + * + * @param cause The error cause. + * @param errorCode See {@link PlaybackException.ErrorCode}. + */ + public ContentDataSourceException( + IOException cause, @PlaybackException.ErrorCode int errorCode) { + super(cause, errorCode); } } @@ -80,9 +93,7 @@ public final class ContentDataSource extends BaseDataSource { // file. if (assetFileDescriptorLength != AssetFileDescriptor.UNKNOWN_LENGTH && dataSpec.position > assetFileDescriptorLength) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } long assetFileDescriptorOffset = assetFileDescriptor.getStartOffset(); long skipped = @@ -91,9 +102,7 @@ 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( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) { // The asset must extend to the end of the file. We can try and resolve the length with @@ -107,20 +116,17 @@ public final class ContentDataSource extends BaseDataSource { if (bytesRemaining < 0) { // The skip above was satisfied in full, but skipped beyond the end of the file. throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } } else { bytesRemaining = assetFileDescriptorLength - skipped; if (bytesRemaining < 0) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } } catch (IOException e) { - throw new ContentDataSourceException(e); + throw new ContentDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } if (dataSpec.length != C.LENGTH_UNSET) { @@ -146,7 +152,7 @@ public final class ContentDataSource extends BaseDataSource { bytesRemaining == C.LENGTH_UNSET ? readLength : (int) min(bytesRemaining, readLength); bytesRead = castNonNull(inputStream).read(buffer, offset, bytesToRead); } catch (IOException e) { - throw new ContentDataSourceException(e); + throw new ContentDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } if (bytesRead == -1) { @@ -174,7 +180,7 @@ public final class ContentDataSource extends BaseDataSource { inputStream.close(); } } catch (IOException e) { - throw new ContentDataSourceException(e); + throw new ContentDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } finally { inputStream = null; try { @@ -182,7 +188,7 @@ public final class ContentDataSource extends BaseDataSource { assetFileDescriptor.close(); } } catch (IOException e) { - throw new ContentDataSourceException(e); + throw new ContentDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } finally { assetFileDescriptor = null; if (opened) { 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 6e5cae7f74..f8ced1378c 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 @@ -70,9 +70,7 @@ public final class DataSchemeDataSource extends BaseDataSource { } if (dataSpec.position > data.length) { data = null; - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } 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 6d759d55da..0d78c100c5 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 @@ -91,9 +91,7 @@ public final class FileDataSource extends BaseDataSource { bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position : dataSpec.length; if (bytesRemaining < 0) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } 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 3edb6e021c..3cd79f6d72 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 @@ -55,13 +55,38 @@ import java.nio.channels.FileChannel; public final class RawResourceDataSource extends BaseDataSource { /** Thrown when an {@link IOException} is encountered reading from a raw resource. */ - public static class RawResourceDataSourceException extends IOException { + public static class RawResourceDataSourceException extends DataSourceException { + /** @deprecated Use {@link #RawResourceDataSourceException(String, int)}. */ + @Deprecated public RawResourceDataSourceException(String message) { - super(message); + super(message, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } + /** + * Creates a new instance. + * + * @param message The error message. + * @param errorCode See {@link PlaybackException.ErrorCode}. + */ + public RawResourceDataSourceException( + String message, @PlaybackException.ErrorCode int errorCode) { + super(message, errorCode); + } + + /** @deprecated Use {@link #RawResourceDataSourceException(Throwable, int)}. */ + @Deprecated public RawResourceDataSourceException(Throwable e) { - super(e); + super(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); + } + + /** + * Creates a new instance. + * + * @param e The error cause. + * @param errorCode See {@link PlaybackException.ErrorCode}. + */ + public RawResourceDataSourceException(Throwable e, @PlaybackException.ErrorCode int errorCode) { + super(e, errorCode); } } @@ -107,7 +132,9 @@ public final class RawResourceDataSource extends BaseDataSource { try { resourceId = Integer.parseInt(Assertions.checkNotNull(uri.getLastPathSegment())); } catch (NumberFormatException e) { - throw new RawResourceDataSourceException("Resource identifier must be an integer."); + throw new RawResourceDataSourceException( + "Resource identifier must be an integer.", + PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND); } } else if (TextUtils.equals(ContentResolver.SCHEME_ANDROID_RESOURCE, uri.getScheme())) { String path = Assertions.checkNotNull(uri.getPath()); @@ -120,14 +147,16 @@ public final class RawResourceDataSource extends BaseDataSource { resources.getIdentifier( resourceName, /* defType= */ "raw", /* defPackage= */ packageName); if (resourceId == 0) { - throw new RawResourceDataSourceException("Resource not found."); + throw new RawResourceDataSourceException( + "Resource not found.", PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND); } } else { throw new RawResourceDataSourceException( "URI must either use scheme " + RAW_RESOURCE_SCHEME + " or " - + ContentResolver.SCHEME_ANDROID_RESOURCE); + + ContentResolver.SCHEME_ANDROID_RESOURCE, + PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND); } transferInitializing(dataSpec); @@ -136,12 +165,13 @@ public final class RawResourceDataSource extends BaseDataSource { try { assetFileDescriptor = resources.openRawResourceFd(resourceId); } catch (Resources.NotFoundException e) { - throw new RawResourceDataSourceException(e); + throw new RawResourceDataSourceException(e, PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND); } this.assetFileDescriptor = assetFileDescriptor; if (assetFileDescriptor == null) { - throw new RawResourceDataSourceException("Resource is compressed: " + uri); + throw new RawResourceDataSourceException( + "Resource is compressed: " + uri, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } long assetFileDescriptorLength = assetFileDescriptor.getLength(); @@ -157,9 +187,7 @@ public final class RawResourceDataSource extends BaseDataSource { // extends to the end of the file. if (assetFileDescriptorLength != AssetFileDescriptor.UNKNOWN_LENGTH && dataSpec.position > assetFileDescriptorLength) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } long assetFileDescriptorOffset = assetFileDescriptor.getStartOffset(); long skipped = @@ -168,9 +196,7 @@ 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( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) { // The asset must extend to the end of the file. We can try and resolve the length with @@ -183,20 +209,17 @@ public final class RawResourceDataSource extends BaseDataSource { if (bytesRemaining < 0) { // The skip above was satisfied in full, but skipped beyond the end of the file. throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } } else { bytesRemaining = assetFileDescriptorLength - skipped; if (bytesRemaining < 0) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } } } catch (IOException e) { - throw new RawResourceDataSourceException(e); + throw new RawResourceDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } if (dataSpec.length != C.LENGTH_UNSET) { @@ -222,13 +245,14 @@ public final class RawResourceDataSource extends BaseDataSource { bytesRemaining == C.LENGTH_UNSET ? readLength : (int) min(bytesRemaining, readLength); bytesRead = castNonNull(inputStream).read(buffer, offset, bytesToRead); } catch (IOException e) { - throw new RawResourceDataSourceException(e); + throw new RawResourceDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } if (bytesRead == -1) { if (bytesRemaining != C.LENGTH_UNSET) { // End of stream reached having not read sufficient data. - throw new RawResourceDataSourceException(new EOFException()); + throw new RawResourceDataSourceException( + new EOFException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } return C.RESULT_END_OF_INPUT; } @@ -254,7 +278,7 @@ public final class RawResourceDataSource extends BaseDataSource { inputStream.close(); } } catch (IOException e) { - throw new RawResourceDataSourceException(e); + throw new RawResourceDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } finally { inputStream = null; try { @@ -262,7 +286,7 @@ public final class RawResourceDataSource extends BaseDataSource { assetFileDescriptor.close(); } } catch (IOException e) { - throw new RawResourceDataSourceException(e); + throw new RawResourceDataSourceException(e, PlaybackException.ERROR_CODE_IO_UNSPECIFIED); } finally { assetFileDescriptor = null; if (opened) { 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 c7dec428fa..44755b0c8b 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 @@ -575,8 +575,7 @@ public final class CacheDataSource implements DataSource { bytesRemaining -= dataSpec.position; if (bytesRemaining < 0) { throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + 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 78f9f554e1..0d14b3ead1 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 @@ -117,9 +117,7 @@ public class FakeDataSource extends BaseDataSource { } if (dataSpec.position > totalLength) { - throw new DataSourceException( - PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE, - DataSourceException.TYPE_READ); + throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE); } // Scan through the segments, configuring them for the current read.