[#1821] Fix IndexOutOfBoundsException which was thrown if the last component was removed but other components was left

This commit is contained in:
Norman Maurer 2013-09-09 20:29:30 +02:00
parent 36c8ac5e5c
commit 451e91d142
2 changed files with 13 additions and 2 deletions

View File

@ -323,7 +323,8 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
}
private void updateComponentOffsets(int cIndex) {
if (components.isEmpty()) {
int size = components.size();
if (size <= cIndex) {
return;
}
@ -334,7 +335,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
cIndex ++;
}
for (int i = cIndex; i < components.size(); i ++) {
for (int i = cIndex; i < size; i ++) {
Component prev = components.get(i - 1);
Component cur = components.get(i);
cur.offset = prev.endOffset;

View File

@ -567,6 +567,16 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
assertEquals(0, freeLater(buf.duplicate()).readableBytes());
}
@Test
public void testRemoveLastComponentWithOthersLeft() {
CompositeByteBuf buf = freeLater(compositeBuffer());
buf.addComponent(wrappedBuffer(new byte[]{1, 2}));
buf.addComponent(wrappedBuffer(new byte[]{1, 2}));
assertEquals(2, buf.numComponents());
buf.removeComponent(1);
assertEquals(1, buf.numComponents());
}
@Override
@Test
public void testInternalNioBuffer() {