From dda1d37368a825bc74ea14dbdb6b1b35d961bbb1 Mon Sep 17 00:00:00 2001 From: claincly Date: Thu, 1 Jul 2021 18:21:47 +0100 Subject: [PATCH] Make HttpDataSourceException subclass DataSourceException. PiperOrigin-RevId: 382551642 --- .../upstream/DataSourceException.java | 94 ++++++++++++++++++- .../exoplayer2/upstream/HttpDataSource.java | 29 +----- 2 files changed, 95 insertions(+), 28 deletions(-) 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 c3ccdb88d9..a4932fc283 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,11 +15,31 @@ */ package com.google.android.exoplayer2.upstream; +import androidx.annotation.IntDef; import androidx.annotation.Nullable; 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 final class DataSourceException extends IOException { +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 @@ -45,17 +65,83 @@ public final class DataSourceException extends IOException { */ public static final int POSITION_OUT_OF_RANGE = 0; + /** Indicates that the error reason is unknown. */ + public static final int REASON_UNKNOWN = 1; + /** - * The reason of this {@link DataSourceException}. It can only be {@link #POSITION_OUT_OF_RANGE}. + * The reason of this {@link DataSourceException}. It can only be {@link #POSITION_OUT_OF_RANGE}, + * or {@link #REASON_UNKNOWN}. */ 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}. + * + * @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}. + */ + @Deprecated + public DataSourceException(int reason) { + this.reason = reason; + this.type = TYPE_READ; + } + /** * Constructs a DataSourceException. * - * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE}. + * @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 type See {@link Type}. */ - public DataSourceException(int reason) { + public DataSourceException(String message, Throwable cause, int reason, @Type int type) { + super(message, cause); this.reason = reason; + this.type = type; + } + + /** + * 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 type See {@link Type}. + */ + public DataSourceException(Throwable cause, int reason, @Type int type) { + super(cause); + this.reason = reason; + this.type = type; + } + + /** + * 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 type See {@link Type}. + */ + public DataSourceException(String message, int reason, @Type int type) { + super(message); + this.reason = reason; + this.type = type; + } + + /** + * Constructs a DataSourceException. + * + * @param reason Reason of the error. It can only be {@link #POSITION_OUT_OF_RANGE} or {@link + * #REASON_UNKNOWN}. + * @param type See {@link Type}. + */ + public DataSourceException(int reason, @Type int type) { + this.reason = reason; + this.type = type; } } 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 27b195a794..af23520c16 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,15 +16,11 @@ package com.google.android.exoplayer2.upstream; import android.text.TextUtils; -import androidx.annotation.IntDef; import androidx.annotation.Nullable; 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; @@ -188,45 +184,30 @@ public interface HttpDataSource extends DataSource { }; /** Thrown when an error is encountered when trying to read from a {@link HttpDataSource}. */ - class HttpDataSourceException extends IOException { - - @Documented - @Retention(RetentionPolicy.SOURCE) - @IntDef({TYPE_OPEN, TYPE_READ, TYPE_CLOSE}) - public @interface Type {} - - public static final int TYPE_OPEN = 1; - public static final int TYPE_READ = 2; - public static final int TYPE_CLOSE = 3; - - @Type public final int type; + class HttpDataSourceException extends DataSourceException { /** The {@link DataSpec} associated with the current connection. */ public final DataSpec dataSpec; public HttpDataSourceException(DataSpec dataSpec, @Type int type) { - super(); + super(REASON_UNKNOWN, type); this.dataSpec = dataSpec; - this.type = type; } public HttpDataSourceException(String message, DataSpec dataSpec, @Type int type) { - super(message); + super(message, REASON_UNKNOWN, type); this.dataSpec = dataSpec; - this.type = type; } public HttpDataSourceException(IOException cause, DataSpec dataSpec, @Type int type) { - super(cause); + super(cause, REASON_UNKNOWN, type); this.dataSpec = dataSpec; - this.type = type; } public HttpDataSourceException( String message, IOException cause, DataSpec dataSpec, @Type int type) { - super(message, cause); + super(message, cause, REASON_UNKNOWN, type); this.dataSpec = dataSpec; - this.type = type; } }