Fixed issue: NETTY-31 (ChannelBuffers.equals() fails to compare the buffers with different endianness)

* Same fix to compareTo
This commit is contained in:
Trustin Lee 2008-08-26 10:52:21 +00:00
parent f4165b10e9
commit 834b373a96
2 changed files with 92 additions and 10 deletions

View File

@ -693,16 +693,31 @@ public class ChannelBuffers {
int aIndex = bufferA.readerIndex(); int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex(); int bIndex = bufferB.readerIndex();
for (int i = longCount; i > 0; i --) {
long va = bufferA.getLong(aIndex); if (bufferA.order() == bufferB.order()) {
long vb = bufferB.getLong(bIndex); for (int i = longCount; i > 0; i --) {
if (va > vb) { long va = bufferA.getLong(aIndex);
return 1; long vb = bufferB.getLong(bIndex);
} else if (va < vb) { if (va > vb) {
return -1; return 1;
} else if (va < vb) {
return -1;
}
aIndex += 8;
bIndex += 8;
}
} else {
for (int i = longCount; i > 0; i --) {
long va = bufferA.getLong(aIndex);
long vb = swapLong(bufferB.getLong(bIndex));
if (va > vb) {
return 1;
} else if (va < vb) {
return -1;
}
aIndex += 8;
bIndex += 8;
} }
aIndex += 8;
bIndex += 8;
} }
for (int i = byteCount; i > 0; i --) { for (int i = byteCount; i > 0; i --) {

View File

@ -1188,7 +1188,7 @@ public abstract class AbstractChannelBufferTest {
} }
@Test @Test
public void testStreamTransfer() throws Exception { public void testStreamTransfer1() throws Exception {
byte[] expected = new byte[buffer.capacity()]; byte[] expected = new byte[buffer.capacity()];
random.nextBytes(expected); random.nextBytes(expected);
@ -1206,6 +1206,29 @@ public abstract class AbstractChannelBufferTest {
assertTrue(Arrays.equals(expected, out.toByteArray())); assertTrue(Arrays.equals(expected, out.toByteArray()));
} }
@Test
public void testStreamTransfer2() throws Exception {
byte[] expected = new byte[buffer.capacity()];
random.nextBytes(expected);
buffer.clear();
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
ByteArrayInputStream in = new ByteArrayInputStream(expected, i, BLOCK_SIZE);
assertEquals(i, buffer.writerIndex());
buffer.writeBytes(in, BLOCK_SIZE);
assertEquals(i + BLOCK_SIZE, buffer.writerIndex());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
assertEquals(i, buffer.readerIndex());
buffer.readBytes(out, BLOCK_SIZE);
assertEquals(i + BLOCK_SIZE, buffer.readerIndex());
}
assertTrue(Arrays.equals(expected, out.toByteArray()));
}
@Test @Test
public void testCopy() { public void testCopy() {
for (int i = 0; i < buffer.capacity(); i ++) { for (int i = 0; i < buffer.capacity(); i ++) {
@ -1232,4 +1255,48 @@ public abstract class AbstractChannelBufferTest {
copy.setByte(1, (byte) (copy.getByte(1) + 1)); copy.setByte(1, (byte) (copy.getByte(1) + 1));
assertTrue(buffer.getByte(readerIndex + 1) != copy.getByte(1)); assertTrue(buffer.getByte(readerIndex + 1) != copy.getByte(1));
} }
@Test
public void testEquals() {
assertFalse(buffer.equals(null));
assertFalse(buffer.equals(new Object()));
byte[] value = new byte[32];
buffer.setIndex(0, value.length);
random.nextBytes(value);
buffer.setBytes(0, value);
assertEquals(buffer, wrappedBuffer(BIG_ENDIAN, value));
assertEquals(buffer, wrappedBuffer(LITTLE_ENDIAN, value));
value[0] ++;
assertFalse(buffer.equals(wrappedBuffer(BIG_ENDIAN, value)));
assertFalse(buffer.equals(wrappedBuffer(LITTLE_ENDIAN, value)));
}
@Test
public void testCompareTo() {
try {
buffer.compareTo(null);
fail();
} catch (NullPointerException e) {
// Expected
}
byte[] value = new byte[32];
buffer.setIndex(0, value.length);
random.nextBytes(value);
buffer.setBytes(0, value);
assertEquals(0, buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)));
assertEquals(0, buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)));
value[0] ++;
assertTrue(buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)) < 0);
assertTrue(buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)) < 0);
value[0] -= 2;
assertTrue(buffer.compareTo(wrappedBuffer(BIG_ENDIAN, value)) > 0);
assertTrue(buffer.compareTo(wrappedBuffer(LITTLE_ENDIAN, value)) > 0);
}
} }