Fix nioBuffer implementation for CompositeByteBuf

This commit is contained in:
Greg Soltis 2013-09-15 14:39:57 -07:00 committed by Norman Maurer
parent c0b9ef9386
commit 335f3807a5
2 changed files with 22 additions and 1 deletions

View File

@ -1122,7 +1122,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
//noinspection ForLoopReplaceableByForEach //noinspection ForLoopReplaceableByForEach
for (int i = 0; i < buffers.length; i++) { for (int i = 0; i < buffers.length; i++) {
merged.put(buffers[0]); merged.put(buffers[i]);
} }
merged.flip(); merged.flip();

View File

@ -176,6 +176,27 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
assertEquals(0, buf.arrayOffset()); assertEquals(0, buf.arrayOffset());
} }
@Test
public void testCompositeToSingleBuffer() {
CompositeByteBuf buf = compositeBuffer(3);
buf.addComponent(wrappedBuffer(new byte[] {1, 2, 3}));
assertEquals(1, buf.numComponents());
buf.addComponent(wrappedBuffer(new byte[] {4}));
assertEquals(2, buf.numComponents());
buf.addComponent(wrappedBuffer(new byte[] {5, 6}));
assertEquals(3, buf.numComponents());
// NOTE: hard-coding 6 here, since it seems like addComponent doesn't bump the writer index.
// I'm unsure as to whether or not this is correct behavior
ByteBuffer nioBuffer = buf.nioBuffer(0, 6);
byte[] bytes = nioBuffer.array();
assertEquals(6, bytes.length);
assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6}, bytes);
}
@Test @Test
public void testFullConsolidation() { public void testFullConsolidation() {
CompositeByteBuf buf = freeLater(compositeBuffer(Integer.MAX_VALUE)); CompositeByteBuf buf = freeLater(compositeBuffer(Integer.MAX_VALUE));