diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index 47e4eb7473..31a9fc8066 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -299,7 +299,7 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { @Override public ByteBuffer nioBuffer(int index, int length) { - return (ByteBuffer) buffer.duplicate().position(index).limit(length); + return (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } @Override diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java index 06ae54eca4..9c9373ed2a 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyDirectByteBufferBufTest.java @@ -194,4 +194,17 @@ public class ReadOnlyDirectByteBufferBufTest { ByteBuf slice = buf.slice(); Assert.assertEquals(buf, slice); } + + @Test + public void testWrapBufferRoundTrip() { + ByteBuf buf = buffer(((ByteBuffer) allocate(16).putInt(1).putInt(2).flip()).asReadOnlyBuffer()); + buffers.add(buf); + + Assert.assertEquals(1, buf.readInt()); + + ByteBuffer nioBuffer = buf.nioBuffer(); + + // Ensure this can be accessed without throwing a BufferUnderflowException + Assert.assertEquals(2, nioBuffer.getInt()); + } }