From 19fcbac57544096a98558cb07705502f31873230 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 10 Aug 2008 00:52:59 +0000 Subject: [PATCH] Added unsigned integer access methods --- .../netty/buffer/AbstractChannelBuffer.java | 42 +++++++++++++++++- .../buffer/BigEndianHeapChannelBuffer.java | 2 +- .../buffer/ByteBufferBackedChannelBuffer.java | 2 +- .../org/jboss/netty/buffer/ChannelBuffer.java | 44 +++++++++++++++++++ .../netty/buffer/CompositeChannelBuffer.java | 4 +- .../netty/buffer/DuplicatedChannelBuffer.java | 4 +- .../netty/buffer/DynamicChannelBuffer.java | 4 +- .../buffer/LittleEndianHeapChannelBuffer.java | 2 +- .../netty/buffer/ReadOnlyChannelBuffer.java | 4 +- .../netty/buffer/SlicedChannelBuffer.java | 4 +- .../netty/buffer/TruncatedChannelBuffer.java | 4 +- .../codec/replay/ReplayingDecoderBuffer.java | 42 +++++++++++++++++- .../buffer/AbstractChannelBufferTest.java | 4 +- 13 files changed, 143 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index cd72ffa4a8..a3f58b4d07 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -122,6 +122,26 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { readerIndex = 0; } + public short getUnsignedByte(int index) { + return (short) (getByte(index) & 0xFF); + } + + public int getUnsignedShort(int index) { + return getShort(index) & 0xFFFF; + } + + public int getMedium(int index) { + int value = getUnsignedMedium(index); + if ((value & 0x800000) != 0) { + value |= 0xff000000; + } + return value; + } + + public long getUnsignedInt(int index) { + return getInt(index) & 0xFFFFFFFFL; + } + public void getBytes(int index, byte[] dst) { getBytes(index, dst, 0, dst.length); } @@ -177,6 +197,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return getByte(readerIndex ++); } + public short readUnsignedByte() { + return (short) (readByte() & 0xFF); + } + public short readShort() { checkReadableBytes(2); short v = getShort(readerIndex); @@ -184,9 +208,21 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return v; } + public int readUnsignedShort() { + return readShort() & 0xFFFF; + } + public int readMedium() { + int value = readUnsignedMedium(); + if ((value & 0x800000) != 0) { + value |= 0xff000000; + } + return value; + } + + public int readUnsignedMedium() { checkReadableBytes(3); - int v = getMedium(readerIndex); + int v = getUnsignedMedium(readerIndex); readerIndex += 3; return v; } @@ -198,6 +234,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return v; } + public long readUnsignedInt() { + return readInt() & 0xFFFFFFFFL; + } + public long readLong() { checkReadableBytes(8); long v = getLong(readerIndex); diff --git a/src/main/java/org/jboss/netty/buffer/BigEndianHeapChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/BigEndianHeapChannelBuffer.java index f5635b1f26..3cf9a75de3 100644 --- a/src/main/java/org/jboss/netty/buffer/BigEndianHeapChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/BigEndianHeapChannelBuffer.java @@ -54,7 +54,7 @@ public class BigEndianHeapChannelBuffer extends HeapChannelBuffer { return (short) (array[index] << 8 | array[index+1] & 0xFF); } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { return (array[index] & 0xff) << 16 | (array[index+1] & 0xff) << 8 | (array[index+2] & 0xff) << 0; diff --git a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java index 22794f8e86..b2cfe8c949 100644 --- a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java @@ -71,7 +71,7 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer { return buffer.getShort(index); } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { return (getByte(index) & 0xff) << 16 | (getByte(index+1) & 0xff) << 8 | (getByte(index+2) & 0xff) << 0; diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java index 537cf5eabe..ddcc1be43a 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java @@ -410,6 +410,16 @@ public interface ChannelBuffer extends Comparable { */ byte getByte(int index); + /** + * Gets a unsigned byte at the specified absolute {@code index} in this + * buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 1} is greater than {@code capacity} + */ + short getUnsignedByte(int index); + /** * Gets a 16-bit short integer at the specified absolute {@code index} in * this buffer. @@ -420,6 +430,16 @@ public interface ChannelBuffer extends Comparable { */ short getShort(int index); + /** + * Gets a unsigned 16-bit short integer at the specified absolute + * {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code capacity} + */ + int getUnsignedShort(int index); + /** * Gets a 24-bit medium integer at the specified absolute {@code index} in * this buffer. @@ -430,6 +450,16 @@ public interface ChannelBuffer extends Comparable { */ int getMedium(int index); + /** + * Gets a unsigned 24-bit medium integer at the specified absolute + * {@code index} in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code capacity} + */ + int getUnsignedMedium(int index); + /** * Gets a 32-bit integer at the specified absolute {@code index} in * this buffer. @@ -440,6 +470,16 @@ public interface ChannelBuffer extends Comparable { */ int getInt(int index); + /** + * Gets a unsigned 32-bit integer at the specified absolute {@code index} + * in this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code capacity} + */ + long getUnsignedInt(int index); + /** * Gets a 64-bit long integer at the specified absolute {@code index} in * this buffer. @@ -597,9 +637,13 @@ public interface ChannelBuffer extends Comparable { void setZero(int index, int length); byte readByte(); + short readUnsignedByte(); short readShort(); + int readUnsignedShort(); int readMedium(); + int readUnsignedMedium(); int readInt(); + long readUnsignedInt(); long readLong(); ChannelBuffer readBytes(int length); diff --git a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java index 0c0ac09f56..30dbfae669 100644 --- a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java @@ -99,10 +99,10 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { } } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { int sliceId = sliceId(index); if (index + 3 <= indices[sliceId + 1]) { - return slices[sliceId].getMedium(index - indices[sliceId]); + return slices[sliceId].getUnsignedMedium(index - indices[sliceId]); } else if (order() == ByteOrder.BIG_ENDIAN) { return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff; } else { diff --git a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java index 520a620c24..ffb31c98bd 100644 --- a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java @@ -75,8 +75,8 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr return buffer.getShort(index); } - public int getMedium(int index) { - return buffer.getMedium(index); + public int getUnsignedMedium(int index) { + return buffer.getUnsignedMedium(index); } public int getInt(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java index 8ea8f7e61c..29be9a0b45 100644 --- a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java @@ -76,8 +76,8 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { return buffer.getShort(index); } - public int getMedium(int index) { - return buffer.getMedium(index); + public int getUnsignedMedium(int index) { + return buffer.getUnsignedMedium(index); } public int getInt(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/LittleEndianHeapChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/LittleEndianHeapChannelBuffer.java index fbd840e9e5..6de5831c9b 100644 --- a/src/main/java/org/jboss/netty/buffer/LittleEndianHeapChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/LittleEndianHeapChannelBuffer.java @@ -54,7 +54,7 @@ public class LittleEndianHeapChannelBuffer extends HeapChannelBuffer { return (short) (array[index] & 0xFF | array[index+1] << 8); } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { return (array[index ] & 0xff) << 0 | (array[index+1] & 0xff) << 8 | (array[index+2] & 0xff) << 16; diff --git a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java index 629dbfdafd..85d703967e 100644 --- a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java @@ -149,8 +149,8 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap return buffer.getShort(index); } - public int getMedium(int index) { - return buffer.getMedium(index); + public int getUnsignedMedium(int index) { + return buffer.getUnsignedMedium(index); } public int getInt(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java index cab48c3968..194da48ece 100644 --- a/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java @@ -81,9 +81,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe return buffer.getShort(index + adjustment); } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { checkIndex(index, 3); - return buffer.getMedium(index + adjustment); + return buffer.getUnsignedMedium(index + adjustment); } public int getInt(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java index 9f0c4687f7..6b38a92ec9 100644 --- a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java @@ -75,9 +75,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra return buffer.getShort(index); } - public int getMedium(int index) { + public int getUnsignedMedium(int index) { checkIndex(index, 3); - return buffer.getMedium(index); + return buffer.getUnsignedMedium(index); } public int getInt(int index) { diff --git a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java index 6108795bd9..f776bacf09 100644 --- a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java +++ b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java @@ -93,6 +93,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.getByte(index); } + public short getUnsignedByte(int index) { + checkIndex(index); + return buffer.getUnsignedByte(index); + } + public void getBytes(int index, byte[] dst, int dstIndex, int length) { checkIndex(index, length); buffer.getBytes(index, dst, dstIndex, length); @@ -130,10 +135,15 @@ class ReplayingDecoderBuffer implements ChannelBuffer { } public int getInt(int index) { - checkIndex(index); + checkIndex(index, 4); return buffer.getInt(index); } + public long getUnsignedInt(int index) { + checkIndex(index, 4); + return buffer.getUnsignedInt(index); + } + public long getLong(int index) { checkIndex(index, 8); return buffer.getLong(index); @@ -144,11 +154,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.getMedium(index); } + public int getUnsignedMedium(int index) { + checkIndex(index, 3); + return buffer.getUnsignedMedium(index); + } + public short getShort(int index) { checkIndex(index, 2); return buffer.getShort(index); } + public int getUnsignedShort(int index) { + checkIndex(index, 2); + return buffer.getUnsignedShort(index); + } + @Override public int hashCode() { reject(); @@ -197,6 +217,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.readByte(); } + public short readUnsignedByte() { + checkReadableBytes(1); + return buffer.readUnsignedByte(); + } + public void readBytes(byte[] dst, int dstIndex, int length) { checkReadableBytes(length); buffer.readBytes(dst, dstIndex, length); @@ -277,6 +302,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.readInt(); } + public long readUnsignedInt() { + checkReadableBytes(4); + return buffer.readUnsignedInt(); + } + public long readLong() { checkReadableBytes(8); return buffer.readLong(); @@ -287,11 +317,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.readMedium(); } + public int readUnsignedMedium() { + checkReadableBytes(3); + return buffer.readUnsignedMedium(); + } + public short readShort() { checkReadableBytes(2); return buffer.readShort(); } + public int readUnsignedShort() { + checkReadableBytes(2); + return buffer.readUnsignedShort(); + } + public void resetReaderIndex() { buffer.resetReaderIndex(); } diff --git a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java index 4ed645383f..d02abbb876 100644 --- a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java @@ -324,7 +324,7 @@ public abstract class AbstractChannelBufferTest { random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 2; i += 3) { int value = random.nextInt() & 0x00FFFFFF; - assertEquals(value, buffer.getMedium(i)); + assertEquals(value, buffer.getUnsignedMedium(i)); } } @@ -431,7 +431,7 @@ public abstract class AbstractChannelBufferTest { int value = random.nextInt() & 0x00FFFFFF; assertEquals(i, buffer.readerIndex()); assertTrue(buffer.readable()); - assertEquals(value, buffer.readMedium()); + assertEquals(value, buffer.readUnsignedMedium()); } assertEquals(buffer.capacity() / 3 * 3, buffer.readerIndex());