* More tests

* Fixed bulk transfer bugs in the CompositeChannelBuffer
This commit is contained in:
Trustin Lee 2008-08-26 07:30:41 +00:00
parent 8f3799c02e
commit 57551914af
2 changed files with 38 additions and 40 deletions

View File

@ -136,7 +136,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void getBytes(int index, byte[] dst, int dstIndex, int length) { public void getBytes(int index, byte[] dst, int dstIndex, int length) {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length || dstIndex > dst.length - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -157,7 +157,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
int sliceId = sliceId(index); int sliceId = sliceId(index);
int limit = dst.limit(); int limit = dst.limit();
int length = dst.remaining(); int length = dst.remaining();
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -180,7 +180,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length || dstIndex > dst.capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -207,7 +207,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void getBytes(int index, OutputStream out, int length) public void getBytes(int index, OutputStream out, int length)
throws IOException { throws IOException {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -282,7 +282,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void setBytes(int index, byte[] src, int srcIndex, int length) { public void setBytes(int index, byte[] src, int srcIndex, int length) {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length || srcIndex > src.length - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -303,7 +303,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
int sliceId = sliceId(index); int sliceId = sliceId(index);
int limit = src.limit(); int limit = src.limit();
int length = src.remaining(); int length = src.remaining();
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -326,7 +326,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) { public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length || srcIndex > src.capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -346,7 +346,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public int setBytes(int index, InputStream in, int length) public int setBytes(int index, InputStream in, int length)
throws IOException { throws IOException {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -384,7 +384,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public int setBytes(int index, ScatteringByteChannel in, int length) public int setBytes(int index, ScatteringByteChannel in, int length)
throws IOException { throws IOException {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
@ -417,7 +417,7 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
public ChannelBuffer copy(int index, int length) { public ChannelBuffer copy(int index, int length) {
int sliceId = sliceId(index); int sliceId = sliceId(index);
if (index + length >= capacity()) { if (index > capacity() - length) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }

View File

@ -197,6 +197,7 @@ public abstract class AbstractChannelBufferTest {
buffer.getBytes(-1, new byte[0], 0, 0); buffer.getBytes(-1, new byte[0], 0, 0);
} }
@Test
public void getByteArrayBoundaryCheck3() { public void getByteArrayBoundaryCheck3() {
byte[] dst = new byte[4]; byte[] dst = new byte[4];
buffer.setInt(0, 0x01020304); buffer.setInt(0, 0x01020304);
@ -214,6 +215,7 @@ public abstract class AbstractChannelBufferTest {
assertEquals(0, dst[3]); assertEquals(0, dst[3]);
} }
@Test
public void getByteArrayBoundaryCheck4() { public void getByteArrayBoundaryCheck4() {
byte[] dst = new byte[4]; byte[] dst = new byte[4];
buffer.setInt(0, 0x01020304); buffer.setInt(0, 0x01020304);
@ -251,28 +253,26 @@ public abstract class AbstractChannelBufferTest {
buffer.setIndex(0, CAPACITY + 1); buffer.setIndex(0, CAPACITY + 1);
} }
public void getByteBufferState1() { @Test
ByteBuffer dst = ByteBuffer.allocate(8); public void getByteBufferState() {
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() {
ByteBuffer dst = ByteBuffer.allocate(4); ByteBuffer dst = ByteBuffer.allocate(4);
dst.position(1); dst.position(1);
dst.limit(3); 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); buffer.getBytes(1, dst);
assertEquals(0x00020300, dst.getInt(0));
assertEquals(3, dst.position()); assertEquals(3, dst.position());
assertEquals(3, dst.limit()); 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) @Test(expected=IndexOutOfBoundsException.class)
@ -280,28 +280,26 @@ public abstract class AbstractChannelBufferTest {
buffer.getBytes(-1, ByteBuffer.allocateDirect(0)); buffer.getBytes(-1, ByteBuffer.allocateDirect(0));
} }
public void getDirectByteBufferState1() { @Test
ByteBuffer dst = ByteBuffer.allocateDirect(8); public void getDirectByteBufferState() {
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() {
ByteBuffer dst = ByteBuffer.allocateDirect(4); ByteBuffer dst = ByteBuffer.allocateDirect(4);
dst.position(1); dst.position(1);
dst.limit(3); 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); buffer.getBytes(1, dst);
assertEquals(0x00020300, dst.getInt(0));
assertEquals(3, dst.position()); assertEquals(3, dst.position());
assertEquals(3, dst.limit()); 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 @Test