Clear supplementalData in DecoderInputBuffer.clear

PiperOrigin-RevId: 268894250
This commit is contained in:
olly 2019-09-13 14:39:11 +01:00 committed by Oliver Woodman
parent 1d3d92ee4b
commit d443be2a46

View File

@ -95,14 +95,19 @@ public class DecoderInputBuffer extends Buffer {
this.bufferReplacementMode = bufferReplacementMode; 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") @EnsuresNonNull("supplementalData")
public void resetSupplementalData(int length) { public void resetSupplementalData(int length) {
if (supplementalData == null || supplementalData.capacity() < length) { if (supplementalData == null || supplementalData.capacity() < length) {
supplementalData = ByteBuffer.allocate(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); ByteBuffer newData = createReplacementByteBuffer(requiredCapacity);
// Copy data up to the current position from the old buffer to the new one. // Copy data up to the current position from the old buffer to the new one.
if (position > 0) { if (position > 0) {
data.position(0); data.flip();
data.limit(position);
newData.put(data); newData.put(data);
} }
// Set the new buffer. // 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() * @see java.nio.Buffer#flip()
*/ */
@ -175,6 +179,9 @@ public class DecoderInputBuffer extends Buffer {
if (data != null) { if (data != null) {
data.clear(); data.clear();
} }
if (supplementalData != null) {
supplementalData.clear();
}
} }
private ByteBuffer createReplacementByteBuffer(int requiredCapacity) { private ByteBuffer createReplacementByteBuffer(int requiredCapacity) {