mirror of
https://github.com/samsonjs/media.git
synced 2026-03-26 09:35:47 +00:00
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
This commit is contained in:
parent
e3620ed646
commit
de0e1e5168
1 changed files with 28 additions and 4 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue