Fix IndexOutOfBoundsException for CompositeByteBuf #4679

Motivation:

Modifications:

Use the correct start index

Result:

Fixes #4679
This commit is contained in:
Xiaoyan Lin 2016-01-08 15:52:15 -08:00 committed by Scott Mitchell
parent 5c05629da1
commit 6fe0db4001
2 changed files with 14 additions and 1 deletions

View File

@ -548,7 +548,7 @@ public final class ByteBufUtil {
try {
buffer.writeBytes(src, readerIndex, len);
// Use internalNioBuffer(...) to reduce object creation.
decodeString(decoder, buffer.internalNioBuffer(readerIndex, len), dst);
decodeString(decoder, buffer.internalNioBuffer(0, len), dst);
} finally {
// Release the temporary buffer again.
buffer.release();

View File

@ -187,4 +187,17 @@ public class ByteBufUtilTest {
Assert.assertEquals(text, ByteBufUtil.decodeString(buffer, 0, buffer.readableBytes(), charset));
buffer.release();
}
@Test
public void testToStringDoesNotThrowIndexOutOfBounds() {
CompositeByteBuf buffer = Unpooled.compositeBuffer();
try {
byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8);
buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes));
buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes));
Assert.assertEquals("1234", buffer.toString(bytes.length, bytes.length, CharsetUtil.UTF_8));
} finally {
buffer.release();
}
}
}