From de0e1e5168461debf602dc0e936c46502b708c8a Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 11 Jan 2021 20:59:29 +0000 Subject: [PATCH] Throw specific exception if input buffer is too small This potentially allows a caller to resize their buffers to take into account the required size. It's kept as an IllegalStateException, since most use cases where it's thrown still reflect invalid states, and since making it a checked exception requires marking a lot of methods with throws clauses. PiperOrigin-RevId: 351216968 --- .../decoder/DecoderInputBuffer.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java b/library/common/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java index 0ae8ce31f9..4d00ee7748 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java @@ -29,6 +29,31 @@ import org.checkerframework.checker.nullness.qual.EnsuresNonNull; */ public class DecoderInputBuffer extends Buffer { + /** + * Thrown when an attempt is made to write into a {@link DecoderInputBuffer} whose {@link + * #bufferReplacementMode} is {@link #BUFFER_REPLACEMENT_MODE_DISABLED} and who {@link #data} + * capacity is smaller than required. + */ + public static final class InsufficientCapacityException extends IllegalStateException { + + /** The current capacity of the buffer. */ + public final int currentCapacity; + /** The required capacity of the buffer. */ + public final int requiredCapacity; + + /** + * Creates an instance. + * + * @param currentCapacity The current capacity of the buffer. + * @param requiredCapacity The required capacity of the buffer. + */ + public InsufficientCapacityException(int currentCapacity, int requiredCapacity) { + super("Buffer too small (" + currentCapacity + " < " + requiredCapacity + ")"); + this.currentCapacity = currentCapacity; + this.requiredCapacity = requiredCapacity; + } + } + /** * The buffer replacement mode. This controls how {@link #ensureSpaceForWrite} generates * replacement buffers when the capacity of the existing buffer is insufficient. One of {@link @@ -144,8 +169,8 @@ public class DecoderInputBuffer extends Buffer { * whose capacity is sufficient. Data up to the current position is copied to the new buffer. * * @param length The length of the write that must be accommodated, in bytes. - * @throws IllegalStateException If there is insufficient capacity to accommodate the write and - * the buffer replacement mode of the holder is {@link #BUFFER_REPLACEMENT_MODE_DISABLED}. + * @throws InsufficientCapacityException If there is insufficient capacity to accommodate the + * write and {@link #bufferReplacementMode} is {@link #BUFFER_REPLACEMENT_MODE_DISABLED}. */ @EnsuresNonNull("data") public void ensureSpaceForWrite(int length) { @@ -223,8 +248,7 @@ public class DecoderInputBuffer extends Buffer { return ByteBuffer.allocateDirect(requiredCapacity); } else { int currentCapacity = data == null ? 0 : data.capacity(); - throw new IllegalStateException("Buffer too small (" + currentCapacity + " < " - + requiredCapacity + ")"); + throw new InsufficientCapacityException(currentCapacity, requiredCapacity); } } }