Added unsigned integer access methods
This commit is contained in:
parent
289e84d69c
commit
19fcbac575
@ -122,6 +122,26 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
readerIndex = 0;
|
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) {
|
public void getBytes(int index, byte[] dst) {
|
||||||
getBytes(index, dst, 0, dst.length);
|
getBytes(index, dst, 0, dst.length);
|
||||||
}
|
}
|
||||||
@ -177,6 +197,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
return getByte(readerIndex ++);
|
return getByte(readerIndex ++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public short readUnsignedByte() {
|
||||||
|
return (short) (readByte() & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
public short readShort() {
|
public short readShort() {
|
||||||
checkReadableBytes(2);
|
checkReadableBytes(2);
|
||||||
short v = getShort(readerIndex);
|
short v = getShort(readerIndex);
|
||||||
@ -184,9 +208,21 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int readUnsignedShort() {
|
||||||
|
return readShort() & 0xFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
public int readMedium() {
|
public int readMedium() {
|
||||||
|
int value = readUnsignedMedium();
|
||||||
|
if ((value & 0x800000) != 0) {
|
||||||
|
value |= 0xff000000;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int readUnsignedMedium() {
|
||||||
checkReadableBytes(3);
|
checkReadableBytes(3);
|
||||||
int v = getMedium(readerIndex);
|
int v = getUnsignedMedium(readerIndex);
|
||||||
readerIndex += 3;
|
readerIndex += 3;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@ -198,6 +234,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long readUnsignedInt() {
|
||||||
|
return readInt() & 0xFFFFFFFFL;
|
||||||
|
}
|
||||||
|
|
||||||
public long readLong() {
|
public long readLong() {
|
||||||
checkReadableBytes(8);
|
checkReadableBytes(8);
|
||||||
long v = getLong(readerIndex);
|
long v = getLong(readerIndex);
|
||||||
|
@ -54,7 +54,7 @@ public class BigEndianHeapChannelBuffer extends HeapChannelBuffer {
|
|||||||
return (short) (array[index] << 8 | array[index+1] & 0xFF);
|
return (short) (array[index] << 8 | array[index+1] & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return (array[index] & 0xff) << 16 |
|
return (array[index] & 0xff) << 16 |
|
||||||
(array[index+1] & 0xff) << 8 |
|
(array[index+1] & 0xff) << 8 |
|
||||||
(array[index+2] & 0xff) << 0;
|
(array[index+2] & 0xff) << 0;
|
||||||
|
@ -71,7 +71,7 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
|
|||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return (getByte(index) & 0xff) << 16 |
|
return (getByte(index) & 0xff) << 16 |
|
||||||
(getByte(index+1) & 0xff) << 8 |
|
(getByte(index+1) & 0xff) << 8 |
|
||||||
(getByte(index+2) & 0xff) << 0;
|
(getByte(index+2) & 0xff) << 0;
|
||||||
|
@ -410,6 +410,16 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
byte getByte(int index);
|
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
|
* Gets a 16-bit short integer at the specified absolute {@code index} in
|
||||||
* this buffer.
|
* this buffer.
|
||||||
@ -420,6 +430,16 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
short getShort(int index);
|
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
|
* Gets a 24-bit medium integer at the specified absolute {@code index} in
|
||||||
* this buffer.
|
* this buffer.
|
||||||
@ -430,6 +450,16 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
int getMedium(int index);
|
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
|
* Gets a 32-bit integer at the specified absolute {@code index} in
|
||||||
* this buffer.
|
* this buffer.
|
||||||
@ -440,6 +470,16 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
*/
|
*/
|
||||||
int getInt(int index);
|
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
|
* Gets a 64-bit long integer at the specified absolute {@code index} in
|
||||||
* this buffer.
|
* this buffer.
|
||||||
@ -597,9 +637,13 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
|
|||||||
void setZero(int index, int length);
|
void setZero(int index, int length);
|
||||||
|
|
||||||
byte readByte();
|
byte readByte();
|
||||||
|
short readUnsignedByte();
|
||||||
short readShort();
|
short readShort();
|
||||||
|
int readUnsignedShort();
|
||||||
int readMedium();
|
int readMedium();
|
||||||
|
int readUnsignedMedium();
|
||||||
int readInt();
|
int readInt();
|
||||||
|
long readUnsignedInt();
|
||||||
long readLong();
|
long readLong();
|
||||||
|
|
||||||
ChannelBuffer readBytes(int length);
|
ChannelBuffer readBytes(int length);
|
||||||
|
@ -99,10 +99,10 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
int sliceId = sliceId(index);
|
int sliceId = sliceId(index);
|
||||||
if (index + 3 <= indices[sliceId + 1]) {
|
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) {
|
} else if (order() == ByteOrder.BIG_ENDIAN) {
|
||||||
return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff;
|
return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff;
|
||||||
} else {
|
} else {
|
||||||
|
@ -75,8 +75,8 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr
|
|||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return buffer.getMedium(index);
|
return buffer.getUnsignedMedium(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
|
@ -76,8 +76,8 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
|
|||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return buffer.getMedium(index);
|
return buffer.getUnsignedMedium(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
|
@ -54,7 +54,7 @@ public class LittleEndianHeapChannelBuffer extends HeapChannelBuffer {
|
|||||||
return (short) (array[index] & 0xFF | array[index+1] << 8);
|
return (short) (array[index] & 0xFF | array[index+1] << 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return (array[index ] & 0xff) << 0 |
|
return (array[index ] & 0xff) << 0 |
|
||||||
(array[index+1] & 0xff) << 8 |
|
(array[index+1] & 0xff) << 8 |
|
||||||
(array[index+2] & 0xff) << 16;
|
(array[index+2] & 0xff) << 16;
|
||||||
|
@ -149,8 +149,8 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap
|
|||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
return buffer.getMedium(index);
|
return buffer.getUnsignedMedium(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
|
@ -81,9 +81,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
|
|||||||
return buffer.getShort(index + adjustment);
|
return buffer.getShort(index + adjustment);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
checkIndex(index, 3);
|
checkIndex(index, 3);
|
||||||
return buffer.getMedium(index + adjustment);
|
return buffer.getUnsignedMedium(index + adjustment);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
|
@ -75,9 +75,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
|
|||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMedium(int index) {
|
public int getUnsignedMedium(int index) {
|
||||||
checkIndex(index, 3);
|
checkIndex(index, 3);
|
||||||
return buffer.getMedium(index);
|
return buffer.getUnsignedMedium(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
|
@ -93,6 +93,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.getByte(index);
|
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) {
|
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||||
checkIndex(index, length);
|
checkIndex(index, length);
|
||||||
buffer.getBytes(index, dst, dstIndex, length);
|
buffer.getBytes(index, dst, dstIndex, length);
|
||||||
@ -130,10 +135,15 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getInt(int index) {
|
public int getInt(int index) {
|
||||||
checkIndex(index);
|
checkIndex(index, 4);
|
||||||
return buffer.getInt(index);
|
return buffer.getInt(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getUnsignedInt(int index) {
|
||||||
|
checkIndex(index, 4);
|
||||||
|
return buffer.getUnsignedInt(index);
|
||||||
|
}
|
||||||
|
|
||||||
public long getLong(int index) {
|
public long getLong(int index) {
|
||||||
checkIndex(index, 8);
|
checkIndex(index, 8);
|
||||||
return buffer.getLong(index);
|
return buffer.getLong(index);
|
||||||
@ -144,11 +154,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.getMedium(index);
|
return buffer.getMedium(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getUnsignedMedium(int index) {
|
||||||
|
checkIndex(index, 3);
|
||||||
|
return buffer.getUnsignedMedium(index);
|
||||||
|
}
|
||||||
|
|
||||||
public short getShort(int index) {
|
public short getShort(int index) {
|
||||||
checkIndex(index, 2);
|
checkIndex(index, 2);
|
||||||
return buffer.getShort(index);
|
return buffer.getShort(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getUnsignedShort(int index) {
|
||||||
|
checkIndex(index, 2);
|
||||||
|
return buffer.getUnsignedShort(index);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
reject();
|
reject();
|
||||||
@ -197,6 +217,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.readByte();
|
return buffer.readByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public short readUnsignedByte() {
|
||||||
|
checkReadableBytes(1);
|
||||||
|
return buffer.readUnsignedByte();
|
||||||
|
}
|
||||||
|
|
||||||
public void readBytes(byte[] dst, int dstIndex, int length) {
|
public void readBytes(byte[] dst, int dstIndex, int length) {
|
||||||
checkReadableBytes(length);
|
checkReadableBytes(length);
|
||||||
buffer.readBytes(dst, dstIndex, length);
|
buffer.readBytes(dst, dstIndex, length);
|
||||||
@ -277,6 +302,11 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.readInt();
|
return buffer.readInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long readUnsignedInt() {
|
||||||
|
checkReadableBytes(4);
|
||||||
|
return buffer.readUnsignedInt();
|
||||||
|
}
|
||||||
|
|
||||||
public long readLong() {
|
public long readLong() {
|
||||||
checkReadableBytes(8);
|
checkReadableBytes(8);
|
||||||
return buffer.readLong();
|
return buffer.readLong();
|
||||||
@ -287,11 +317,21 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
|
|||||||
return buffer.readMedium();
|
return buffer.readMedium();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int readUnsignedMedium() {
|
||||||
|
checkReadableBytes(3);
|
||||||
|
return buffer.readUnsignedMedium();
|
||||||
|
}
|
||||||
|
|
||||||
public short readShort() {
|
public short readShort() {
|
||||||
checkReadableBytes(2);
|
checkReadableBytes(2);
|
||||||
return buffer.readShort();
|
return buffer.readShort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int readUnsignedShort() {
|
||||||
|
checkReadableBytes(2);
|
||||||
|
return buffer.readUnsignedShort();
|
||||||
|
}
|
||||||
|
|
||||||
public void resetReaderIndex() {
|
public void resetReaderIndex() {
|
||||||
buffer.resetReaderIndex();
|
buffer.resetReaderIndex();
|
||||||
}
|
}
|
||||||
|
@ -324,7 +324,7 @@ public abstract class AbstractChannelBufferTest {
|
|||||||
random.setSeed(seed);
|
random.setSeed(seed);
|
||||||
for (int i = 0; i < buffer.capacity() - 2; i += 3) {
|
for (int i = 0; i < buffer.capacity() - 2; i += 3) {
|
||||||
int value = random.nextInt() & 0x00FFFFFF;
|
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;
|
int value = random.nextInt() & 0x00FFFFFF;
|
||||||
assertEquals(i, buffer.readerIndex());
|
assertEquals(i, buffer.readerIndex());
|
||||||
assertTrue(buffer.readable());
|
assertTrue(buffer.readable());
|
||||||
assertEquals(value, buffer.readMedium());
|
assertEquals(value, buffer.readUnsignedMedium());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(buffer.capacity() / 3 * 3, buffer.readerIndex());
|
assertEquals(buffer.capacity() / 3 * 3, buffer.readerIndex());
|
||||||
|
Loading…
Reference in New Issue
Block a user