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

* Made sure to swap the order if the two buffers have different orders
This commit is contained in:
Trustin Lee 2008-08-26 10:35:36 +00:00
parent 48929afd60
commit f4165b10e9
2 changed files with 70 additions and 5 deletions

View File

@ -650,6 +650,8 @@ public class ChannelBuffers {
int aIndex = bufferA.readerIndex();
int bIndex = bufferB.readerIndex();
if (bufferA.order() == bufferB.order()) {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != bufferB.getLong(bIndex)) {
return false;
@ -657,6 +659,15 @@ public class ChannelBuffers {
aIndex += 8;
bIndex += 8;
}
} else {
for (int i = longCount; i > 0; i --) {
if (bufferA.getLong(aIndex) != swapLong(bufferB.getLong(bIndex))) {
return false;
}
aIndex += 8;
bIndex += 8;
}
}
for (int i = byteCount; i > 0; i --) {
if (bufferA.getByte(aIndex) != bufferB.getByte(bIndex)) {

View File

@ -1069,6 +1069,60 @@ public abstract class AbstractChannelBufferTest {
}
}
@Test
public void testSequentialCopiedBufferTransfer() {
buffer.writerIndex(0);
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
byte[] value = new byte[BLOCK_SIZE];
random.nextBytes(value);
assertEquals(0, buffer.readerIndex());
assertEquals(i, buffer.writerIndex());
buffer.writeBytes(value);
}
random.setSeed(seed);
byte[] expectedValue = new byte[BLOCK_SIZE];
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
random.nextBytes(expectedValue);
assertEquals(i, buffer.readerIndex());
assertEquals(CAPACITY, buffer.writerIndex());
ChannelBuffer actualValue = buffer.readBytes(BLOCK_SIZE);
assertEquals(wrappedBuffer(expectedValue), actualValue);
// Make sure if it's a copied buffer.
actualValue.setByte(0, (byte) (actualValue.getByte(0) + 1));
assertFalse(buffer.getByte(i) == actualValue.getByte(0));
}
}
@Test
public void testSequentialSlice() {
buffer.writerIndex(0);
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
byte[] value = new byte[BLOCK_SIZE];
random.nextBytes(value);
assertEquals(0, buffer.readerIndex());
assertEquals(i, buffer.writerIndex());
buffer.writeBytes(value);
}
random.setSeed(seed);
byte[] expectedValue = new byte[BLOCK_SIZE];
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
random.nextBytes(expectedValue);
assertEquals(i, buffer.readerIndex());
assertEquals(CAPACITY, buffer.writerIndex());
ChannelBuffer actualValue = buffer.readSlice(BLOCK_SIZE);
System.out.println(wrappedBuffer(expectedValue) + ", " + hexDump(wrappedBuffer(expectedValue)));
System.out.println(actualValue + ", " + hexDump(actualValue));
assertEquals(wrappedBuffer(expectedValue), actualValue);
// Make sure if it's a sliced buffer.
actualValue.setByte(0, (byte) (actualValue.getByte(0) + 1));
assertEquals(buffer.getByte(i), actualValue.getByte(0));
}
}
@Test
public void testWriteZero() {
buffer.clear();