diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 6ede8208a3..f0258c89b9 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -348,11 +348,24 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract short _getShort(int index); + @Override + public short getShortLE(int index) { + checkIndex(index, 2); + return _getShortLE(index); + } + + protected abstract short _getShortLE(int index); + @Override public int getUnsignedShort(int index) { return getShort(index) & 0xFFFF; } + @Override + public int getUnsignedShortLE(int index) { + return getShortLE(index) & 0xFFFF; + } + @Override public int getUnsignedMedium(int index) { checkIndex(index, 3); @@ -361,6 +374,14 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract int _getUnsignedMedium(int index); + @Override + public int getUnsignedMediumLE(int index) { + checkIndex(index, 3); + return _getUnsignedMediumLE(index); + } + + protected abstract int _getUnsignedMediumLE(int index); + @Override public int getMedium(int index) { int value = getUnsignedMedium(index); @@ -370,6 +391,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return value; } + @Override + public int getMediumLE(int index) { + int value = getUnsignedMediumLE(index); + if ((value & 0x800000) != 0) { + value |= 0xff000000; + } + return value; + } + @Override public int getInt(int index) { checkIndex(index, 4); @@ -378,11 +408,24 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract int _getInt(int index); + @Override + public int getIntLE(int index) { + checkIndex(index, 4); + return _getIntLE(index); + } + + protected abstract int _getIntLE(int index); + @Override public long getUnsignedInt(int index) { return getInt(index) & 0xFFFFFFFFL; } + @Override + public long getUnsignedIntLE(int index) { + return getIntLE(index) & 0xFFFFFFFFL; + } + @Override public long getLong(int index) { checkIndex(index, 8); @@ -391,6 +434,14 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract long _getLong(int index); + @Override + public long getLongLE(int index) { + checkIndex(index, 8); + return _getLongLE(index); + } + + protected abstract long _getLongLE(int index); + @Override public char getChar(int index) { return (char) getShort(index); @@ -449,6 +500,15 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract void _setShort(int index, int value); + @Override + public ByteBuf setShortLE(int index, int value) { + checkIndex(index, 2); + _setShortLE(index, value); + return this; + } + + protected abstract void _setShortLE(int index, int value); + @Override public ByteBuf setChar(int index, int value) { setShort(index, value); @@ -464,6 +524,15 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract void _setMedium(int index, int value); + @Override + public ByteBuf setMediumLE(int index, int value) { + checkIndex(index, 3); + _setMediumLE(index, value); + return this; + } + + protected abstract void _setMediumLE(int index, int value); + @Override public ByteBuf setInt(int index, int value) { checkIndex(index, 4); @@ -473,6 +542,15 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract void _setInt(int index, int value); + @Override + public ByteBuf setIntLE(int index, int value) { + checkIndex(index, 4); + _setIntLE(index, value); + return this; + } + + protected abstract void _setIntLE(int index, int value); + @Override public ByteBuf setFloat(int index, float value) { setInt(index, Float.floatToRawIntBits(value)); @@ -488,6 +566,15 @@ public abstract class AbstractByteBuf extends ByteBuf { protected abstract void _setLong(int index, long value); + @Override + public ByteBuf setLongLE(int index, long value) { + checkIndex(index, 8); + _setLongLE(index, value); + return this; + } + + protected abstract void _setLongLE(int index, long value); + @Override public ByteBuf setDouble(int index, double value) { setLong(index, Double.doubleToRawLongBits(value)); @@ -582,11 +669,24 @@ public abstract class AbstractByteBuf extends ByteBuf { return v; } + @Override + public short readShortLE() { + checkReadableBytes0(2); + short v = _getShortLE(readerIndex); + readerIndex += 2; + return v; + } + @Override public int readUnsignedShort() { return readShort() & 0xFFFF; } + @Override + public int readUnsignedShortLE() { + return readShortLE() & 0xFFFF; + } + @Override public int readMedium() { int value = readUnsignedMedium(); @@ -596,6 +696,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return value; } + @Override + public int readMediumLE() { + int value = readUnsignedMediumLE(); + if ((value & 0x800000) != 0) { + value |= 0xff000000; + } + return value; + } + @Override public int readUnsignedMedium() { checkReadableBytes0(3); @@ -604,6 +713,14 @@ public abstract class AbstractByteBuf extends ByteBuf { return v; } + @Override + public int readUnsignedMediumLE() { + checkReadableBytes0(3); + int v = _getUnsignedMediumLE(readerIndex); + readerIndex += 3; + return v; + } + @Override public int readInt() { checkReadableBytes0(4); @@ -612,11 +729,24 @@ public abstract class AbstractByteBuf extends ByteBuf { return v; } + @Override + public int readIntLE() { + checkReadableBytes0(4); + int v = _getIntLE(readerIndex); + readerIndex += 4; + return v; + } + @Override public long readUnsignedInt() { return readInt() & 0xFFFFFFFFL; } + @Override + public long readUnsignedIntLE() { + return readIntLE() & 0xFFFFFFFFL; + } + @Override public long readLong() { checkReadableBytes0(8); @@ -625,6 +755,14 @@ public abstract class AbstractByteBuf extends ByteBuf { return v; } + @Override + public long readLongLE() { + checkReadableBytes0(8); + long v = _getLongLE(readerIndex); + readerIndex += 8; + return v; + } + @Override public char readChar() { return (char) readShort(); @@ -756,6 +894,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeShortLE(int value) { + ensureAccessible(); + ensureWritable0(2); + _setShortLE(writerIndex, value); + writerIndex += 2; + return this; + } + @Override public ByteBuf writeMedium(int value) { ensureAccessible(); @@ -765,6 +912,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeMediumLE(int value) { + ensureAccessible(); + ensureWritable0(3); + _setMediumLE(writerIndex, value); + writerIndex += 3; + return this; + } + @Override public ByteBuf writeInt(int value) { ensureAccessible(); @@ -774,6 +930,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeIntLE(int value) { + ensureAccessible(); + ensureWritable0(4); + _setIntLE(writerIndex, value); + writerIndex += 4; + return this; + } + @Override public ByteBuf writeLong(long value) { ensureAccessible(); @@ -783,6 +948,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeLongLE(long value) { + ensureAccessible(); + ensureWritable0(8); + _setLongLE(writerIndex, value); + writerIndex += 8; + return this; + } + @Override public ByteBuf writeChar(int value) { writeShort(value); diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index addb338469..886243d130 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -262,6 +262,9 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { /** * Returns the endianness * of this buffer. + * + * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} + * instead of creating a buffer with swapped {@code endianness}. */ public abstract ByteOrder order(); @@ -272,6 +275,9 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { * specified {@code endianness} is identical to this buffer's byte order, this method can * return {@code this}. This method does not modify {@code readerIndex} or {@code writerIndex} * of this buffer. + * + * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} + * instead of creating a buffer with swapped {@code endianness}. */ public abstract ByteBuf order(ByteOrder endianness); @@ -557,6 +563,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract short getShort(int index); + /** + * Gets a 16-bit short integer at the specified absolute {@code index} in + * this buffer in Little Endian Byte Order. This method does not modify + * {@code readerIndex} or {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code this.capacity} + */ + public abstract short getShortLE(int index); + /** * Gets an unsigned 16-bit short integer at the specified absolute * {@code index} in this buffer. This method does not modify @@ -568,6 +585,18 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int getUnsignedShort(int index); + /** + * Gets an unsigned 16-bit short integer at the specified absolute + * {@code index} in this buffer in Little Endian Byte Order. + * This method does not modify {@code readerIndex} or + * {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code this.capacity} + */ + public abstract int getUnsignedShortLE(int index); + /** * Gets a 24-bit medium integer at the specified absolute {@code index} in * this buffer. This method does not modify {@code readerIndex} or @@ -579,6 +608,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int getMedium(int index); + /** + * Gets a 24-bit medium integer at the specified absolute {@code index} in + * this buffer in the Little Endian Byte Order. This method does not + * modify {@code readerIndex} or {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code this.capacity} + */ + public abstract int getMediumLE(int index); + /** * Gets an unsigned 24-bit medium integer at the specified absolute * {@code index} in this buffer. This method does not modify @@ -590,6 +630,18 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int getUnsignedMedium(int index); + /** + * Gets an unsigned 24-bit medium integer at the specified absolute + * {@code index} in this buffer in Little Endian Byte Order. + * This method does not modify {@code readerIndex} or + * {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code this.capacity} + */ + public abstract int getUnsignedMediumLE(int index); + /** * Gets a 32-bit integer at the specified absolute {@code index} in * this buffer. This method does not modify {@code readerIndex} or @@ -601,6 +653,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int getInt(int index); + /** + * Gets a 32-bit integer at the specified absolute {@code index} in + * this buffer with Little Endian Byte Order. This method does not + * modify {@code readerIndex} or {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code this.capacity} + */ + public abstract int getIntLE(int index); + /** * Gets an unsigned 32-bit integer at the specified absolute {@code index} * in this buffer. This method does not modify {@code readerIndex} or @@ -612,6 +675,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract long getUnsignedInt(int index); + /** + * Gets an unsigned 32-bit integer at the specified absolute {@code index} + * in this buffer in Little Endian Byte Order. This method does not + * modify {@code readerIndex} or {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code this.capacity} + */ + public abstract long getUnsignedIntLE(int index); + /** * Gets a 64-bit long integer at the specified absolute {@code index} in * this buffer. This method does not modify {@code readerIndex} or @@ -623,6 +697,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract long getLong(int index); + /** + * Gets a 64-bit long integer at the specified absolute {@code index} in + * this buffer in Little Endian Byte Order. This method does not + * modify {@code readerIndex} or {@code writerIndex} of this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 8} is greater than {@code this.capacity} + */ + public abstract long getLongLE(int index); + /** * Gets a 2-byte UTF-16 character at the specified absolute * {@code index} in this buffer. This method does not modify @@ -832,6 +917,19 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf setShort(int index, int value); + /** + * Sets the specified 16-bit short integer at the specified absolute + * {@code index} in this buffer with the Little Endian Byte Order. + * The 16 high-order bits of the specified value are ignored. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 2} is greater than {@code this.capacity} + */ + public abstract ByteBuf setShortLE(int index, int value); + /** * Sets the specified 24-bit medium integer at the specified absolute * {@code index} in this buffer. Please note that the most significant @@ -845,6 +943,20 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf setMedium(int index, int value); + /** + * Sets the specified 24-bit medium integer at the specified absolute + * {@code index} in this buffer in the Little Endian Byte Order. + * Please note that the most significant byte is ignored in the + * specified value. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 3} is greater than {@code this.capacity} + */ + public abstract ByteBuf setMediumLE(int index, int value); + /** * Sets the specified 32-bit integer at the specified absolute * {@code index} in this buffer. @@ -857,6 +969,19 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf setInt(int index, int value); + /** + * Sets the specified 32-bit integer at the specified absolute + * {@code index} in this buffer with Little Endian byte order + * . + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 4} is greater than {@code this.capacity} + */ + public abstract ByteBuf setIntLE(int index, int value); + /** * Sets the specified 64-bit long integer at the specified absolute * {@code index} in this buffer. @@ -869,6 +994,18 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf setLong(int index, long value); + /** + * Sets the specified 64-bit long integer at the specified absolute + * {@code index} in this buffer in Little Endian Byte Order. + * This method does not modify {@code readerIndex} or {@code writerIndex} of + * this buffer. + * + * @throws IndexOutOfBoundsException + * if the specified {@code index} is less than {@code 0} or + * {@code index + 8} is greater than {@code this.capacity} + */ + public abstract ByteBuf setLongLE(int index, long value); + /** * Sets the specified 2-byte UTF-16 character at the specified absolute * {@code index} in this buffer. @@ -1093,6 +1230,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract short readShort(); + /** + * Gets a 16-bit short integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 2} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 2} + */ + public abstract short readShortLE(); + /** * Gets an unsigned 16-bit short integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 2} in this buffer. @@ -1102,6 +1249,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int readUnsignedShort(); + /** + * Gets an unsigned 16-bit short integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 2} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 2} + */ + public abstract int readUnsignedShortLE(); + /** * Gets a 24-bit medium integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 3} in this buffer. @@ -1111,6 +1268,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int readMedium(); + /** + * Gets a 24-bit medium integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the + * {@code readerIndex} by {@code 3} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 3} + */ + public abstract int readMediumLE(); + /** * Gets an unsigned 24-bit medium integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 3} in this buffer. @@ -1120,6 +1287,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int readUnsignedMedium(); + /** + * Gets an unsigned 24-bit medium integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 3} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 3} + */ + public abstract int readUnsignedMediumLE(); + /** * Gets a 32-bit integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 4} in this buffer. @@ -1129,6 +1306,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract int readInt(); + /** + * Gets a 32-bit integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 4} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 4} + */ + public abstract int readIntLE(); + /** * Gets an unsigned 32-bit integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 4} in this buffer. @@ -1138,6 +1325,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract long readUnsignedInt(); + /** + * Gets an unsigned 32-bit integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 4} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 4} + */ + public abstract long readUnsignedIntLE(); + /** * Gets a 64-bit integer at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 8} in this buffer. @@ -1147,6 +1344,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract long readLong(); + /** + * Gets a 64-bit integer at the current {@code readerIndex} + * in the Little Endian Byte Order and increases the {@code readerIndex} + * by {@code 8} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.readableBytes} is less than {@code 8} + */ + public abstract long readLongLE(); + /** * Gets a 2-byte UTF-16 character at the current {@code readerIndex} * and increases the {@code readerIndex} by {@code 2} in this buffer. @@ -1357,6 +1564,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf writeShort(int value); + /** + * Sets the specified 16-bit short integer in the Little Endian Byte + * Order at the current {@code writerIndex} and increases the + * {@code writerIndex} by {@code 2} in this buffer. + * The 16 high-order bits of the specified value are ignored. + * + * @throws IndexOutOfBoundsException + * if {@code this.writableBytes} is less than {@code 2} + */ + public abstract ByteBuf writeShortLE(int value); + /** * Sets the specified 24-bit medium integer at the current * {@code writerIndex} and increases the {@code writerIndex} by {@code 3} @@ -1367,6 +1585,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf writeMedium(int value); + /** + * Sets the specified 24-bit medium integer at the current + * {@code writerIndex} in the Little Endian Byte Order and + * increases the {@code writerIndex} by {@code 3} in this + * buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.writableBytes} is less than {@code 3} + */ + public abstract ByteBuf writeMediumLE(int value); + /** * Sets the specified 32-bit integer at the current {@code writerIndex} * and increases the {@code writerIndex} by {@code 4} in this buffer. @@ -1376,6 +1605,16 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf writeInt(int value); + /** + * Sets the specified 32-bit integer at the current {@code writerIndex} + * in the Little Endian Byte Order and increases the {@code writerIndex} + * by {@code 4} in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.writableBytes} is less than {@code 4} + */ + public abstract ByteBuf writeIntLE(int value); + /** * Sets the specified 64-bit long integer at the current * {@code writerIndex} and increases the {@code writerIndex} by {@code 8} @@ -1386,6 +1625,17 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { */ public abstract ByteBuf writeLong(long value); + /** + * Sets the specified 64-bit long integer at the current + * {@code writerIndex} in the Little Endian Byte Order and + * increases the {@code writerIndex} by {@code 8} + * in this buffer. + * + * @throws IndexOutOfBoundsException + * if {@code this.writableBytes} is less than {@code 8} + */ + public abstract ByteBuf writeLongLE(long value); + /** * Sets the specified 2-byte UTF-16 character at the current * {@code writerIndex} and increases the {@code writerIndex} by {@code 2} diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index e18e028e47..d48a4b475e 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -635,6 +635,18 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected short _getShortLE(int index) { + Component c = findComponent(index); + if (index + 2 <= c.endOffset) { + return c.buf.getShortLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8); + } else { + return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff); + } + } + @Override protected int _getUnsignedMedium(int index) { Component c = findComponent(index); @@ -647,6 +659,18 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected int _getUnsignedMediumLE(int index) { + Component c = findComponent(index); + if (index + 3 <= c.endOffset) { + return c.buf.getUnsignedMediumLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16; + } else { + return (_getShortLE(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff; + } + } + @Override protected int _getInt(int index) { Component c = findComponent(index); @@ -659,6 +683,18 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected int _getIntLE(int index) { + Component c = findComponent(index); + if (index + 4 <= c.endOffset) { + return c.buf.getIntLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16; + } else { + return (_getShortLE(index) & 0xffff) << 16 | _getShortLE(index + 2) & 0xffff; + } + } + @Override protected long _getLong(int index) { Component c = findComponent(index); @@ -671,6 +707,18 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected long _getLongLE(int index) { + Component c = findComponent(index); + if (index + 8 <= c.endOffset) { + return c.buf.getLongLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32; + } else { + return (_getIntLE(index) & 0xffffffffL) << 32 | _getIntLE(index + 4) & 0xffffffffL; + } + } + @Override public CompositeByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.length); @@ -812,6 +860,20 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected void _setShortLE(int index, int value) { + Component c = findComponent(index); + if (index + 2 <= c.endOffset) { + c.buf.setShortLE(index - c.offset, value); + } else if (order() == ByteOrder.BIG_ENDIAN) { + _setByte(index, (byte) value); + _setByte(index + 1, (byte) (value >>> 8)); + } else { + _setByte(index, (byte) (value >>> 8)); + _setByte(index + 1, (byte) value); + } + } + @Override public CompositeByteBuf setMedium(int index, int value) { return (CompositeByteBuf) super.setMedium(index, value); @@ -831,6 +893,20 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected void _setMediumLE(int index, int value) { + Component c = findComponent(index); + if (index + 3 <= c.endOffset) { + c.buf.setMediumLE(index - c.offset, value); + } else if (order() == ByteOrder.BIG_ENDIAN) { + _setShortLE(index, (short) value); + _setByte(index + 2, (byte) (value >>> 16)); + } else { + _setShortLE(index, (short) (value >> 8)); + _setByte(index + 2, (byte) value); + } + } + @Override public CompositeByteBuf setInt(int index, int value) { return (CompositeByteBuf) super.setInt(index, value); @@ -850,6 +926,20 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected void _setIntLE(int index, int value) { + Component c = findComponent(index); + if (index + 4 <= c.endOffset) { + c.buf.setIntLE(index - c.offset, value); + } else if (order() == ByteOrder.BIG_ENDIAN) { + _setShortLE(index, (short) value); + _setShortLE(index + 2, (short) (value >>> 16)); + } else { + _setShortLE(index, (short) (value >>> 16)); + _setShortLE(index + 2, (short) value); + } + } + @Override public CompositeByteBuf setLong(int index, long value) { return (CompositeByteBuf) super.setLong(index, value); @@ -869,6 +959,20 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements } } + @Override + protected void _setLongLE(int index, long value) { + Component c = findComponent(index); + if (index + 8 <= c.endOffset) { + c.buf.setLongLE(index - c.offset, value); + } else if (order() == ByteOrder.BIG_ENDIAN) { + _setIntLE(index, (int) value); + _setIntLE(index + 4, (int) (value >>> 32)); + } else { + _setIntLE(index, (int) (value >>> 32)); + _setIntLE(index + 4, (int) value); + } + } + @Override public CompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.length); diff --git a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java index 468e349cbb..4f878e6ec5 100644 --- a/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java @@ -125,6 +125,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { return buffer.getShort(index); } + @Override + protected short _getShortLE(int index) { + return buffer.getShortLE(index); + } + @Override public int getUnsignedMedium(int index) { return buffer.getUnsignedMedium(index); @@ -135,6 +140,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { return buffer.getUnsignedMedium(index); } + @Override + protected int _getUnsignedMediumLE(int index) { + return buffer.getUnsignedMediumLE(index); + } + @Override public int getInt(int index) { return buffer.getInt(index); @@ -145,6 +155,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { return buffer.getInt(index); } + @Override + protected int _getIntLE(int index) { + return buffer.getIntLE(index); + } + @Override public long getLong(int index) { return buffer.getLong(index); @@ -155,6 +170,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { return buffer.getLong(index); } + @Override + protected long _getLongLE(int index) { + return buffer.getLongLE(index); + } + @Override public ByteBuf copy(int index, int length) { return buffer.copy(index, length); @@ -205,6 +225,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { buffer.setShort(index, value); } + @Override + protected void _setShortLE(int index, int value) { + buffer.setShortLE(index, value); + } + @Override public ByteBuf setMedium(int index, int value) { buffer.setMedium(index, value); @@ -216,6 +241,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { buffer.setMedium(index, value); } + @Override + protected void _setMediumLE(int index, int value) { + buffer.setMediumLE(index, value); + } + @Override public ByteBuf setInt(int index, int value) { buffer.setInt(index, value); @@ -227,6 +257,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { buffer.setInt(index, value); } + @Override + protected void _setIntLE(int index, int value) { + buffer.setIntLE(index, value); + } + @Override public ByteBuf setLong(int index, long value) { buffer.setLong(index, value); @@ -238,6 +273,11 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf { buffer.setLong(index, value); } + @Override + protected void _setLongLE(int index, long value) { + buffer.setLongLE(index, value); + } + @Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { buffer.setBytes(index, src, srcIndex, length); diff --git a/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java b/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java index 80facdb7fa..3f26360e82 100644 --- a/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/EmptyByteBuf.java @@ -253,36 +253,71 @@ public final class EmptyByteBuf extends ByteBuf { throw new IndexOutOfBoundsException(); } + @Override + public short getShortLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public int getUnsignedShort(int index) { throw new IndexOutOfBoundsException(); } + @Override + public int getUnsignedShortLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public int getMedium(int index) { throw new IndexOutOfBoundsException(); } + @Override + public int getMediumLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public int getUnsignedMedium(int index) { throw new IndexOutOfBoundsException(); } + @Override + public int getUnsignedMediumLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public int getInt(int index) { throw new IndexOutOfBoundsException(); } + @Override + public int getIntLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public long getUnsignedInt(int index) { throw new IndexOutOfBoundsException(); } + @Override + public long getUnsignedIntLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public long getLong(int index) { throw new IndexOutOfBoundsException(); } + @Override + public long getLongLE(int index) { + throw new IndexOutOfBoundsException(); + } + @Override public char getChar(int index) { throw new IndexOutOfBoundsException(); @@ -354,21 +389,41 @@ public final class EmptyByteBuf extends ByteBuf { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf setShortLE(int index, int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf setMedium(int index, int value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf setMediumLE(int index, int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf setInt(int index, int value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf setIntLE(int index, int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf setLong(int index, long value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf setLongLE(int index, long value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf setChar(int index, int value) { throw new IndexOutOfBoundsException(); @@ -451,36 +506,71 @@ public final class EmptyByteBuf extends ByteBuf { throw new IndexOutOfBoundsException(); } + @Override + public short readShortLE() { + throw new IndexOutOfBoundsException(); + } + @Override public int readUnsignedShort() { throw new IndexOutOfBoundsException(); } + @Override + public int readUnsignedShortLE() { + throw new IndexOutOfBoundsException(); + } + @Override public int readMedium() { throw new IndexOutOfBoundsException(); } + @Override + public int readMediumLE() { + throw new IndexOutOfBoundsException(); + } + @Override public int readUnsignedMedium() { throw new IndexOutOfBoundsException(); } + @Override + public int readUnsignedMediumLE() { + throw new IndexOutOfBoundsException(); + } + @Override public int readInt() { throw new IndexOutOfBoundsException(); } + @Override + public int readIntLE() { + throw new IndexOutOfBoundsException(); + } + @Override public long readUnsignedInt() { throw new IndexOutOfBoundsException(); } + @Override + public long readUnsignedIntLE() { + throw new IndexOutOfBoundsException(); + } + @Override public long readLong() { throw new IndexOutOfBoundsException(); } + @Override + public long readLongLE() { + throw new IndexOutOfBoundsException(); + } + @Override public char readChar() { throw new IndexOutOfBoundsException(); @@ -567,21 +657,41 @@ public final class EmptyByteBuf extends ByteBuf { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf writeShortLE(int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf writeMedium(int value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf writeMediumLE(int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf writeInt(int value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf writeIntLE(int value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf writeLong(long value) { throw new IndexOutOfBoundsException(); } + @Override + public ByteBuf writeLongLE(long value) { + throw new IndexOutOfBoundsException(); + } + @Override public ByteBuf writeChar(int value) { throw new IndexOutOfBoundsException(); diff --git a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java index af28d682d8..a17aed680a 100644 --- a/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/FixedCompositeByteBuf.java @@ -127,6 +127,11 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setMedium(int index, int value) { throw new ReadOnlyBufferException(); @@ -137,6 +142,11 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setInt(int index, int value) { throw new ReadOnlyBufferException(); @@ -147,6 +157,11 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setLong(int index, long value) { throw new ReadOnlyBufferException(); @@ -157,6 +172,11 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override public int setBytes(int index, InputStream in, int length) { throw new ReadOnlyBufferException(); @@ -265,6 +285,18 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { } } + @Override + protected short _getShortLE(int index) { + Component c = findComponent(index); + if (index + 2 <= c.endOffset) { + return c.buf.getShortLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8); + } else { + return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff); + } + } + @Override protected int _getUnsignedMedium(int index) { Component c = findComponent(index); @@ -277,6 +309,18 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { } } + @Override + protected int _getUnsignedMediumLE(int index) { + Component c = findComponent(index); + if (index + 3 <= c.endOffset) { + return c.buf.getUnsignedMediumLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16; + } else { + return (_getShortLE(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff; + } + } + @Override protected int _getInt(int index) { Component c = findComponent(index); @@ -289,6 +333,18 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { } } + @Override + protected int _getIntLE(int index) { + Component c = findComponent(index); + if (index + 4 <= c.endOffset) { + return c.buf.getIntLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getShortLE(index) & 0xFFFF | (_getShortLE(index + 2) & 0xFFFF) << 16; + } else { + return (_getShortLE(index) & 0xffff) << 16 | _getShortLE(index + 2) & 0xffff; + } + } + @Override protected long _getLong(int index) { Component c = findComponent(index); @@ -301,6 +357,18 @@ final class FixedCompositeByteBuf extends AbstractReferenceCountedByteBuf { } } + @Override + protected long _getLongLE(int index) { + Component c = findComponent(index); + if (index + 8 <= c.endOffset) { + return c.buf.getLongLE(index - c.offset); + } else if (order() == ByteOrder.BIG_ENDIAN) { + return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32; + } else { + return (_getIntLE(index) & 0xffffffffL) << 32 | _getIntLE(index + 4) & 0xffffffffL; + } + } + @Override public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.length); diff --git a/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java index aac7ebc77e..abb93cda74 100644 --- a/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/HeapByteBufUtil.java @@ -28,12 +28,22 @@ final class HeapByteBufUtil { return (short) (memory[index] << 8 | memory[index + 1] & 0xFF); } + static short getShortLE(byte[] memory, int index) { + return (short) (memory[index] & 0xff | memory[index + 1] << 8); + } + static int getUnsignedMedium(byte[] memory, int index) { return (memory[index] & 0xff) << 16 | (memory[index + 1] & 0xff) << 8 | memory[index + 2] & 0xff; } + static int getUnsignedMediumLE(byte[] memory, int index) { + return memory[index] & 0xff | + (memory[index + 1] & 0xff) << 8 | + (memory[index + 2] & 0xff) << 16; + } + static int getInt(byte[] memory, int index) { return (memory[index] & 0xff) << 24 | (memory[index + 1] & 0xff) << 16 | @@ -41,6 +51,13 @@ final class HeapByteBufUtil { memory[index + 3] & 0xff; } + static int getIntLE(byte[] memory, int index) { + return memory[index] & 0xff | + (memory[index + 1] & 0xff) << 8 | + (memory[index + 2] & 0xff) << 16 | + (memory[index + 3] & 0xff) << 24; + } + static long getLong(byte[] memory, int index) { return ((long) memory[index] & 0xff) << 56 | ((long) memory[index + 1] & 0xff) << 48 | @@ -52,6 +69,17 @@ final class HeapByteBufUtil { (long) memory[index + 7] & 0xff; } + static long getLongLE(byte[] memory, int index) { + return (long) memory[index] & 0xff | + ((long) memory[index + 1] & 0xff) << 8 | + ((long) memory[index + 2] & 0xff) << 16 | + ((long) memory[index + 3] & 0xff) << 24 | + ((long) memory[index + 4] & 0xff) << 32 | + ((long) memory[index + 5] & 0xff) << 40 | + ((long) memory[index + 6] & 0xff) << 48 | + ((long) memory[index + 7] & 0xff) << 56; + } + static void setByte(byte[] memory, int index, int value) { memory[index] = (byte) value; } @@ -61,12 +89,23 @@ final class HeapByteBufUtil { memory[index + 1] = (byte) value; } + static void setShortLE(byte[] memory, int index, int value) { + memory[index] = (byte) value; + memory[index + 1] = (byte) (value >>> 8); + } + static void setMedium(byte[] memory, int index, int value) { memory[index] = (byte) (value >>> 16); memory[index + 1] = (byte) (value >>> 8); memory[index + 2] = (byte) value; } + static void setMediumLE(byte[] memory, int index, int value) { + memory[index] = (byte) value; + memory[index + 1] = (byte) (value >>> 8); + memory[index + 2] = (byte) (value >>> 16); + } + static void setInt(byte[] memory, int index, int value) { memory[index] = (byte) (value >>> 24); memory[index + 1] = (byte) (value >>> 16); @@ -74,6 +113,13 @@ final class HeapByteBufUtil { memory[index + 3] = (byte) value; } + static void setIntLE(byte[] memory, int index, int value) { + memory[index] = (byte) value; + memory[index + 1] = (byte) (value >>> 8); + memory[index + 2] = (byte) (value >>> 16); + memory[index + 3] = (byte) (value >>> 24); + } + static void setLong(byte[] memory, int index, long value) { memory[index] = (byte) (value >>> 56); memory[index + 1] = (byte) (value >>> 48); @@ -85,5 +131,16 @@ final class HeapByteBufUtil { memory[index + 7] = (byte) value; } + static void setLongLE(byte[] memory, int index, long value) { + memory[index] = (byte) value; + memory[index + 1] = (byte) (value >>> 8); + memory[index + 2] = (byte) (value >>> 16); + memory[index + 3] = (byte) (value >>> 24); + memory[index + 4] = (byte) (value >>> 32); + memory[index + 5] = (byte) (value >>> 40); + memory[index + 6] = (byte) (value >>> 48); + memory[index + 7] = (byte) (value >>> 56); + } + private HeapByteBufUtil() { } } diff --git a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java index 94abd7f1cc..591e96946e 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledDirectByteBuf.java @@ -65,10 +65,25 @@ final class PooledDirectByteBuf extends PooledByteBuf { return memory.getShort(idx(index)); } + @Override + protected short _getShortLE(int index) { + return ByteBufUtil.swapShort(_getShort(index)); + } + @Override protected int _getUnsignedMedium(int index) { index = idx(index); - return (memory.get(index) & 0xff) << 16 | (memory.get(index + 1) & 0xff) << 8 | memory.get(index + 2) & 0xff; + return (memory.get(index) & 0xff) << 16 | + (memory.get(index + 1) & 0xff) << 8 | + memory.get(index + 2) & 0xff; + } + + @Override + protected int _getUnsignedMediumLE(int index) { + index = idx(index); + return memory.get(index) & 0xff | + (memory.get(index + 1) & 0xff) << 8 | + (memory.get(index + 2) & 0xff) << 16; } @Override @@ -76,11 +91,21 @@ final class PooledDirectByteBuf extends PooledByteBuf { return memory.getInt(idx(index)); } + @Override + protected int _getIntLE(int index) { + return ByteBufUtil.swapInt(_getInt(index)); + } + @Override protected long _getLong(int index) { return memory.getLong(idx(index)); } + @Override + protected long _getLongLE(int index) { + return ByteBufUtil.swapLong(_getLong(index)); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); @@ -226,6 +251,11 @@ final class PooledDirectByteBuf extends PooledByteBuf { memory.putShort(idx(index), (short) value); } + @Override + protected void _setShortLE(int index, int value) { + _setShort(index, ByteBufUtil.swapShort((short) value)); + } + @Override protected void _setMedium(int index, int value) { index = idx(index); @@ -234,16 +264,34 @@ final class PooledDirectByteBuf extends PooledByteBuf { memory.put(index + 2, (byte) value); } + @Override + protected void _setMediumLE(int index, int value) { + index = idx(index); + memory.put(index, (byte) value); + memory.put(index + 1, (byte) (value >>> 8)); + memory.put(index + 2, (byte) (value >>> 16)); + } + @Override protected void _setInt(int index, int value) { memory.putInt(idx(index), value); } + @Override + protected void _setIntLE(int index, int value) { + _setInt(index, ByteBufUtil.swapInt(value)); + } + @Override protected void _setLong(int index, long value) { memory.putLong(idx(index), value); } + @Override + protected void _setLongLE(int index, long value) { + _setLong(index, ByteBufUtil.swapLong(value)); + } + @Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); diff --git a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java index 93084555f4..aab6bd34fd 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledHeapByteBuf.java @@ -59,21 +59,41 @@ class PooledHeapByteBuf extends PooledByteBuf { return HeapByteBufUtil.getShort(memory, idx(index)); } + @Override + protected short _getShortLE(int index) { + return HeapByteBufUtil.getShortLE(memory, idx(index)); + } + @Override protected int _getUnsignedMedium(int index) { return HeapByteBufUtil.getUnsignedMedium(memory, idx(index)); } + @Override + protected int _getUnsignedMediumLE(int index) { + return HeapByteBufUtil.getUnsignedMediumLE(memory, idx(index)); + } + @Override protected int _getInt(int index) { return HeapByteBufUtil.getInt(memory, idx(index)); } + @Override + protected int _getIntLE(int index) { + return HeapByteBufUtil.getIntLE(memory, idx(index)); + } + @Override protected long _getLong(int index) { return HeapByteBufUtil.getLong(memory, idx(index)); } + @Override + protected long _getLongLE(int index) { + return HeapByteBufUtil.getLongLE(memory, idx(index)); + } + @Override public final ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); @@ -143,21 +163,41 @@ class PooledHeapByteBuf extends PooledByteBuf { HeapByteBufUtil.setShort(memory, idx(index), value); } + @Override + protected void _setShortLE(int index, int value) { + HeapByteBufUtil.setShortLE(memory, idx(index), value); + } + @Override protected void _setMedium(int index, int value) { HeapByteBufUtil.setMedium(memory, idx(index), value); } + @Override + protected void _setMediumLE(int index, int value) { + HeapByteBufUtil.setMediumLE(memory, idx(index), value); + } + @Override protected void _setInt(int index, int value) { HeapByteBufUtil.setInt(memory, idx(index), value); } + @Override + protected void _setIntLE(int index, int value) { + HeapByteBufUtil.setIntLE(memory, idx(index), value); + } + @Override protected void _setLong(int index, long value) { HeapByteBufUtil.setLong(memory, idx(index), value); } + @Override + protected void _setLongLE(int index, long value) { + HeapByteBufUtil.setLongLE(memory, idx(index), value); + } + @Override public final ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); diff --git a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java index fca08e40a3..1f245a68fa 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledUnsafeDirectByteBuf.java @@ -84,21 +84,41 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf { return UnsafeByteBufUtil.getShort(addr(index)); } + @Override + protected short _getShortLE(int index) { + return UnsafeByteBufUtil.getShortLE(addr(index)); + } + @Override protected int _getUnsignedMedium(int index) { return UnsafeByteBufUtil.getUnsignedMedium(addr(index)); } + @Override + protected int _getUnsignedMediumLE(int index) { + return UnsafeByteBufUtil.getUnsignedMediumLE(addr(index)); + } + @Override protected int _getInt(int index) { return UnsafeByteBufUtil.getInt(addr(index)); } + @Override + protected int _getIntLE(int index) { + return UnsafeByteBufUtil.getIntLE(addr(index)); + } + @Override protected long _getLong(int index) { return UnsafeByteBufUtil.getLong(addr(index)); } + @Override + protected long _getLongLE(int index) { + return UnsafeByteBufUtil.getLongLE(addr(index)); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { UnsafeByteBufUtil.getBytes(this, addr(index), index, dst, dstIndex, length); @@ -173,21 +193,41 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf { UnsafeByteBufUtil.setShort(addr(index), value); } + @Override + protected void _setShortLE(int index, int value) { + UnsafeByteBufUtil.setShortLE(addr(index), value); + } + @Override protected void _setMedium(int index, int value) { UnsafeByteBufUtil.setMedium(addr(index), value); } + @Override + protected void _setMediumLE(int index, int value) { + UnsafeByteBufUtil.setMediumLE(addr(index), value); + } + @Override protected void _setInt(int index, int value) { UnsafeByteBufUtil.setInt(addr(index), value); } + @Override + protected void _setIntLE(int index, int value) { + UnsafeByteBufUtil.setIntLE(addr(index), value); + } + @Override protected void _setLong(int index, long value) { UnsafeByteBufUtil.setLong(addr(index), value); } + @Override + protected void _setLongLE(int index, long value) { + UnsafeByteBufUtil.setLongLE(addr(index), value); + } + @Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { UnsafeByteBufUtil.setBytes(this, addr(index), index, src, srcIndex, length); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java index b6e46ab64c..0c60511aef 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBuf.java @@ -141,6 +141,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setMedium(int index, int value) { throw new ReadOnlyBufferException(); @@ -151,6 +156,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setInt(int index, int value) { throw new ReadOnlyBufferException(); @@ -161,6 +171,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override public ByteBuf setLong(int index, long value) { throw new ReadOnlyBufferException(); @@ -171,6 +186,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override public int setBytes(int index, InputStream in, int length) { throw new ReadOnlyBufferException(); @@ -247,6 +267,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { return buffer.getShort(index); } + @Override + protected short _getShortLE(int index) { + return buffer.getShortLE(index); + } + @Override public int getUnsignedMedium(int index) { return _getUnsignedMedium(index); @@ -257,6 +282,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { return buffer.getUnsignedMedium(index); } + @Override + protected int _getUnsignedMediumLE(int index) { + return buffer.getUnsignedMediumLE(index); + } + @Override public int getInt(int index) { return _getInt(index); @@ -267,6 +297,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { return buffer.getInt(index); } + @Override + protected int _getIntLE(int index) { + return buffer.getIntLE(index); + } + @Override public long getLong(int index) { return _getLong(index); @@ -277,6 +312,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf { return buffer.getLong(index); } + @Override + protected long _getLongLE(int index) { + return buffer.getLongLE(index); + } + @Override public int nioBufferCount() { return buffer.nioBufferCount(); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index a4643f6248..3cf1dedae9 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -72,6 +72,11 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { return buffer.getShort(index); } + @Override + protected short _getShortLE(int index) { + return ByteBufUtil.swapShort(buffer.getShort(index)); + } + @Override public int getUnsignedMedium(int index) { ensureAccessible(); @@ -80,7 +85,16 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { @Override protected int _getUnsignedMedium(int index) { - return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff; + return (getByte(index) & 0xff) << 16 | + (getByte(index + 1) & 0xff) << 8 | + getByte(index + 2) & 0xff; + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return getByte(index) & 0xff | + (getByte(index + 1) & 0xff) << 8 | + (getByte(index + 2) & 0xff) << 16; } @Override @@ -94,6 +108,11 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { return buffer.getInt(index); } + @Override + protected int _getIntLE(int index) { + return ByteBufUtil.swapInt(buffer.getInt(index)); + } + @Override public long getLong(int index) { ensureAccessible(); @@ -105,6 +124,11 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { return buffer.getLong(index); } + @Override + protected long _getLongLE(int index) { + return ByteBufUtil.swapLong(buffer.getLong(index)); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); @@ -161,21 +185,41 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { throw new ReadOnlyBufferException(); } + @Override + protected void _setShortLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setMedium(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + protected void _setMediumLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setInt(int index, int value) { throw new ReadOnlyBufferException(); } + @Override + protected void _setIntLE(int index, int value) { + throw new ReadOnlyBufferException(); + } + @Override protected void _setLong(int index, long value) { throw new ReadOnlyBufferException(); } + @Override + protected void _setLongLE(int index, long value) { + throw new ReadOnlyBufferException(); + } + @Override public int capacity() { return maxCapacity(); diff --git a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java index 78db1eb9b8..3f69052453 100644 --- a/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SlicedByteBuf.java @@ -124,21 +124,41 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf { return buffer.getShort(idx(index)); } + @Override + protected short _getShortLE(int index) { + return buffer.getShortLE(idx(index)); + } + @Override protected int _getUnsignedMedium(int index) { return buffer.getUnsignedMedium(idx(index)); } + @Override + protected int _getUnsignedMediumLE(int index) { + return buffer.getUnsignedMediumLE(idx(index)); + } + @Override protected int _getInt(int index) { return buffer.getInt(idx(index)); } + @Override + protected int _getIntLE(int index) { + return buffer.getIntLE(idx(index)); + } + @Override protected long _getLong(int index) { return buffer.getLong(idx(index)); } + @Override + protected long _getLongLE(int index) { + return buffer.getLongLE(idx(index)); + } + @Override public ByteBuf duplicate() { ByteBuf duplicate = buffer.slice(adjustment, length); @@ -189,21 +209,41 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf { buffer.setShort(idx(index), value); } + @Override + protected void _setShortLE(int index, int value) { + buffer.setShortLE(idx(index), value); + } + @Override protected void _setMedium(int index, int value) { buffer.setMedium(idx(index), value); } + @Override + protected void _setMediumLE(int index, int value) { + buffer.setMediumLE(idx(index), value); + } + @Override protected void _setInt(int index, int value) { buffer.setInt(idx(index), value); } + @Override + protected void _setIntLE(int index, int value) { + buffer.setIntLE(idx(index), value); + } + @Override protected void _setLong(int index, long value) { buffer.setLong(idx(index), value); } + @Override + protected void _setLongLE(int index, long value) { + buffer.setLongLE(idx(index), value); + } + @Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { checkIndex0(index, length); diff --git a/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java index df257b45c9..597c8fda86 100644 --- a/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java @@ -28,6 +28,9 @@ import java.nio.charset.Charset; /** * Wrapper which swap the {@link ByteOrder} of a {@link ByteBuf}. + * + * @deprecated use the Little Endian accessors, e.g. {@code getShortLE}, {@code getIntLE} + * instead. */ public class SwappedByteBuf extends ByteBuf { @@ -229,36 +232,71 @@ public class SwappedByteBuf extends ByteBuf { return ByteBufUtil.swapShort(buf.getShort(index)); } + @Override + public short getShortLE(int index) { + return buf.getShort(index); + } + @Override public int getUnsignedShort(int index) { return getShort(index) & 0xFFFF; } + @Override + public int getUnsignedShortLE(int index) { + return getShortLE(index) & 0xFFFF; + } + @Override public int getMedium(int index) { return ByteBufUtil.swapMedium(buf.getMedium(index)); } + @Override + public int getMediumLE(int index) { + return buf.getMedium(index); + } + @Override public int getUnsignedMedium(int index) { return getMedium(index) & 0xFFFFFF; } + @Override + public int getUnsignedMediumLE(int index) { + return getMediumLE(index) & 0xFFFFFF; + } + @Override public int getInt(int index) { return ByteBufUtil.swapInt(buf.getInt(index)); } + @Override + public int getIntLE(int index) { + return buf.getInt(index); + } + @Override public long getUnsignedInt(int index) { return getInt(index) & 0xFFFFFFFFL; } + @Override + public long getUnsignedIntLE(int index) { + return getIntLE(index) & 0xFFFFFFFFL; + } + @Override public long getLong(int index) { return ByteBufUtil.swapLong(buf.getLong(index)); } + @Override + public long getLongLE(int index) { + return buf.getLong(index); + } + @Override public char getChar(int index) { return (char) getShort(index); @@ -339,24 +377,48 @@ public class SwappedByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf setShortLE(int index, int value) { + buf.setShort(index, (short) value); + return this; + } + @Override public ByteBuf setMedium(int index, int value) { buf.setMedium(index, ByteBufUtil.swapMedium(value)); return this; } + @Override + public ByteBuf setMediumLE(int index, int value) { + buf.setMedium(index, value); + return this; + } + @Override public ByteBuf setInt(int index, int value) { buf.setInt(index, ByteBufUtil.swapInt(value)); return this; } + @Override + public ByteBuf setIntLE(int index, int value) { + buf.setInt(index, value); + return this; + } + @Override public ByteBuf setLong(int index, long value) { buf.setLong(index, ByteBufUtil.swapLong(value)); return this; } + @Override + public ByteBuf setLongLE(int index, long value) { + buf.setLong(index, value); + return this; + } + @Override public ByteBuf setChar(int index, int value) { setShort(index, value); @@ -447,36 +509,71 @@ public class SwappedByteBuf extends ByteBuf { return ByteBufUtil.swapShort(buf.readShort()); } + @Override + public short readShortLE() { + return buf.readShort(); + } + @Override public int readUnsignedShort() { return readShort() & 0xFFFF; } + @Override + public int readUnsignedShortLE() { + return readShortLE() & 0xFFFF; + } + @Override public int readMedium() { return ByteBufUtil.swapMedium(buf.readMedium()); } + @Override + public int readMediumLE() { + return buf.readMedium(); + } + @Override public int readUnsignedMedium() { return readMedium() & 0xFFFFFF; } + @Override + public int readUnsignedMediumLE() { + return readMediumLE() & 0xFFFFFF; + } + @Override public int readInt() { return ByteBufUtil.swapInt(buf.readInt()); } + @Override + public int readIntLE() { + return buf.readInt(); + } + @Override public long readUnsignedInt() { return readInt() & 0xFFFFFFFFL; } + @Override + public long readUnsignedIntLE() { + return readIntLE() & 0xFFFFFFFFL; + } + @Override public long readLong() { return ByteBufUtil.swapLong(buf.readLong()); } + @Override + public long readLongLE() { + return buf.readLong(); + } + @Override public char readChar() { return (char) readShort(); @@ -573,24 +670,48 @@ public class SwappedByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeShortLE(int value) { + buf.writeShort((short) value); + return this; + } + @Override public ByteBuf writeMedium(int value) { buf.writeMedium(ByteBufUtil.swapMedium(value)); return this; } + @Override + public ByteBuf writeMediumLE(int value) { + buf.writeMedium(value); + return this; + } + @Override public ByteBuf writeInt(int value) { buf.writeInt(ByteBufUtil.swapInt(value)); return this; } + @Override + public ByteBuf writeIntLE(int value) { + buf.writeInt(value); + return this; + } + @Override public ByteBuf writeLong(long value) { buf.writeLong(ByteBufUtil.swapLong(value)); return this; } + @Override + public ByteBuf writeLongLE(long value) { + buf.writeLong(value); + return this; + } + @Override public ByteBuf writeChar(int value) { writeShort(value); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java index de95ea4eb1..c0613ca2c9 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java @@ -232,6 +232,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { return buffer.getShort(index); } + @Override + protected short _getShortLE(int index) { + return ByteBufUtil.swapShort(buffer.getShort(index)); + } + @Override public int getUnsignedMedium(int index) { ensureAccessible(); @@ -240,7 +245,16 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { @Override protected int _getUnsignedMedium(int index) { - return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff; + return (getByte(index) & 0xff) << 16 | + (getByte(index + 1) & 0xff) << 8 | + getByte(index + 2) & 0xff; + } + + @Override + protected int _getUnsignedMediumLE(int index) { + return getByte(index) & 0xff | + (getByte(index + 1) & 0xff) << 8 | + (getByte(index + 2) & 0xff) << 16; } @Override @@ -254,6 +268,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { return buffer.getInt(index); } + @Override + protected int _getIntLE(int index) { + return ByteBufUtil.swapInt(buffer.getInt(index)); + } + @Override public long getLong(int index) { ensureAccessible(); @@ -265,6 +284,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { return buffer.getLong(index); } + @Override + protected long _getLongLE(int index) { + return ByteBufUtil.swapLong(buffer.getLong(index)); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); @@ -365,6 +389,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { buffer.putShort(index, (short) value); } + @Override + protected void _setShortLE(int index, int value) { + buffer.putShort(index, ByteBufUtil.swapShort((short) value)); + } + @Override public ByteBuf setMedium(int index, int value) { ensureAccessible(); @@ -379,6 +408,13 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { setByte(index + 2, (byte) value); } + @Override + protected void _setMediumLE(int index, int value) { + setByte(index, (byte) value); + setByte(index + 1, (byte) (value >>> 8)); + setByte(index + 2, (byte) (value >>> 16)); + } + @Override public ByteBuf setInt(int index, int value) { ensureAccessible(); @@ -391,6 +427,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { buffer.putInt(index, value); } + @Override + protected void _setIntLE(int index, int value) { + buffer.putInt(index, ByteBufUtil.swapInt(value)); + } + @Override public ByteBuf setLong(int index, long value) { ensureAccessible(); @@ -403,6 +444,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { buffer.putLong(index, value); } + @Override + protected void _setLongLE(int index, long value) { + buffer.putLong(index, ByteBufUtil.swapLong(value)); + } + @Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index cde398b27d..5325eeaec5 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -303,6 +303,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { return HeapByteBufUtil.getShort(array, index); } + @Override + protected short _getShortLE(int index) { + return HeapByteBufUtil.getShortLE(array, index); + } + @Override public int getUnsignedMedium(int index) { ensureAccessible(); @@ -314,6 +319,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { return HeapByteBufUtil.getUnsignedMedium(array, index); } + @Override + protected int _getUnsignedMediumLE(int index) { + return HeapByteBufUtil.getUnsignedMediumLE(array, index); + } + @Override public int getInt(int index) { ensureAccessible(); @@ -325,6 +335,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { return HeapByteBufUtil.getInt(array, index); } + @Override + protected int _getIntLE(int index) { + return HeapByteBufUtil.getIntLE(array, index); + } + @Override public long getLong(int index) { ensureAccessible(); @@ -336,6 +351,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { return HeapByteBufUtil.getLong(array, index); } + @Override + protected long _getLongLE(int index) { + return HeapByteBufUtil.getLongLE(array, index); + } + @Override public ByteBuf setByte(int index, int value) { ensureAccessible(); @@ -360,6 +380,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { HeapByteBufUtil.setShort(array, index, value); } + @Override + protected void _setShortLE(int index, int value) { + HeapByteBufUtil.setShortLE(array, index, value); + } + @Override public ByteBuf setMedium(int index, int value) { ensureAccessible(); @@ -372,6 +397,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { HeapByteBufUtil.setMedium(array, index, value); } + @Override + protected void _setMediumLE(int index, int value) { + HeapByteBufUtil.setMediumLE(array, index, value); + } + @Override public ByteBuf setInt(int index, int value) { ensureAccessible(); @@ -384,6 +414,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { HeapByteBufUtil.setInt(array, index, value); } + @Override + protected void _setIntLE(int index, int value) { + HeapByteBufUtil.setIntLE(array, index, value); + } + @Override public ByteBuf setLong(int index, long value) { ensureAccessible(); @@ -396,6 +431,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { HeapByteBufUtil.setLong(array, index, value); } + @Override + protected void _setLongLE(int index, long value) { + HeapByteBufUtil.setLongLE(array, index, value); + } + @Override public ByteBuf copy(int index, int length) { checkIndex(index, length); diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java index f6d65150ec..7c5b2e2d5b 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java @@ -223,21 +223,41 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf return UnsafeByteBufUtil.getShort(addr(index)); } + @Override + protected short _getShortLE(int index) { + return UnsafeByteBufUtil.getShortLE(addr(index)); + } + @Override protected int _getUnsignedMedium(int index) { return UnsafeByteBufUtil.getUnsignedMedium(addr(index)); } + @Override + protected int _getUnsignedMediumLE(int index) { + return UnsafeByteBufUtil.getUnsignedMediumLE(addr(index)); + } + @Override protected int _getInt(int index) { return UnsafeByteBufUtil.getInt(addr(index)); } + @Override + protected int _getIntLE(int index) { + return UnsafeByteBufUtil.getIntLE(addr(index)); + } + @Override protected long _getLong(int index) { return UnsafeByteBufUtil.getLong(addr(index)); } + @Override + protected long _getLongLE(int index) { + return UnsafeByteBufUtil.getLongLE(addr(index)); + } + @Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { UnsafeByteBufUtil.getBytes(this, addr(index), index, dst, dstIndex, length); @@ -275,21 +295,41 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf UnsafeByteBufUtil.setShort(addr(index), value); } + @Override + protected void _setShortLE(int index, int value) { + UnsafeByteBufUtil.setShortLE(addr(index), value); + } + @Override protected void _setMedium(int index, int value) { UnsafeByteBufUtil.setMedium(addr(index), value); } + @Override + protected void _setMediumLE(int index, int value) { + UnsafeByteBufUtil.setMediumLE(addr(index), value); + } + @Override protected void _setInt(int index, int value) { UnsafeByteBufUtil.setInt(addr(index), value); } + @Override + protected void _setIntLE(int index, int value) { + UnsafeByteBufUtil.setIntLE(addr(index), value); + } + @Override protected void _setLong(int index, long value) { UnsafeByteBufUtil.setLong(addr(index), value); } + @Override + protected void _setLongLE(int index, long value) { + UnsafeByteBufUtil.setLongLE(addr(index), value); + } + @Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { UnsafeByteBufUtil.setBytes(this, addr(index), index, src, srcIndex, length); diff --git a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java index b074c1f08b..aade5a0d8a 100644 --- a/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java @@ -45,6 +45,14 @@ final class UnsafeByteBufUtil { return (short) (PlatformDependent.getByte(address) << 8 | PlatformDependent.getByte(address + 1) & 0xff); } + static short getShortLE(long address) { + if (UNALIGNED) { + short v = PlatformDependent.getShort(address); + return BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes(v) : v; + } + return (short) (PlatformDependent.getByte(address) & 0xff | PlatformDependent.getByte(address + 1) << 8); + } + static int getUnsignedMedium(long address) { if (UNALIGNED) { if (BIG_ENDIAN_NATIVE_ORDER) { @@ -59,6 +67,20 @@ final class UnsafeByteBufUtil { PlatformDependent.getByte(address + 2) & 0xff; } + static int getUnsignedMediumLE(long address) { + if (UNALIGNED) { + if (BIG_ENDIAN_NATIVE_ORDER) { + return (Short.reverseBytes(PlatformDependent.getShort(address)) & 0xffff) << 8 | + PlatformDependent.getByte(address + 2) & 0xff; + } + return (PlatformDependent.getByte(address) & 0xff) | + (PlatformDependent.getShort(address + 1) & 0xffff) << 8; + } + return PlatformDependent.getByte(address) & 0xff | + (PlatformDependent.getByte(address + 1) & 0xff) << 8 | + (PlatformDependent.getByte(address + 1) & 0xff) << 16 ; + } + static int getInt(long address) { if (UNALIGNED) { int v = PlatformDependent.getInt(address); @@ -70,6 +92,17 @@ final class UnsafeByteBufUtil { PlatformDependent.getByte(address + 3) & 0xff; } + static int getIntLE(long address) { + if (UNALIGNED) { + int v = PlatformDependent.getInt(address); + return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v; + } + return PlatformDependent.getByte(address) & 0xff | + (PlatformDependent.getByte(address + 1) & 0xff) << 8 | + (PlatformDependent.getByte(address + 2) & 0xff) << 16 | + PlatformDependent.getByte(address + 3) << 24; + } + static long getLong(long address) { if (UNALIGNED) { long v = PlatformDependent.getLong(address); @@ -85,6 +118,21 @@ final class UnsafeByteBufUtil { (long) PlatformDependent.getByte(address + 7) & 0xff; } + static long getLongLE(long address) { + if (UNALIGNED) { + long v = PlatformDependent.getLong(address); + return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v; + } + return (long) PlatformDependent.getByte(address) & 0xff | + ((long) PlatformDependent.getByte(address + 1) & 0xff) << 8 | + ((long) PlatformDependent.getByte(address + 2) & 0xff) << 16 | + ((long) PlatformDependent.getByte(address + 3) & 0xff) << 24 | + ((long) PlatformDependent.getByte(address + 4) & 0xff) << 32 | + ((long) PlatformDependent.getByte(address + 5) & 0xff) << 40 | + ((long) PlatformDependent.getByte(address + 6) & 0xff) << 48 | + (long) PlatformDependent.getByte(address + 7) << 56; + } + static void setByte(long address, int value) { PlatformDependent.putByte(address, (byte) value); } @@ -99,6 +147,16 @@ final class UnsafeByteBufUtil { } } + static void setShortLE(long address, int value) { + if (UNALIGNED) { + PlatformDependent.putShort( + address, BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value); + } else { + PlatformDependent.putByte(address, (byte) value); + PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); + } + } + static void setMedium(long address, int value) { if (UNALIGNED) { if (BIG_ENDIAN_NATIVE_ORDER) { @@ -115,6 +173,22 @@ final class UnsafeByteBufUtil { } } + static void setMediumLE(long address, int value) { + if (UNALIGNED) { + if (BIG_ENDIAN_NATIVE_ORDER) { + PlatformDependent.putShort(address, Short.reverseBytes((short) (value >>> 8))); + PlatformDependent.putByte(address + 2, (byte) value); + } else { + PlatformDependent.putByte(address, (byte) value); + PlatformDependent.putShort(address + 1, (short) (value >>> 8)); + } + } else { + PlatformDependent.putByte(address, (byte) value); + PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); + } + } + static void setInt(long address, int value) { if (UNALIGNED) { PlatformDependent.putInt(address, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value)); @@ -126,6 +200,17 @@ final class UnsafeByteBufUtil { } } + static void setIntLE(long address, int value) { + if (UNALIGNED) { + PlatformDependent.putInt(address, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value); + } else { + PlatformDependent.putByte(address, (byte) value); + PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); + PlatformDependent.putByte(address + 3, (byte) (value >>> 24)); + } + } + static void setLong(long address, long value) { if (UNALIGNED) { PlatformDependent.putLong(address, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value)); @@ -141,6 +226,21 @@ final class UnsafeByteBufUtil { } } + static void setLongLE(long address, long value) { + if (UNALIGNED) { + PlatformDependent.putLong(address, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value); + } else { + PlatformDependent.putByte(address, (byte) value); + PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); + PlatformDependent.putByte(address + 3, (byte) (value >>> 24)); + PlatformDependent.putByte(address + 4, (byte) (value >>> 32)); + PlatformDependent.putByte(address + 5, (byte) (value >>> 40)); + PlatformDependent.putByte(address + 6, (byte) (value >>> 48)); + PlatformDependent.putByte(address + 7, (byte) (value >>> 56)); + } + } + static byte getByte(byte[] array, int index) { return PlatformDependent.getByte(array, index); } @@ -153,6 +253,14 @@ final class UnsafeByteBufUtil { return (short) (PlatformDependent.getByte(index) << 8 | PlatformDependent.getByte(index + 1) & 0xff); } + static short getShortLE(byte[] array, int index) { + if (UNALIGNED) { + short v = PlatformDependent.getShort(array, index); + return BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes(v) : v; + } + return (short) (PlatformDependent.getByte(index) & 0xff | PlatformDependent.getByte(index + 1) << 8); + } + static int getUnsignedMedium(byte[] array, int index) { if (UNALIGNED) { if (BIG_ENDIAN_NATIVE_ORDER) { @@ -167,6 +275,20 @@ final class UnsafeByteBufUtil { PlatformDependent.getByte(array, index + 2) & 0xff; } + static int getUnsignedMediumLE(byte[] array, int index) { + if (UNALIGNED) { + if (BIG_ENDIAN_NATIVE_ORDER) { + return (Short.reverseBytes(PlatformDependent.getShort(array, index)) & 0xffff) << 8 | + PlatformDependent.getByte(array, index + 2) & 0xff; + } + return (PlatformDependent.getByte(array, index) & 0xff) | + (PlatformDependent.getShort(array, index + 1) & 0xffff) << 8; + } + return PlatformDependent.getByte(array, index) & 0xff | + (PlatformDependent.getByte(array, index + 1) & 0xff) << 8 | + (PlatformDependent.getByte(array, index + 2) & 0xff) << 16; + } + static int getInt(byte[] array, int index) { if (UNALIGNED) { int v = PlatformDependent.getInt(array, index); @@ -178,9 +300,20 @@ final class UnsafeByteBufUtil { PlatformDependent.getByte(array, index + 3) & 0xff; } + static int getIntLE(byte[] array, int index) { + if (UNALIGNED) { + int v = PlatformDependent.getInt(array, index); + return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v; + } + return PlatformDependent.getByte(array, index) & 0xff | + (PlatformDependent.getByte(array, index + 1) & 0xff) << 8 | + (PlatformDependent.getByte(array, index + 2) & 0xff) << 16 | + PlatformDependent.getByte(array, index + 3) << 24; + } + static long getLong(byte[] array, int index) { if (UNALIGNED) { - long v = PlatformDependent.getLong(array, index); + long v = PlatformDependent.getLong(array, index); return BIG_ENDIAN_NATIVE_ORDER ? v : Long.reverseBytes(v); } return (long) PlatformDependent.getByte(array, index) << 56 | @@ -193,6 +326,21 @@ final class UnsafeByteBufUtil { (long) PlatformDependent.getByte(array, index + 7) & 0xff; } + static long getLongLE(byte[] array, int index) { + if (UNALIGNED) { + long v = PlatformDependent.getLong(array, index); + return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v; + } + return (long) PlatformDependent.getByte(array, index) & 0xff | + ((long) PlatformDependent.getByte(array, index + 1) & 0xff) << 8 | + ((long) PlatformDependent.getByte(array, index + 2) & 0xff) << 16 | + ((long) PlatformDependent.getByte(array, index + 3) & 0xff) << 24 | + ((long) PlatformDependent.getByte(array, index + 4) & 0xff) << 32 | + ((long) PlatformDependent.getByte(array, index + 5) & 0xff) << 40 | + ((long) PlatformDependent.getByte(array, index + 6) & 0xff) << 48 | + (long) PlatformDependent.getByte(array, index + 7) << 56; + } + static void setByte(byte[] array, int index, int value) { PlatformDependent.putByte(array, index, (byte) value); } @@ -207,6 +355,16 @@ final class UnsafeByteBufUtil { } } + static void setShortLE(byte[] array, int index, int value) { + if (UNALIGNED) { + PlatformDependent.putShort( + array, index, BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value); + } else { + PlatformDependent.putByte(array, index, (byte) value); + PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); + } + } + static void setMedium(byte[] array, int index, int value) { if (UNALIGNED) { if (BIG_ENDIAN_NATIVE_ORDER) { @@ -223,6 +381,22 @@ final class UnsafeByteBufUtil { } } + static void setMediumLE(byte[] array, int index, int value) { + if (UNALIGNED) { + if (BIG_ENDIAN_NATIVE_ORDER) { + PlatformDependent.putShort(array, index, Short.reverseBytes((short) (value >>> 8))); + PlatformDependent.putByte(array, index + 2, (byte) value); + } else { + PlatformDependent.putByte(array, index, (byte) value); + PlatformDependent.putShort(array, index + 1, (short) (value >>> 8)); + } + } else { + PlatformDependent.putByte(array, index, (byte) value); + PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16)); + } + } + static void setInt(byte[] array, int index, int value) { if (UNALIGNED) { PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value)); @@ -234,6 +408,17 @@ final class UnsafeByteBufUtil { } } + static void setIntLE(byte[] array, int index, int value) { + if (UNALIGNED) { + PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value); + } else { + PlatformDependent.putByte(array, index, (byte) value); + PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16)); + PlatformDependent.putByte(array, index + 3, (byte) (value >>> 24)); + } + } + static void setLong(byte[] array, int index, long value) { if (UNALIGNED) { PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value)); @@ -249,6 +434,21 @@ final class UnsafeByteBufUtil { } } + static void setLongLE(byte[] array, int index, long value) { + if (UNALIGNED) { + PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value); + } else { + PlatformDependent.putByte(array, index, (byte) value); + PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); + PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16)); + PlatformDependent.putByte(array, index + 3, (byte) (value >>> 24)); + PlatformDependent.putByte(array, index + 4, (byte) (value >>> 32)); + PlatformDependent.putByte(array, index + 5, (byte) (value >>> 40)); + PlatformDependent.putByte(array, index + 6, (byte) (value >>> 48)); + PlatformDependent.putByte(array, index + 7, (byte) (value >>> 56)); + } + } + static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); diff --git a/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java b/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java index 56db37201b..f38ef71419 100644 --- a/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/WrappedByteBuf.java @@ -223,36 +223,71 @@ class WrappedByteBuf extends ByteBuf { return buf.getShort(index); } + @Override + public short getShortLE(int index) { + return buf.getShortLE(index); + } + @Override public int getUnsignedShort(int index) { return buf.getUnsignedShort(index); } + @Override + public int getUnsignedShortLE(int index) { + return buf.getUnsignedShortLE(index); + } + @Override public int getMedium(int index) { return buf.getMedium(index); } + @Override + public int getMediumLE(int index) { + return buf.getMediumLE(index); + } + @Override public int getUnsignedMedium(int index) { return buf.getUnsignedMedium(index); } + @Override + public int getUnsignedMediumLE(int index) { + return buf.getUnsignedMediumLE(index); + } + @Override public int getInt(int index) { return buf.getInt(index); } + @Override + public int getIntLE(int index) { + return buf.getIntLE(index); + } + @Override public long getUnsignedInt(int index) { return buf.getUnsignedInt(index); } + @Override + public long getUnsignedIntLE(int index) { + return buf.getUnsignedIntLE(index); + } + @Override public long getLong(int index) { return buf.getLong(index); } + @Override + public long getLongLE(int index) { + return buf.getLongLE(index); + } + @Override public char getChar(int index) { return buf.getChar(index); @@ -333,24 +368,48 @@ class WrappedByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf setShortLE(int index, int value) { + buf.setShortLE(index, value); + return this; + } + @Override public ByteBuf setMedium(int index, int value) { buf.setMedium(index, value); return this; } + @Override + public ByteBuf setMediumLE(int index, int value) { + buf.setMediumLE(index, value); + return this; + } + @Override public ByteBuf setInt(int index, int value) { buf.setInt(index, value); return this; } + @Override + public ByteBuf setIntLE(int index, int value) { + buf.setIntLE(index, value); + return this; + } + @Override public ByteBuf setLong(int index, long value) { buf.setLong(index, value); return this; } + @Override + public ByteBuf setLongLE(int index, long value) { + buf.setLongLE(index, value); + return this; + } + @Override public ByteBuf setChar(int index, int value) { buf.setChar(index, value); @@ -441,36 +500,71 @@ class WrappedByteBuf extends ByteBuf { return buf.readShort(); } + @Override + public short readShortLE() { + return buf.readShortLE(); + } + @Override public int readUnsignedShort() { return buf.readUnsignedShort(); } + @Override + public int readUnsignedShortLE() { + return buf.readUnsignedShortLE(); + } + @Override public int readMedium() { return buf.readMedium(); } + @Override + public int readMediumLE() { + return buf.readMediumLE(); + } + @Override public int readUnsignedMedium() { return buf.readUnsignedMedium(); } + @Override + public int readUnsignedMediumLE() { + return buf.readUnsignedMediumLE(); + } + @Override public int readInt() { return buf.readInt(); } + @Override + public int readIntLE() { + return buf.readIntLE(); + } + @Override public long readUnsignedInt() { return buf.readUnsignedInt(); } + @Override + public long readUnsignedIntLE() { + return buf.readUnsignedIntLE(); + } + @Override public long readLong() { return buf.readLong(); } + @Override + public long readLongLE() { + return buf.readLongLE(); + } + @Override public char readChar() { return buf.readChar(); @@ -567,24 +661,48 @@ class WrappedByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeShortLE(int value) { + buf.writeShortLE(value); + return this; + } + @Override public ByteBuf writeMedium(int value) { buf.writeMedium(value); return this; } + @Override + public ByteBuf writeMediumLE(int value) { + buf.writeMediumLE(value); + return this; + } + @Override public ByteBuf writeInt(int value) { buf.writeInt(value); return this; } + @Override + public ByteBuf writeIntLE(int value) { + buf.writeIntLE(value); + return this; + } + @Override public ByteBuf writeLong(long value) { buf.writeLong(value); return this; } + @Override + public ByteBuf writeLongLE(long value) { + buf.writeLongLE(value); + return this; + } + @Override public ByteBuf writeChar(int value) { buf.writeChar(value); diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java index a9d7955909..f8c347a6f4 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java @@ -394,99 +394,245 @@ public abstract class AbstractByteBufTest { @Test public void testRandomShortAccess() { + testRandomShortAccess(true); + } + @Test + public void testRandomShortLEAccess() { + testRandomShortAccess(false); + } + + private void testRandomShortAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 1; i += 2) { short value = (short) random.nextInt(); - buffer.setShort(i, value); + if (testBigEndian) { + buffer.setShort(i, value); + } else { + buffer.setShortLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 1; i += 2) { short value = (short) random.nextInt(); - assertEquals(value, buffer.getShort(i)); + if (testBigEndian) { + assertEquals(value, buffer.getShort(i)); + } else { + assertEquals(value, buffer.getShortLE(i)); + } } } @Test public void testRandomUnsignedShortAccess() { + testRandomUnsignedShortAccess(true); + } + + @Test + public void testRandomUnsignedShortLEAccess() { + testRandomUnsignedShortAccess(false); + } + + private void testRandomUnsignedShortAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 1; i += 2) { short value = (short) random.nextInt(); - buffer.setShort(i, value); + if (testBigEndian) { + buffer.setShort(i, value); + } else { + buffer.setShortLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 1; i += 2) { int value = random.nextInt() & 0xFFFF; - assertEquals(value, buffer.getUnsignedShort(i)); + if (testBigEndian) { + assertEquals(value, buffer.getUnsignedShort(i)); + } else { + assertEquals(value, buffer.getUnsignedShortLE(i)); + } } } @Test public void testRandomMediumAccess() { + testRandomMediumAccess(true); + } + + @Test + public void testRandomMediumLEAccess() { + testRandomMediumAccess(false); + } + + private void testRandomMediumAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 2; i += 3) { int value = random.nextInt(); - buffer.setMedium(i, value); + if (testBigEndian) { + buffer.setMedium(i, value); + } else { + buffer.setMediumLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 2; i += 3) { int value = random.nextInt() << 8 >> 8; - assertEquals(value, buffer.getMedium(i)); + if (testBigEndian) { + assertEquals(value, buffer.getMedium(i)); + } else { + assertEquals(value, buffer.getMediumLE(i)); + } } } @Test public void testRandomUnsignedMediumAccess() { + testRandomUnsignedMediumAccess(true); + } + + @Test + public void testRandomUnsignedMediumLEAccess() { + testRandomUnsignedMediumAccess(false); + } + + private void testRandomUnsignedMediumAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 2; i += 3) { int value = random.nextInt(); - buffer.setMedium(i, value); + if (testBigEndian) { + buffer.setMedium(i, value); + } else { + buffer.setMediumLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 2; i += 3) { int value = random.nextInt() & 0x00FFFFFF; - assertEquals(value, buffer.getUnsignedMedium(i)); + if (testBigEndian) { + assertEquals(value, buffer.getUnsignedMedium(i)); + } else { + assertEquals(value, buffer.getUnsignedMediumLE(i)); + } } } @Test public void testRandomIntAccess() { + testRandomIntAccess(true); + } + + @Test + public void testRandomIntLEAccess() { + testRandomIntAccess(false); + } + + private void testRandomIntAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 3; i += 4) { int value = random.nextInt(); - buffer.setInt(i, value); + if (testBigEndian) { + buffer.setInt(i, value); + } else { + buffer.setIntLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 3; i += 4) { int value = random.nextInt(); - assertEquals(value, buffer.getInt(i)); + if (testBigEndian) { + assertEquals(value, buffer.getInt(i)); + } else { + assertEquals(value, buffer.getIntLE(i)); + } } } @Test public void testRandomUnsignedIntAccess() { + testRandomUnsignedIntAccess(true); + } + + @Test + public void testRandomUnsignedIntLEAccess() { + testRandomUnsignedIntAccess(false); + } + + private void testRandomUnsignedIntAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 3; i += 4) { int value = random.nextInt(); - buffer.setInt(i, value); + if (testBigEndian) { + buffer.setInt(i, value); + } else { + buffer.setIntLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 3; i += 4) { long value = random.nextInt() & 0xFFFFFFFFL; - assertEquals(value, buffer.getUnsignedInt(i)); + if (testBigEndian) { + assertEquals(value, buffer.getUnsignedInt(i)); + } else { + assertEquals(value, buffer.getUnsignedIntLE(i)); + } } } @Test public void testRandomLongAccess() { + testRandomLongAccess(true); + } + + @Test + public void testRandomLongLEAccess() { + testRandomLongAccess(false); + } + + private void testRandomLongAccess(boolean testBigEndian) { for (int i = 0; i < buffer.capacity() - 7; i += 8) { long value = random.nextLong(); - buffer.setLong(i, value); + if (testBigEndian) { + buffer.setLong(i, value); + } else { + buffer.setLongLE(i, value); + } } random.setSeed(seed); for (int i = 0; i < buffer.capacity() - 7; i += 8) { long value = random.nextLong(); - assertEquals(value, buffer.getLong(i)); + if (testBigEndian) { + assertEquals(value, buffer.getLong(i)); + } else { + assertEquals(value, buffer.getLongLE(i)); + } + } + } + + @Test + public void testRandomFloatAccess() { + for (int i = 0; i < buffer.capacity() - 7; i += 8) { + float value = random.nextFloat(); + buffer.setFloat(i, value); + } + + random.setSeed(seed); + for (int i = 0; i < buffer.capacity() - 7; i += 8) { + float value = random.nextFloat(); + assertEquals(value, buffer.getFloat(i), 0.01); + } + } + + @Test + public void testRandomDoubleAccess() { + for (int i = 0; i < buffer.capacity() - 7; i += 8) { + double value = random.nextDouble(); + buffer.setDouble(i, value); + } + + random.setSeed(seed); + for (int i = 0; i < buffer.capacity() - 7; i += 8) { + double value = random.nextDouble(); + assertEquals(value, buffer.getDouble(i), 0.01); } } @@ -566,12 +712,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialShortAccess() { + testSequentialShortAccess(true); + } + + @Test + public void testSequentialShortLEAccess() { + testSequentialShortAccess(false); + } + + private void testSequentialShortAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity(); i += 2) { short value = (short) random.nextInt(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeShort(value); + if (testBigEndian) { + buffer.writeShort(value); + } else { + buffer.writeShortLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -583,7 +742,11 @@ public abstract class AbstractByteBufTest { short value = (short) random.nextInt(); assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readShort()); + if (testBigEndian) { + assertEquals(value, buffer.readShort()); + } else { + assertEquals(value, buffer.readShortLE()); + } } assertEquals(buffer.capacity(), buffer.readerIndex()); @@ -594,12 +757,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialUnsignedShortAccess() { + testSequentialUnsignedShortAccess(true); + } + + @Test + public void testSequentialUnsignedShortLEAccess() { + testSequentialUnsignedShortAccess(true); + } + + private void testSequentialUnsignedShortAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity(); i += 2) { short value = (short) random.nextInt(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeShort(value); + if (testBigEndian) { + buffer.writeShort(value); + } else { + buffer.writeShortLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -611,7 +787,11 @@ public abstract class AbstractByteBufTest { int value = random.nextInt() & 0xFFFF; assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readUnsignedShort()); + if (testBigEndian) { + assertEquals(value, buffer.readUnsignedShort()); + } else { + assertEquals(value, buffer.readUnsignedShortLE()); + } } assertEquals(buffer.capacity(), buffer.readerIndex()); @@ -622,12 +802,24 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialMediumAccess() { + testSequentialMediumAccess(true); + } + @Test + public void testSequentialMediumLEAccess() { + testSequentialMediumAccess(false); + } + + private void testSequentialMediumAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity() / 3 * 3; i += 3) { int value = random.nextInt(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeMedium(value); + if (testBigEndian) { + buffer.writeMedium(value); + } else { + buffer.writeMediumLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -639,7 +831,11 @@ public abstract class AbstractByteBufTest { int value = random.nextInt() << 8 >> 8; assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readMedium()); + if (testBigEndian) { + assertEquals(value, buffer.readMedium()); + } else { + assertEquals(value, buffer.readMediumLE()); + } } assertEquals(buffer.capacity() / 3 * 3, buffer.readerIndex()); @@ -650,12 +846,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialUnsignedMediumAccess() { + testSequentialUnsignedMediumAccess(true); + } + + @Test + public void testSequentialUnsignedMediumLEAccess() { + testSequentialUnsignedMediumAccess(false); + } + + private void testSequentialUnsignedMediumAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity() / 3 * 3; i += 3) { int value = random.nextInt() & 0x00FFFFFF; assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeMedium(value); + if (testBigEndian) { + buffer.writeMedium(value); + } else { + buffer.writeMediumLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -667,7 +876,11 @@ public abstract class AbstractByteBufTest { int value = random.nextInt() & 0x00FFFFFF; assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readUnsignedMedium()); + if (testBigEndian) { + assertEquals(value, buffer.readUnsignedMedium()); + } else { + assertEquals(value, buffer.readUnsignedMediumLE()); + } } assertEquals(buffer.capacity() / 3 * 3, buffer.readerIndex()); @@ -678,12 +891,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialIntAccess() { + testSequentialIntAccess(true); + } + + @Test + public void testSequentialIntLEAccess() { + testSequentialIntAccess(false); + } + + private void testSequentialIntAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity(); i += 4) { int value = random.nextInt(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeInt(value); + if (testBigEndian) { + buffer.writeInt(value); + } else { + buffer.writeIntLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -695,7 +921,11 @@ public abstract class AbstractByteBufTest { int value = random.nextInt(); assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readInt()); + if (testBigEndian) { + assertEquals(value, buffer.readInt()); + } else { + assertEquals(value, buffer.readIntLE()); + } } assertEquals(buffer.capacity(), buffer.readerIndex()); @@ -706,12 +936,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialUnsignedIntAccess() { + testSequentialUnsignedIntAccess(true); + } + + @Test + public void testSequentialUnsignedIntLEAccess() { + testSequentialUnsignedIntAccess(false); + } + + private void testSequentialUnsignedIntAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity(); i += 4) { int value = random.nextInt(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeInt(value); + if (testBigEndian) { + buffer.writeInt(value); + } else { + buffer.writeIntLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -723,7 +966,11 @@ public abstract class AbstractByteBufTest { long value = random.nextInt() & 0xFFFFFFFFL; assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readUnsignedInt()); + if (testBigEndian) { + assertEquals(value, buffer.readUnsignedInt()); + } else { + assertEquals(value, buffer.readUnsignedIntLE()); + } } assertEquals(buffer.capacity(), buffer.readerIndex()); @@ -734,12 +981,25 @@ public abstract class AbstractByteBufTest { @Test public void testSequentialLongAccess() { + testSequentialLongAccess(true); + } + + @Test + public void testSequentialLongLEAccess() { + testSequentialLongAccess(false); + } + + private void testSequentialLongAccess(boolean testBigEndian) { buffer.writerIndex(0); for (int i = 0; i < buffer.capacity(); i += 8) { long value = random.nextLong(); assertEquals(i, buffer.writerIndex()); assertTrue(buffer.isWritable()); - buffer.writeLong(value); + if (testBigEndian) { + buffer.writeLong(value); + } else { + buffer.writeLongLE(value); + } } assertEquals(0, buffer.readerIndex()); @@ -751,7 +1011,11 @@ public abstract class AbstractByteBufTest { long value = random.nextLong(); assertEquals(i, buffer.readerIndex()); assertTrue(buffer.isReadable()); - assertEquals(value, buffer.readLong()); + if (testBigEndian) { + assertEquals(value, buffer.readLong()); + } else { + assertEquals(value, buffer.readLongLE()); + } } assertEquals(buffer.capacity(), buffer.readerIndex()); @@ -1996,16 +2260,31 @@ public abstract class AbstractByteBufTest { releasedBuffer().getShort(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetShortLEAfterRelease() { + releasedBuffer().getShortLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetUnsignedShortAfterRelease() { releasedBuffer().getUnsignedShort(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetUnsignedShortLEAfterRelease() { + releasedBuffer().getUnsignedShortLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetMediumAfterRelease() { releasedBuffer().getMedium(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetMediumLEAfterRelease() { + releasedBuffer().getMediumLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetUnsignedMediumAfterRelease() { releasedBuffer().getUnsignedMedium(0); @@ -2016,16 +2295,31 @@ public abstract class AbstractByteBufTest { releasedBuffer().getInt(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetIntLEAfterRelease() { + releasedBuffer().getIntLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetUnsignedIntAfterRelease() { releasedBuffer().getUnsignedInt(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetUnsignedIntLEAfterRelease() { + releasedBuffer().getUnsignedIntLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetLongAfterRelease() { releasedBuffer().getLong(0); } + @Test(expected = IllegalReferenceCountException.class) + public void testGetLongLEAfterRelease() { + releasedBuffer().getLongLE(0); + } + @Test(expected = IllegalReferenceCountException.class) public void testGetCharAfterRelease() { releasedBuffer().getChar(0); @@ -2096,21 +2390,41 @@ public abstract class AbstractByteBufTest { releasedBuffer().setShort(0, 1); } + @Test(expected = IllegalReferenceCountException.class) + public void testSetShortLEAfterRelease() { + releasedBuffer().setShortLE(0, 1); + } + @Test(expected = IllegalReferenceCountException.class) public void testSetMediumAfterRelease() { releasedBuffer().setMedium(0, 1); } + @Test(expected = IllegalReferenceCountException.class) + public void testSetMediumLEAfterRelease() { + releasedBuffer().setMediumLE(0, 1); + } + @Test(expected = IllegalReferenceCountException.class) public void testSetIntAfterRelease() { releasedBuffer().setInt(0, 1); } + @Test(expected = IllegalReferenceCountException.class) + public void testSetIntLEAfterRelease() { + releasedBuffer().setIntLE(0, 1); + } + @Test(expected = IllegalReferenceCountException.class) public void testSetLongAfterRelease() { releasedBuffer().setLong(0, 1); } + @Test(expected = IllegalReferenceCountException.class) + public void testSetLongLEAfterRelease() { + releasedBuffer().setLongLE(0, 1); + } + @Test(expected = IllegalReferenceCountException.class) public void testSetCharAfterRelease() { releasedBuffer().setChar(0, 1); @@ -2191,36 +2505,71 @@ public abstract class AbstractByteBufTest { releasedBuffer().readShort(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadShortLEAfterRelease() { + releasedBuffer().readShortLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadUnsignedShortAfterRelease() { releasedBuffer().readUnsignedShort(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadUnsignedShortLEAfterRelease() { + releasedBuffer().readUnsignedShortLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadMediumAfterRelease() { releasedBuffer().readMedium(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadMediumLEAfterRelease() { + releasedBuffer().readMediumLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadUnsignedMediumAfterRelease() { releasedBuffer().readUnsignedMedium(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadUnsignedMediumLEAfterRelease() { + releasedBuffer().readUnsignedMediumLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadIntAfterRelease() { releasedBuffer().readInt(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadIntLEAfterRelease() { + releasedBuffer().readIntLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadUnsignedIntAfterRelease() { releasedBuffer().readUnsignedInt(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadUnsignedIntLEAfterRelease() { + releasedBuffer().readUnsignedIntLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadLongAfterRelease() { releasedBuffer().readLong(); } + @Test(expected = IllegalReferenceCountException.class) + public void testReadLongLEAfterRelease() { + releasedBuffer().readLongLE(); + } + @Test(expected = IllegalReferenceCountException.class) public void testReadCharAfterRelease() { releasedBuffer().readChar(); @@ -2301,21 +2650,41 @@ public abstract class AbstractByteBufTest { releasedBuffer().writeShort(1); } + @Test(expected = IllegalReferenceCountException.class) + public void testWriteShortLEAfterRelease() { + releasedBuffer().writeShortLE(1); + } + @Test(expected = IllegalReferenceCountException.class) public void testWriteMediumAfterRelease() { releasedBuffer().writeMedium(1); } + @Test(expected = IllegalReferenceCountException.class) + public void testWriteMediumLEAfterRelease() { + releasedBuffer().writeMediumLE(1); + } + @Test(expected = IllegalReferenceCountException.class) public void testWriteIntAfterRelease() { releasedBuffer().writeInt(1); } + @Test(expected = IllegalReferenceCountException.class) + public void testWriteIntLEAfterRelease() { + releasedBuffer().writeIntLE(1); + } + @Test(expected = IllegalReferenceCountException.class) public void testWriteLongAfterRelease() { releasedBuffer().writeLong(1); } + @Test(expected = IllegalReferenceCountException.class) + public void testWriteLongLEAfterRelease() { + releasedBuffer().writeLongLE(1); + } + @Test(expected = IllegalReferenceCountException.class) public void testWriteCharAfterRelease() { releasedBuffer().writeChar(1); diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java index 24d06dc1ef..212819ea79 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoderByteBuf.java @@ -245,42 +245,84 @@ final class ReplayingDecoderByteBuf extends ByteBuf { return buffer.getInt(index); } + @Override + public int getIntLE(int index) { + checkIndex(index, 4); + return buffer.getIntLE(index); + } + @Override public long getUnsignedInt(int index) { checkIndex(index, 4); return buffer.getUnsignedInt(index); } + @Override + public long getUnsignedIntLE(int index) { + checkIndex(index, 4); + return buffer.getUnsignedIntLE(index); + } + @Override public long getLong(int index) { checkIndex(index, 8); return buffer.getLong(index); } + @Override + public long getLongLE(int index) { + checkIndex(index, 8); + return buffer.getLongLE(index); + } + @Override public int getMedium(int index) { checkIndex(index, 3); return buffer.getMedium(index); } + @Override + public int getMediumLE(int index) { + checkIndex(index, 3); + return buffer.getMediumLE(index); + } + @Override public int getUnsignedMedium(int index) { checkIndex(index, 3); return buffer.getUnsignedMedium(index); } + @Override + public int getUnsignedMediumLE(int index) { + checkIndex(index, 3); + return buffer.getUnsignedMediumLE(index); + } + @Override public short getShort(int index) { checkIndex(index, 2); return buffer.getShort(index); } + @Override + public short getShortLE(int index) { + checkIndex(index, 2); + return buffer.getShortLE(index); + } + @Override public int getUnsignedShort(int index) { checkIndex(index, 2); return buffer.getUnsignedShort(index); } + @Override + public int getUnsignedShortLE(int index) { + checkIndex(index, 2); + return buffer.getUnsignedShortLE(index); + } + @Override public char getChar(int index) { checkIndex(index, 2); @@ -550,42 +592,84 @@ final class ReplayingDecoderByteBuf extends ByteBuf { return buffer.readInt(); } + @Override + public int readIntLE() { + checkReadableBytes(4); + return buffer.readIntLE(); + } + @Override public long readUnsignedInt() { checkReadableBytes(4); return buffer.readUnsignedInt(); } + @Override + public long readUnsignedIntLE() { + checkReadableBytes(4); + return buffer.readUnsignedIntLE(); + } + @Override public long readLong() { checkReadableBytes(8); return buffer.readLong(); } + @Override + public long readLongLE() { + checkReadableBytes(8); + return buffer.readLongLE(); + } + @Override public int readMedium() { checkReadableBytes(3); return buffer.readMedium(); } + @Override + public int readMediumLE() { + checkReadableBytes(3); + return buffer.readMediumLE(); + } + @Override public int readUnsignedMedium() { checkReadableBytes(3); return buffer.readUnsignedMedium(); } + @Override + public int readUnsignedMediumLE() { + checkReadableBytes(3); + return buffer.readUnsignedMediumLE(); + } + @Override public short readShort() { checkReadableBytes(2); return buffer.readShort(); } + @Override + public short readShortLE() { + checkReadableBytes(2); + return buffer.readShortLE(); + } + @Override public int readUnsignedShort() { checkReadableBytes(2); return buffer.readUnsignedShort(); } + @Override + public int readUnsignedShortLE() { + checkReadableBytes(2); + return buffer.readUnsignedShortLE(); + } + @Override public char readChar() { checkReadableBytes(2); @@ -694,24 +778,48 @@ final class ReplayingDecoderByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf setIntLE(int index, int value) { + reject(); + return this; + } + @Override public ByteBuf setLong(int index, long value) { reject(); return this; } + @Override + public ByteBuf setLongLE(int index, long value) { + reject(); + return this; + } + @Override public ByteBuf setMedium(int index, int value) { reject(); return this; } + @Override + public ByteBuf setMediumLE(int index, int value) { + reject(); + return this; + } + @Override public ByteBuf setShort(int index, int value) { reject(); return this; } + @Override + public ByteBuf setShortLE(int index, int value) { + reject(); + return this; + } + @Override public ByteBuf setChar(int index, int value) { reject(); @@ -893,18 +1001,36 @@ final class ReplayingDecoderByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeIntLE(int value) { + reject(); + return this; + } + @Override public ByteBuf writeLong(long value) { reject(); return this; } + @Override + public ByteBuf writeLongLE(long value) { + reject(); + return this; + } + @Override public ByteBuf writeMedium(int value) { reject(); return this; } + @Override + public ByteBuf writeMediumLE(int value) { + reject(); + return this; + } + @Override public ByteBuf writeZero(int length) { reject(); @@ -928,6 +1054,12 @@ final class ReplayingDecoderByteBuf extends ByteBuf { return this; } + @Override + public ByteBuf writeShortLE(int value) { + reject(); + return this; + } + @Override public ByteBuf writeChar(int value) { reject();