diff --git a/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java index 96c8036c55..860925f506 100644 --- a/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java @@ -578,14 +578,19 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit @Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { - int componentId = toComponentIndex(index); if (index > capacity() - length || dstIndex > dst.length - length) { throw new IndexOutOfBoundsException("Too many bytes to read - Needs " + (index + length) + ", maximum is " + capacity() + " or " + dst.length); } + if (index < 0) { + throw new IndexOutOfBoundsException("index must be >= 0"); + } + if (length == 0) { + return; + } + int i = toComponentIndex(index); - int i = componentId; while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; @@ -601,15 +606,20 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit @Override public void getBytes(int index, ByteBuffer dst) { - int componentId = toComponentIndex(index); int limit = dst.limit(); int length = dst.remaining(); + if (index > capacity() - length) { throw new IndexOutOfBoundsException("Too many bytes to be read - Needs " + (index + length) + ", maximum is " + capacity()); } - - int i = componentId; + if (index < 0) { + throw new IndexOutOfBoundsException("index must be >= 0"); + } + if (length == 0) { + return; + } + int i = toComponentIndex(index); try { while (length > 0) { Component c = components.get(i); @@ -629,14 +639,18 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit @Override public void getBytes(int index, ByteBuf dst, int dstIndex, int length) { - int componentId = toComponentIndex(index); if (index > capacity() - length || dstIndex > dst.capacity() - length) { throw new IndexOutOfBoundsException("Too many bytes to be read - Needs " + (index + length) + " or " + (dstIndex + length) + ", maximum is " + capacity() + " or " + dst.capacity()); } - - int i = componentId; + if (index < 0) { + throw new IndexOutOfBoundsException("index must be >= 0"); + } + if (length == 0) { + return; + } + int i = toComponentIndex(index); while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; @@ -670,13 +684,18 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit @Override public void getBytes(int index, OutputStream out, int length) throws IOException { - int componentId = toComponentIndex(index); if (index > capacity() - length) { throw new IndexOutOfBoundsException("Too many bytes to be read - needs " + (index + length) + ", maximum of " + capacity()); } + if (index < 0) { + throw new IndexOutOfBoundsException("index must be >= 0"); + } + if (length == 0) { + return; + } - int i = componentId; + int i = toComponentIndex(index); while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; @@ -1031,11 +1050,17 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit @Override public ByteBuffer[] nioBuffers(int index, int length) { - int componentId = toComponentIndex(index); if (index + length > capacity()) { throw new IndexOutOfBoundsException("Too many bytes to convert - Needs" + (index + length) + ", maximum is " + capacity()); } + if (index < 0) { + throw new IndexOutOfBoundsException("index must be >= 0"); + } + if (length == 0) { + return new ByteBuffer[0]; + } + int componentId = toComponentIndex(index); List buffers = new ArrayList(components.size()); diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java index 2900336c25..b73513e358 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeChannelBufferTest.java @@ -410,4 +410,11 @@ public abstract class AbstractCompositeChannelBufferTest extends wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order)); assertFalse(ByteBufUtil.equals(a, b)); } + + @Test + public void testEmptyBuffer() { + ByteBuf b = wrappedBuffer(new byte[] {1, 2}, new byte[] {3, 4}); + b.readBytes(new byte[4]); + b.readBytes(new byte[0]); + } }