Fix limit computation of NIO ByteBuffers obtained via ReadOnlyByteBufferBuf.nioBuffer

Motivation:

When starting with a read-only NIO buffer, wrapping it in a ByteBuf,
and then later retrieving a re-wrapped NIO buffer the limit was getting
too short.

Modifications:

Changed ReadOnlyByteBufferBuf.nioBuffer(int,int) to compute the
limit in the same manner as the internalNioBuffer method.

Result:

Round-trip conversion from NIO to ByteBuf to NIO will work reliably.
This commit is contained in:
Bourne, Geoff 2014-03-13 15:10:24 -05:00 committed by Norman Maurer
parent 92037e8bea
commit 1c074eabe5
2 changed files with 14 additions and 1 deletions

View File

@ -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

View File

@ -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());
}
}