Fixed issue: NETTY-31 (ChannelBuffers.equals() fails to compare the buffers with different endianness)
* Same fix to compareTo
This commit is contained in:
parent
f4165b10e9
commit
834b373a96
@ -693,6 +693,8 @@ public class ChannelBuffers {
|
|||||||
|
|
||||||
int aIndex = bufferA.readerIndex();
|
int aIndex = bufferA.readerIndex();
|
||||||
int bIndex = bufferB.readerIndex();
|
int bIndex = bufferB.readerIndex();
|
||||||
|
|
||||||
|
if (bufferA.order() == bufferB.order()) {
|
||||||
for (int i = longCount; i > 0; i --) {
|
for (int i = longCount; i > 0; i --) {
|
||||||
long va = bufferA.getLong(aIndex);
|
long va = bufferA.getLong(aIndex);
|
||||||
long vb = bufferB.getLong(bIndex);
|
long vb = bufferB.getLong(bIndex);
|
||||||
@ -704,6 +706,19 @@ public class ChannelBuffers {
|
|||||||
aIndex += 8;
|
aIndex += 8;
|
||||||
bIndex += 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = byteCount; i > 0; i --) {
|
for (int i = byteCount; i > 0; i --) {
|
||||||
byte va = bufferA.getByte(aIndex);
|
byte va = bufferA.getByte(aIndex);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user