Skip to content

Commit

Permalink
Fix IndexOutOfBoundException when using CompositeChannelBuffer and th…
Browse files Browse the repository at this point in the history
…e readerIndex is at the last position and an empty array is passed to read to. See #474
  • Loading branch information
normanmaurer committed Sep 22, 2012
1 parent ef0ee5f commit 530b72f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java
Expand Up @@ -228,12 +228,19 @@ public long getLong(int index) {
}

public void getBytes(int index, byte[] dst, int dstIndex, int length) {
int componentId = componentId(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 componentId = componentId(index);

int i = componentId;
while (length > 0) {
Expand All @@ -256,7 +263,9 @@ public void getBytes(int index, ByteBuffer dst) {
throw new IndexOutOfBoundsException("Too many bytes to be read - Needs "
+ (index + length) + ", maximum is " + capacity());
}

if (index < 0) {
throw new IndexOutOfBoundsException("Index must be >= 0");
}
int i = componentId;
try {
while (length > 0) {
Expand All @@ -275,14 +284,18 @@ public void getBytes(int index, ByteBuffer dst) {
}

public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
int componentId = componentId(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 = componentId(index);
while (length > 0) {
ChannelBuffer s = components[i];
int adjustment = indices[i];
Expand Down Expand Up @@ -310,13 +323,17 @@ public int getBytes(int index, GatheringByteChannel out, int length)

public void getBytes(int index, OutputStream out, int length)
throws IOException {
int componentId = componentId(index);
if (index > capacity() - length) {
throw new IndexOutOfBoundsException("Too many bytes to be read - needs "
+ (index + length) + ", maximum of " + capacity());
}

int i = componentId;
if (index < 0) {
throw new IndexOutOfBoundsException("Index must be >= 0");
}
if (length == 0) {
return;
}
int i = componentId(index);
while (length > 0) {
ChannelBuffer s = components[i];
int adjustment = indices[i];
Expand Down Expand Up @@ -640,15 +657,20 @@ public ByteBuffer toByteBuffer(int index, int length) {

@Override
public ByteBuffer[] toByteBuffers(int index, int length) {
int componentId = componentId(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];
}

List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(components.length);

int i = componentId;
int i = componentId(index);
while (length > 0) {
ChannelBuffer s = components[i];
int adjustment = indices[i];
Expand Down
Expand Up @@ -355,4 +355,12 @@ public void testWrittenBuffersEquals() {
wrappedBuffer(order, new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5));
assertFalse(ChannelBuffers.equals(a, b));
}

// Test for #474
@Test
public void testEmptyBuffer() {
ChannelBuffer b = wrappedBuffer(order, new byte[] {1,2}, new byte[] {3,4});
b.readBytes(new byte[4]);
b.readBytes(new byte[0]);
}
}

0 comments on commit 530b72f

Please sign in to comment.