diff --git a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java index 26ab1ecfc4..b50f720637 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java @@ -330,7 +330,7 @@ final class PooledDirectByteBuf extends PooledByteBuf { 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java index 74831e9f17..1eb445b04e 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java @@ -259,7 +259,8 @@ final class PooledHeapByteBuf extends PooledByteBuf { 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 diff --git a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java index 6f547e9255..2ed0437210 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java @@ -339,7 +339,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf { 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 diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index 1ecacbeb6f..948b348f24 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -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 diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index 4c80c30317..674a7a440c 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -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 diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java index 46fc3729ab..9d076b09e6 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java @@ -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 diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index d9ecbb7824..3d0183204a 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -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); diff --git a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java index 5a3c891ab5..505de88d70 100644 --- a/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SlicedByteBufTest.java @@ -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(); + } }