Commit d443be2a by olly Committed by Oliver Woodman

Clear supplementalData in DecoderInputBuffer.clear

PiperOrigin-RevId: 268894250
parent 1d3d92ee
...@@ -95,14 +95,19 @@ public class DecoderInputBuffer extends Buffer { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment