Restrict Decoder generic exception type to extend DecoderException

This is useful for merging the FFmpeg video support pull request,
since it allows a Decoder base class implementation to catch
DecoderException rather than forcing it to catch Exception (i.e.,
everything). Note that unfortunately, Java doesn't allow catching
of a generic type (i.e., you cannot "catch (E e)") due to type
erasure.

PiperOrigin-RevId: 311300719
This commit is contained in:
olly 2020-05-13 11:58:56 +01:00 committed by Oliver Woodman
parent f599155bbf
commit a5824f7325
4 changed files with 32 additions and 16 deletions

View File

@ -24,7 +24,7 @@ import androidx.annotation.Nullable;
* @param <O> The type of buffer output from the decoder. * @param <O> The type of buffer output from the decoder.
* @param <E> The type of exception thrown from the decoder. * @param <E> The type of exception thrown from the decoder.
*/ */
public interface Decoder<I, O, E extends Exception> { public interface Decoder<I, O, E extends DecoderException> {
/** /**
* Returns the name of the decoder. * Returns the name of the decoder.

View File

@ -15,20 +15,36 @@
*/ */
package com.google.android.exoplayer2.decoder; package com.google.android.exoplayer2.decoder;
import androidx.annotation.Nullable;
/** Thrown when a {@link Decoder} error occurs. */ /** Thrown when a {@link Decoder} error occurs. */
public class DecoderException extends Exception { public class DecoderException extends Exception {
/** @param message The detail message for this exception. */ /**
* Creates an instance.
*
* @param message The detail message for this exception.
*/
public DecoderException(String message) { public DecoderException(String message) {
super(message); super(message);
} }
/** /**
* @param message The detail message for this exception. * Creates an instance.
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). *
* A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown. * @param cause The cause of this exception, or {@code null}.
*/ */
public DecoderException(String message, Throwable cause) { public DecoderException(@Nullable Throwable cause) {
super(cause);
}
/**
* Creates an instance.
*
* @param message The detail message for this exception.
* @param cause The cause of this exception, or {@code null}.
*/
public DecoderException(String message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
} }
} }

View File

@ -27,7 +27,7 @@ import java.util.ArrayDeque;
*/ */
@SuppressWarnings("UngroupedOverloads") @SuppressWarnings("UngroupedOverloads")
public abstract class SimpleDecoder< public abstract class SimpleDecoder<
I extends DecoderInputBuffer, O extends OutputBuffer, E extends Exception> I extends DecoderInputBuffer, O extends OutputBuffer, E extends DecoderException>
implements Decoder<I, O, E> { implements Decoder<I, O, E> {
private final Thread decodeThread; private final Thread decodeThread;

View File

@ -15,10 +15,11 @@
*/ */
package com.google.android.exoplayer2.text; package com.google.android.exoplayer2.text;
/** import androidx.annotation.Nullable;
* Thrown when an error occurs decoding subtitle data. import com.google.android.exoplayer2.decoder.DecoderException;
*/
public class SubtitleDecoderException extends Exception { /** Thrown when an error occurs decoding subtitle data. */
public class SubtitleDecoderException extends DecoderException {
/** /**
* @param message The detail message for this exception. * @param message The detail message for this exception.
@ -27,17 +28,16 @@ public class SubtitleDecoderException extends Exception {
super(message); super(message);
} }
/** @param cause The cause of this exception. */ /** @param cause The cause of this exception, or {@code null}. */
public SubtitleDecoderException(Exception cause) { public SubtitleDecoderException(@Nullable Throwable cause) {
super(cause); super(cause);
} }
/** /**
* @param message The detail message for this exception. * @param message The detail message for this exception.
* @param cause The cause of this exception. * @param cause The cause of this exception, or {@code null}.
*/ */
public SubtitleDecoderException(String message, Throwable cause) { public SubtitleDecoderException(String message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
} }
} }