#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:
parent
b2203115e2
commit
4068901c14
|
@ -124,7 +124,7 @@ public class ArrayRetainableByteBufferPool implements RetainableByteBufferPool,
|
||||||
{
|
{
|
||||||
buffer = newRetainableByteBuffer(bucket._capacity, direct, byteBuffer ->
|
buffer = newRetainableByteBuffer(bucket._capacity, direct, byteBuffer ->
|
||||||
{
|
{
|
||||||
BufferUtil.clear(byteBuffer);
|
BufferUtil.reset(byteBuffer);
|
||||||
reservedEntry.release();
|
reservedEntry.release();
|
||||||
});
|
});
|
||||||
reservedEntry.enable(buffer, true);
|
reservedEntry.enable(buffer, true);
|
||||||
|
|
|
@ -187,7 +187,7 @@ public interface ByteBufferPool
|
||||||
public void release(ByteBuffer buffer)
|
public void release(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
resetUpdateTime();
|
resetUpdateTime();
|
||||||
BufferUtil.clear(buffer);
|
BufferUtil.reset(buffer);
|
||||||
if (_size == null || _size.incrementAndGet() <= _maxSize)
|
if (_size == null || _size.incrementAndGet() <= _maxSize)
|
||||||
{
|
{
|
||||||
_queue.offer(buffer);
|
_queue.offer(buffer);
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package org.eclipse.jetty.io;
|
package org.eclipse.jetty.io;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -240,4 +241,15 @@ public class ArrayByteBufferPoolTest
|
||||||
assertThat(buckets[2].size(), equalTo(2));
|
assertThat(buckets[2].size(), equalTo(2));
|
||||||
assertThat(buckets[7].size(), equalTo(1));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package org.eclipse.jetty.io;
|
package org.eclipse.jetty.io;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -379,4 +380,15 @@ public class ArrayRetainableByteBufferPoolTest
|
||||||
i -> 1 << i);
|
i -> 1 << i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEndiannessResetOnRelease()
|
||||||
|
{
|
||||||
|
ArrayRetainableByteBufferPool bufferPool = new ArrayRetainableByteBufferPool();
|
||||||
|
RetainableByteBuffer buffer = bufferPool.acquire(10, true);
|
||||||
|
assertThat(buffer.getBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN));
|
||||||
|
buffer.getBuffer().order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
assertThat(buffer.release(), is(true));
|
||||||
|
assertThat(buffer.getBuffer().order(), Matchers.is(ByteOrder.BIG_ENDIAN));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.io.RandomAccessFile;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.nio.BufferOverflowException;
|
import java.nio.BufferOverflowException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.FileChannel.MapMode;
|
import java.nio.channels.FileChannel.MapMode;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
@ -167,10 +168,27 @@ public class BufferUtil
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the buffer to be empty in flush mode.
|
* Resets the buffer's endianness to {@link ByteOrder#BIG_ENDIAN}
|
||||||
* The position and limit are set to 0;
|
* 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)
|
public static void clear(ByteBuffer buffer)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue