#7243 ByteBufferPool and RetainableByteBufferPool now reset the buffer's endianness on release

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
This commit is contained in:
Ludovic Orban 2021-12-09 10:59:20 +01:00
parent 4ef4bf039d
commit 068c24a744
3 changed files with 34 additions and 4 deletions

View File

@ -208,7 +208,7 @@ public interface ByteBufferPool
public void release(ByteBuffer buffer)
{
resetUpdateTime();
BufferUtil.clear(buffer);
BufferUtil.reset(buffer);
if (_size == null || _size.incrementAndGet() <= _maxSize)
{
_queue.offer(buffer);

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.io;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Objects;
@ -245,4 +246,15 @@ public class ArrayByteBufferPoolTest
assertThat(buckets[2].size(), equalTo(2));
assertThat(buckets[7].size(), equalTo(1));
}
@Test
public void testEndiannessResetOnRelease()
{
ArrayByteBufferPool bufferPool = new ArrayByteBufferPool();
ByteBuffer buffer = bufferPool.acquire(10, true);
assertThat(buffer.order(), is(ByteOrder.BIG_ENDIAN));
buffer.order(ByteOrder.LITTLE_ENDIAN);
bufferPool.release(buffer);
assertThat(buffer.order(), is(ByteOrder.BIG_ENDIAN));
}
}

View File

@ -26,6 +26,7 @@ import java.io.RandomAccessFile;
import java.nio.Buffer;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
@ -155,10 +156,27 @@ public class BufferUtil
}
/**
* Clear the buffer to be empty in flush mode.
* The position and limit are set to 0;
* Resets the buffer's endianness to {@link ByteOrder#BIG_ENDIAN}
* and clears the buffer to be empty in flush mode.
* The position and limit are set to 0.
*
* @param buffer The buffer to clear.
* @param buffer the buffer to reset.
*/
public static void reset(ByteBuffer buffer)
{
if (buffer != null)
{
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.position(0);
buffer.limit(0);
}
}
/**
* Clears the buffer to be empty in flush mode.
* The position and limit are set to 0.
*
* @param buffer the buffer to clear.
*/
public static void clear(ByteBuffer buffer)
{