Added unsigned integer access methods

This commit is contained in:
Trustin Lee 2008-08-10 00:52:59 +00:00
parent 289e84d69c
commit 19fcbac575
13 changed files with 143 additions and 19 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -410,6 +410,16 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
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<ChannelBuffer> {
*/
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<ChannelBuffer> {
*/
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<ChannelBuffer> {
*/
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<ChannelBuffer> {
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);

View File

@ -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 {

View File

@ -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) {

View File

@ -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) {

View File

@ -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;

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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();
}

View File

@ -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());