From acb1ac84e6e737ac934dcc148f247dc02ffcc796 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 1 Aug 2011 04:28:08 +0900 Subject: [PATCH] NETTY-425 ChannelBuffers.compare does not handle unsigned bytes correctly * Fixed a bug where signed comparison is made where unsigned comparison is expected --- .../java/org/jboss/netty/buffer/ChannelBuffers.java | 4 ++-- .../org/jboss/netty/buffer/ChannelBuffersTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java index 572f398ae8..02f27d2af3 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java @@ -979,8 +979,8 @@ public class ChannelBuffers { } for (int i = byteCount; i > 0; i --) { - byte va = bufferA.getByte(aIndex); - byte vb = bufferB.getByte(bIndex); + short va = bufferA.getUnsignedByte(aIndex); + short vb = bufferB.getUnsignedByte(bIndex); if (va > vb) { return 1; } else if (va < vb) { diff --git a/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java b/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java index 88a1afa07e..81a649e93b 100644 --- a/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java +++ b/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java @@ -200,6 +200,19 @@ public class ChannelBuffersTest { assertSame(EMPTY_BUFFER, copiedBuffer(new ChannelBuffer[] { buffer(0), buffer(0) })); } + @Test + public void testCompare2() { + assertTrue(ChannelBuffers.compare( + ChannelBuffers.wrappedBuffer(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}), + ChannelBuffers.wrappedBuffer(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00})) + > 0); + + assertTrue(ChannelBuffers.compare( + ChannelBuffers.wrappedBuffer(new byte[]{(byte) 0xFF}), + ChannelBuffers.wrappedBuffer(new byte[]{(byte) 0x00})) + > 0); + } + @Test(expected = NullPointerException.class) public void shouldDisallowNullEndian1() { buffer(null, 0);