From 3f33f10a32b734316996bb324faee2e6009ce32a Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Fri, 21 May 2021 16:02:29 +0100 Subject: [PATCH] Add ExtractorUtil method for checking input conditions Unlike Assertions, the introduced method cannot be disabled and throws a ParserException instead. This method is meant to replace regular assertions (which throw RuntimeExceptions) which check input in the parsing code. PiperOrigin-RevId: 375085160 --- .../exoplayer2/extractor/ExtractorUtil.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ExtractorUtil.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ExtractorUtil.java index 5ce274b11a..164c991597 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ExtractorUtil.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/ExtractorUtil.java @@ -15,11 +15,26 @@ */ package com.google.android.exoplayer2.extractor; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.ParserException; import java.io.IOException; +import org.checkerframework.dataflow.qual.Pure; /** Extractor related utility methods. */ -/* package */ final class ExtractorUtil { +public final class ExtractorUtil { + + /** + * If {@code expression} is false, throws a {@link ParserException#createForMalformedContainer + * container malformed ParserException} with the given message. Otherwise, does nothing. + */ + @Pure + public static void checkContainerInput(boolean expression, @Nullable String message) + throws ParserException { + if (!expression) { + throw ParserException.createForMalformedContainer(message, /* cause= */ null); + } + } /** * Peeks {@code length} bytes from the input peek position, or all the bytes to the end of the