[#1925] Only expose sub-region of ByteBuf on nioBuffer(...)
This commit is contained in:
parent
53fcff2eab
commit
68b616728a
@ -330,7 +330,7 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
index = idx(index);
|
||||
return (ByteBuffer) memory.duplicate().position(index).limit(index + length);
|
||||
return ((ByteBuffer) memory.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -259,7 +259,8 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
index = idx(index);
|
||||
return (ByteBuffer) ByteBuffer.wrap(memory).position(index).limit(index + length);
|
||||
ByteBuffer buf = ByteBuffer.wrap(memory, index, length);
|
||||
return buf.slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -339,7 +339,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
index = idx(index);
|
||||
return (ByteBuffer) memory.duplicate().position(index).limit(index + length);
|
||||
return ((ByteBuffer) memory.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -575,7 +575,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
|
||||
return ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -267,7 +267,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
ensureAccessible();
|
||||
return ByteBuffer.wrap(array, index, length);
|
||||
return ByteBuffer.wrap(array, index, length).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -475,7 +475,7 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
|
||||
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
|
||||
return ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1942,6 +1942,23 @@ public abstract class AbstractByteBufTest {
|
||||
assertEquals((byte) 0 , buffer.readByte());
|
||||
buffer.readByte();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNioBufferExposeOnlyRegion() {
|
||||
final ByteBuf buffer = freeLater(newBuffer(8));
|
||||
byte[] data = new byte[8];
|
||||
random.nextBytes(data);
|
||||
buffer.writeBytes(data);
|
||||
|
||||
ByteBuffer nioBuf = buffer.nioBuffer(1, data.length - 2);
|
||||
assertEquals(0, nioBuf.position());
|
||||
assertEquals(6, nioBuf.remaining());
|
||||
|
||||
for (int i = 1; nioBuf.hasRemaining(); i++) {
|
||||
assertEquals(data[i], nioBuf.get());
|
||||
}
|
||||
}
|
||||
|
||||
static final class TestGatheringByteChannel implements GatheringByteChannel {
|
||||
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
private final WritableByteChannel channel = Channels.newChannel(out);
|
||||
|
@ -88,4 +88,10 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
|
||||
public void testSliceBytesInArrayMultipleThreads() throws Exception {
|
||||
super.testSliceBytesInArrayMultipleThreads();
|
||||
}
|
||||
|
||||
@Test(expected = IndexOutOfBoundsException.class)
|
||||
@Override
|
||||
public void testNioBufferExposeOnlyRegion() {
|
||||
super.testNioBufferExposeOnlyRegion();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user