From 57551914af578f4d4615440828394b769338ce7f Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 26 Aug 2008 07:30:41 +0000 Subject: [PATCH] * More tests * Fixed bulk transfer bugs in the CompositeChannelBuffer --- .../netty/buffer/CompositeChannelBuffer.java | 20 +++---- .../buffer/AbstractChannelBufferTest.java | 58 +++++++++---------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java index 5c85c7ea74..007ca4a96d 100644 --- a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java @@ -136,7 +136,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public void getBytes(int index, byte[] dst, int dstIndex, int length) { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length || dstIndex > dst.length - length) { throw new IndexOutOfBoundsException(); } @@ -157,7 +157,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { int sliceId = sliceId(index); int limit = dst.limit(); int length = dst.remaining(); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -180,7 +180,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length || dstIndex > dst.capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -207,7 +207,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public void getBytes(int index, OutputStream out, int length) throws IOException { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -282,7 +282,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public void setBytes(int index, byte[] src, int srcIndex, int length) { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length || srcIndex > src.length - length) { throw new IndexOutOfBoundsException(); } @@ -303,7 +303,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { int sliceId = sliceId(index); int limit = src.limit(); int length = src.remaining(); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -326,7 +326,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length || srcIndex > src.capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -346,7 +346,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public int setBytes(int index, InputStream in, int length) throws IOException { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -384,7 +384,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } @@ -417,7 +417,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { public ChannelBuffer copy(int index, int length) { int sliceId = sliceId(index); - if (index + length >= capacity()) { + if (index > capacity() - length) { throw new IndexOutOfBoundsException(); } diff --git a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java index e4385356ab..7c8be6f910 100644 --- a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java @@ -197,6 +197,7 @@ public abstract class AbstractChannelBufferTest { buffer.getBytes(-1, new byte[0], 0, 0); } + @Test public void getByteArrayBoundaryCheck3() { byte[] dst = new byte[4]; buffer.setInt(0, 0x01020304); @@ -214,6 +215,7 @@ public abstract class AbstractChannelBufferTest { assertEquals(0, dst[3]); } + @Test public void getByteArrayBoundaryCheck4() { byte[] dst = new byte[4]; buffer.setInt(0, 0x01020304); @@ -251,28 +253,26 @@ public abstract class AbstractChannelBufferTest { buffer.setIndex(0, CAPACITY + 1); } - public void getByteBufferState1() { - ByteBuffer dst = ByteBuffer.allocate(8); - dst.position(1); - - buffer.setInt(0, 0x01020304); - buffer.getBytes(0, dst); - - assertEquals(0x0001020304000000L, dst.getLong(0)); - assertEquals(5, dst.position()); - assertEquals(8, dst.limit()); - } - - public void getByteBufferState2() { + @Test + public void getByteBufferState() { ByteBuffer dst = ByteBuffer.allocate(4); dst.position(1); dst.limit(3); - buffer.setInt(0, 0x01020304); + buffer.setByte(0, (byte) 1); + buffer.setByte(1, (byte) 2); + buffer.setByte(2, (byte) 3); + buffer.setByte(3, (byte) 4); buffer.getBytes(1, dst); - assertEquals(0x00020300, dst.getInt(0)); + assertEquals(3, dst.position()); assertEquals(3, dst.limit()); + + dst.clear(); + assertEquals(0, dst.get(0)); + assertEquals(2, dst.get(1)); + assertEquals(3, dst.get(2)); + assertEquals(0, dst.get(3)); } @Test(expected=IndexOutOfBoundsException.class) @@ -280,28 +280,26 @@ public abstract class AbstractChannelBufferTest { buffer.getBytes(-1, ByteBuffer.allocateDirect(0)); } - public void getDirectByteBufferState1() { - ByteBuffer dst = ByteBuffer.allocateDirect(8); - dst.position(1); - - buffer.setInt(0, 0x01020304); - buffer.getBytes(0, dst); - - assertEquals(0x0001020304000000L, dst.getLong(0)); - assertEquals(5, dst.position()); - assertEquals(8, dst.limit()); - } - - public void getDirectByteBufferState2() { + @Test + public void getDirectByteBufferState() { ByteBuffer dst = ByteBuffer.allocateDirect(4); dst.position(1); dst.limit(3); - buffer.setInt(0, 0x01020304); + buffer.setByte(0, (byte) 1); + buffer.setByte(1, (byte) 2); + buffer.setByte(2, (byte) 3); + buffer.setByte(3, (byte) 4); buffer.getBytes(1, dst); - assertEquals(0x00020300, dst.getInt(0)); + assertEquals(3, dst.position()); assertEquals(3, dst.limit()); + + dst.clear(); + assertEquals(0, dst.get(0)); + assertEquals(2, dst.get(1)); + assertEquals(3, dst.get(2)); + assertEquals(0, dst.get(3)); } @Test