diff --git a/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java b/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java index 7a19d85aa8..302ae0609f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/decoder/DecoderInputBuffer.java @@ -95,14 +95,19 @@ public class DecoderInputBuffer extends Buffer { this.bufferReplacementMode = bufferReplacementMode; } - /** Resets {@link #supplementalData} in preparation for storing {@code length} bytes. */ + /** + * Clears {@link #supplementalData} and ensures that it's large enough to accommodate {@code + * length} bytes. + * + * @param length The length of the supplemental data that must be accommodated, in bytes. + */ @EnsuresNonNull("supplementalData") public void resetSupplementalData(int length) { if (supplementalData == null || supplementalData.capacity() < length) { supplementalData = ByteBuffer.allocate(length); + } else { + supplementalData.clear(); } - supplementalData.position(0); - supplementalData.limit(length); } /** @@ -134,8 +139,7 @@ public class DecoderInputBuffer extends Buffer { ByteBuffer newData = createReplacementByteBuffer(requiredCapacity); // Copy data up to the current position from the old buffer to the new one. if (position > 0) { - data.position(0); - data.limit(position); + data.flip(); newData.put(data); } // Set the new buffer. @@ -158,7 +162,7 @@ public class DecoderInputBuffer extends Buffer { } /** - * Flips {@link #data} in preparation for being queued to a decoder. + * Flips {@link #data} and {@link #supplementalData} in preparation for being queued to a decoder. * * @see java.nio.Buffer#flip() */ @@ -175,6 +179,9 @@ public class DecoderInputBuffer extends Buffer { if (data != null) { data.clear(); } + if (supplementalData != null) { + supplementalData.clear(); + } } private ByteBuffer createReplacementByteBuffer(int requiredCapacity) {