Fix a bug where DefaultCompositeByteBuf.nioBuffers() fails when its component's nioBufferCount() is greater than 1

- Fixes #1267
This commit is contained in:
Trustin Lee 2013-04-19 04:35:44 +09:00
parent 9e890f0ab8
commit 8884e311f1
2 changed files with 26 additions and 1 deletions

View File

@ -1094,7 +1094,16 @@ public class DefaultCompositeByteBuf extends AbstractReferenceCountedByteBuf
ByteBuf s = c.buf;
int adjustment = c.offset;
int localLength = Math.min(length, s.capacity() - (index - adjustment));
buffers.add(s.nioBuffer(index - adjustment, localLength));
switch (s.nioBufferCount()) {
case 0:
throw new UnsupportedOperationException();
case 1:
buffers.add(s.nioBuffer(index - adjustment, localLength));
break;
default:
Collections.addAll(buffers, s.nioBuffers(index - adjustment, localLength));
}
index += localLength;
length -= localLength;
i ++;

View File

@ -517,4 +517,20 @@ public abstract class AbstractCompositeByteBufTest extends
c3.release(2);
c2.release();
}
@Test
public void testNestedLayout() {
CompositeByteBuf buf = freeLater(compositeBuffer());
buf.addComponent(
compositeBuffer()
.addComponent(wrappedBuffer(new byte[]{1, 2}))
.addComponent(wrappedBuffer(new byte[]{3, 4})).slice(1, 2));
ByteBuffer[] nioBuffers = buf.nioBuffers(0, 2);
assertThat(nioBuffers.length, is(2));
assertThat(nioBuffers[0].remaining(), is(1));
assertThat(nioBuffers[0].get(), is((byte) 2));
assertThat(nioBuffers[1].remaining(), is(1));
assertThat(nioBuffers[1].get(), is((byte) 3));
}
}