diff --git a/buffer/src/main/java/io/netty/buffer/b2/BBuf.java b/buffer/src/main/java/io/netty/buffer/b2/BBuf.java index 40ddcd3..eb9ca67 100644 --- a/buffer/src/main/java/io/netty/buffer/b2/BBuf.java +++ b/buffer/src/main/java/io/netty/buffer/b2/BBuf.java @@ -20,6 +20,7 @@ import jdk.incubator.foreign.MemorySegment; import java.nio.ByteOrder; import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset_BE; +import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset_LE; import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset_BE; import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset_LE; import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset_BE; @@ -136,7 +137,7 @@ class BBuf extends RcSupport implements Buf { @Override public byte readByte() { checkRead(roff, Byte.BYTES); - byte value = getByteAtOffset_BE(seg, roff); + byte value = (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)); roff += Byte.BYTES; return value; } @@ -144,13 +145,13 @@ class BBuf extends RcSupport implements Buf { @Override public byte readByte(int roff) { checkRead(roff, Byte.BYTES); - return getByteAtOffset_BE(seg, roff); + return (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)); } @Override public int readUnsignedByte() { checkRead(roff, Byte.BYTES); - int value = getByteAtOffset_BE(seg, roff) & 0xFF; + int value = (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)) & 0xFF; roff += Byte.BYTES; return value; } @@ -158,39 +159,55 @@ class BBuf extends RcSupport implements Buf { @Override public int readUnsignedByte(int roff) { checkRead(roff, Byte.BYTES); - return getByteAtOffset_BE(seg, roff) & 0xFF; + return (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)) & 0xFF; } @Override public Buf writeByte(byte value) { - setByteAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, value); + } else { + setByteAtOffset_LE(seg, woff, value); + } woff += Byte.BYTES; return this; } @Override public Buf writeByte(int woff, byte value) { - setByteAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, value); + } else { + setByteAtOffset_LE(seg, woff, value); + } return this; } @Override public Buf writeUnsignedByte(int value) { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + } else { + setByteAtOffset_LE(seg, woff, (byte) (value & 0xFF)); + } woff += Byte.BYTES; return this; } @Override public Buf writeUnsignedByte(int woff, int value) { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + } else { + setByteAtOffset_LE(seg, woff, (byte) (value & 0xFF)); + } return this; } @Override public char readChar() { checkRead(roff, 2); - char value = getCharAtOffset_BE(seg, roff); + char value = (isBigEndian? getCharAtOffset_BE(seg, roff) : getCharAtOffset_LE(seg, roff)); roff += 2; return value; } @@ -198,7 +215,7 @@ class BBuf extends RcSupport implements Buf { @Override public char readChar(int roff) { checkRead(roff, 2); - return getCharAtOffset_BE(seg, roff); + return (isBigEndian? getCharAtOffset_BE(seg, roff) : getCharAtOffset_LE(seg, roff)); } @Override @@ -215,16 +232,38 @@ class BBuf extends RcSupport implements Buf { return getCharAtOffset_LE(seg, roff); } + @Override + public char readCharBE() { + checkRead(roff, 2); + char value = getCharAtOffset_BE(seg, roff); + roff += 2; + return value; + } + + @Override + public char readCharBE(int roff) { + checkRead(roff, 2); + return getCharAtOffset_BE(seg, roff); + } + @Override public Buf writeChar(char value) { - setCharAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setCharAtOffset_BE(seg, woff, value); + } else { + setCharAtOffset_LE(seg, woff, value); + } woff += 2; return this; } @Override public Buf writeChar(int woff, char value) { - setCharAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setCharAtOffset_BE(seg, woff, value); + } else { + setCharAtOffset_LE(seg, woff, value); + } return this; } @@ -241,10 +280,23 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeCharBE(char value) { + setCharAtOffset_BE(seg, woff, value); + woff += 2; + return this; + } + + @Override + public Buf writeCharBE(int woff, char value) { + setCharAtOffset_BE(seg, woff, value); + return this; + } + @Override public short readShort() { checkRead(roff, Short.BYTES); - short value = getShortAtOffset_BE(seg, roff); + short value = (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)); roff += Short.BYTES; return value; } @@ -252,7 +304,7 @@ class BBuf extends RcSupport implements Buf { @Override public short readShort(int roff) { checkRead(roff, Short.BYTES); - return getShortAtOffset_BE(seg, roff); + return (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)); } @Override @@ -269,10 +321,24 @@ class BBuf extends RcSupport implements Buf { return getShortAtOffset_LE(seg, roff); } + @Override + public short readShortBE() { + checkRead(roff, Short.BYTES); + short value = getShortAtOffset_BE(seg, roff); + roff += Short.BYTES; + return value; + } + + @Override + public short readShortBE(int roff) { + checkRead(roff, Short.BYTES); + return getShortAtOffset_BE(seg, roff); + } + @Override public int readUnsignedShort() { checkRead(roff, Short.BYTES); - int value = getShortAtOffset_BE(seg, roff) & 0xFFFF; + int value = (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)) & 0xFFFF; roff += Short.BYTES; return value; } @@ -280,7 +346,7 @@ class BBuf extends RcSupport implements Buf { @Override public int readUnsignedShort(int roff) { checkRead(roff, Short.BYTES); - return getShortAtOffset_BE(seg, roff) & 0xFFFF; + return (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)) & 0xFFFF; } @Override @@ -297,16 +363,38 @@ class BBuf extends RcSupport implements Buf { return getShortAtOffset_LE(seg, roff) & 0xFFFF; } + @Override + public int readUnsignedShortBE() { + checkRead(roff, Short.BYTES); + int value = getShortAtOffset_BE(seg, roff) & 0xFFFF; + roff += Short.BYTES; + return value; + } + + @Override + public int readUnsignedShortBE(int roff) { + checkRead(roff, Short.BYTES); + return getShortAtOffset_BE(seg, roff) & 0xFFFF; + } + @Override public Buf writeShort(short value) { - setShortAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setShortAtOffset_BE(seg, woff, value); + } else { + setShortAtOffset_LE(seg, woff, value); + } woff += Short.BYTES; return this; } @Override public Buf writeShort(int woff, short value) { - setShortAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setShortAtOffset_BE(seg, woff, value); + } else { + setShortAtOffset_LE(seg, woff, value); + } return this; } @@ -323,16 +411,37 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeShortBE(short value) { + setShortAtOffset_BE(seg, woff, value); + woff += Short.BYTES; + return this; + } + + @Override + public Buf writeShortBE(int woff, short value) { + setShortAtOffset_BE(seg, woff, value); + return this; + } + @Override public Buf writeUnsignedShort(int value) { - setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + if (isBigEndian) { + setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + } else { + setShortAtOffset_LE(seg, woff, (short) (value & 0xFFFF)); + } woff += Short.BYTES; return this; } @Override public Buf writeUnsignedShort(int woff, int value) { - setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + if (isBigEndian) { + setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + } else { + setShortAtOffset_LE(seg, woff, (short) (value & 0xFFFF)); + } return this; } @@ -349,14 +458,27 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeUnsignedShortBE(int value) { + setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + woff += Short.BYTES; + return this; + } + + @Override + public Buf writeUnsignedShortBE(int woff, int value) { + setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); + return this; + } + @Override public int readMedium() { checkRead(roff, 3); int value = isBigEndian? - getByteAtOffset_BE(seg, roff) << 16 | +getByteAtOffset_BE(seg, roff) << 16 | (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF : - getByteAtOffset_BE(seg, roff) & 0xFF | + getByteAtOffset_BE(seg, roff + 2) & 0xFF : +getByteAtOffset_BE(seg, roff) & 0xFF | (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | getByteAtOffset_BE(seg, roff + 2) << 16; roff += 3; @@ -366,9 +488,13 @@ class BBuf extends RcSupport implements Buf { @Override public int readMedium(int roff) { checkRead(roff, 3); - return getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF; + return isBigEndian? +getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF : +getByteAtOffset_BE(seg, roff) & 0xFF | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) << 16; } @Override @@ -389,12 +515,34 @@ class BBuf extends RcSupport implements Buf { getByteAtOffset_BE(seg, roff + 2) << 16; } + @Override + public int readMediumBE() { + checkRead(roff, 3); + int value = getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF; + roff += 3; + return value; + } + + @Override + public int readMediumBE(int roff) { + checkRead(roff, 3); + return getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF; + } + @Override public int readUnsignedMedium() { checkRead(roff, 3); - int value = (getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF; + int value = isBigEndian? +(getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF : +(getByteAtOffset_BE(seg, roff) & 0xFF | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) << 16) & 0xFFFFFF; roff += 3; return value; } @@ -402,9 +550,13 @@ class BBuf extends RcSupport implements Buf { @Override public int readUnsignedMedium(int roff) { checkRead(roff, 3); - return (getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF; + return isBigEndian? +(getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF : +(getByteAtOffset_BE(seg, roff) & 0xFF | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) << 16) & 0xFFFFFF; } @Override @@ -425,12 +577,36 @@ class BBuf extends RcSupport implements Buf { getByteAtOffset_BE(seg, roff + 2) << 16) & 0xFFFFFF; } + @Override + public int readUnsignedMediumBE() { + checkRead(roff, 3); + int value = (getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF; + roff += 3; + return value; + } + + @Override + public int readUnsignedMediumBE(int roff) { + checkRead(roff, 3); + return (getByteAtOffset_BE(seg, roff) << 16 | + (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF; + } + @Override public Buf writeMedium(int value) { checkWrite(woff, 3); - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + } else { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + } woff += 3; return this; } @@ -438,9 +614,15 @@ class BBuf extends RcSupport implements Buf { @Override public Buf writeMedium(int woff, int value) { checkWrite(woff, 3); - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + } else { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + } return this; } @@ -464,7 +646,7 @@ class BBuf extends RcSupport implements Buf { } @Override - public Buf writeUnsignedMedium(int value) { + public Buf writeMediumBE(int value) { checkWrite(woff, 3); setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); @@ -474,7 +656,7 @@ class BBuf extends RcSupport implements Buf { } @Override - public Buf writeUnsignedMedium(int woff, int value) { + public Buf writeMediumBE(int woff, int value) { checkWrite(woff, 3); setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); @@ -482,6 +664,37 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeUnsignedMedium(int value) { + checkWrite(woff, 3); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + } else { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + } + woff += 3; + return this; + } + + @Override + public Buf writeUnsignedMedium(int woff, int value) { + checkWrite(woff, 3); + if (isBigEndian) { + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + } else { + setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + } + return this; + } + @Override public Buf writeUnsignedMediumLE(int value) { checkWrite(woff, 3); @@ -501,10 +714,29 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeUnsignedMediumBE(int value) { + checkWrite(woff, 3); + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + woff += 3; + return this; + } + + @Override + public Buf writeUnsignedMediumBE(int woff, int value) { + checkWrite(woff, 3); + setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); + setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + return this; + } + @Override public int readInt() { checkRead(roff, Integer.BYTES); - int value = getIntAtOffset_BE(seg, roff); + int value = (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)); roff += Integer.BYTES; return value; } @@ -512,7 +744,7 @@ class BBuf extends RcSupport implements Buf { @Override public int readInt(int roff) { checkRead(roff, Integer.BYTES); - return getIntAtOffset_BE(seg, roff); + return (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)); } @Override @@ -529,10 +761,24 @@ class BBuf extends RcSupport implements Buf { return getIntAtOffset_LE(seg, roff); } + @Override + public int readIntBE() { + checkRead(roff, Integer.BYTES); + int value = getIntAtOffset_BE(seg, roff); + roff += Integer.BYTES; + return value; + } + + @Override + public int readIntBE(int roff) { + checkRead(roff, Integer.BYTES); + return getIntAtOffset_BE(seg, roff); + } + @Override public long readUnsignedInt() { checkRead(roff, Integer.BYTES); - long value = getIntAtOffset_BE(seg, roff) & 0xFFFFFFFFL; + long value = (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)) & 0xFFFFFFFFL; roff += Integer.BYTES; return value; } @@ -540,7 +786,7 @@ class BBuf extends RcSupport implements Buf { @Override public long readUnsignedInt(int roff) { checkRead(roff, Integer.BYTES); - return getIntAtOffset_BE(seg, roff) & 0xFFFFFFFFL; + return (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)) & 0xFFFFFFFFL; } @Override @@ -557,16 +803,38 @@ class BBuf extends RcSupport implements Buf { return getIntAtOffset_LE(seg, roff) & 0xFFFFFFFFL; } + @Override + public long readUnsignedIntBE() { + checkRead(roff, Integer.BYTES); + long value = getIntAtOffset_BE(seg, roff) & 0xFFFFFFFFL; + roff += Integer.BYTES; + return value; + } + + @Override + public long readUnsignedIntBE(int roff) { + checkRead(roff, Integer.BYTES); + return getIntAtOffset_BE(seg, roff) & 0xFFFFFFFFL; + } + @Override public Buf writeInt(int value) { - setIntAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setIntAtOffset_BE(seg, woff, value); + } else { + setIntAtOffset_LE(seg, woff, value); + } woff += Integer.BYTES; return this; } @Override public Buf writeInt(int woff, int value) { - setIntAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setIntAtOffset_BE(seg, woff, value); + } else { + setIntAtOffset_LE(seg, woff, value); + } return this; } @@ -583,16 +851,37 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeIntBE(int value) { + setIntAtOffset_BE(seg, woff, value); + woff += Integer.BYTES; + return this; + } + + @Override + public Buf writeIntBE(int woff, int value) { + setIntAtOffset_BE(seg, woff, value); + return this; + } + @Override public Buf writeUnsignedInt(long value) { - setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + if (isBigEndian) { + setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + } else { + setIntAtOffset_LE(seg, woff, (int) (value & 0xFFFFFFFFL)); + } woff += Integer.BYTES; return this; } @Override public Buf writeUnsignedInt(int woff, long value) { - setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + if (isBigEndian) { + setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + } else { + setIntAtOffset_LE(seg, woff, (int) (value & 0xFFFFFFFFL)); + } return this; } @@ -609,10 +898,23 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeUnsignedIntBE(long value) { + setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + woff += Integer.BYTES; + return this; + } + + @Override + public Buf writeUnsignedIntBE(int woff, long value) { + setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); + return this; + } + @Override public float readFloat() { checkRead(roff, Float.BYTES); - float value = getFloatAtOffset_BE(seg, roff); + float value = (isBigEndian? getFloatAtOffset_BE(seg, roff) : getFloatAtOffset_LE(seg, roff)); roff += Float.BYTES; return value; } @@ -620,7 +922,7 @@ class BBuf extends RcSupport implements Buf { @Override public float readFloat(int roff) { checkRead(roff, Float.BYTES); - return getFloatAtOffset_BE(seg, roff); + return (isBigEndian? getFloatAtOffset_BE(seg, roff) : getFloatAtOffset_LE(seg, roff)); } @Override @@ -637,16 +939,38 @@ class BBuf extends RcSupport implements Buf { return getFloatAtOffset_LE(seg, roff); } + @Override + public float readFloatBE() { + checkRead(roff, Float.BYTES); + float value = getFloatAtOffset_BE(seg, roff); + roff += Float.BYTES; + return value; + } + + @Override + public float readFloatBE(int roff) { + checkRead(roff, Float.BYTES); + return getFloatAtOffset_BE(seg, roff); + } + @Override public Buf writeFloat(float value) { - setFloatAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setFloatAtOffset_BE(seg, woff, value); + } else { + setFloatAtOffset_LE(seg, woff, value); + } woff += Float.BYTES; return this; } @Override public Buf writeFloat(int woff, float value) { - setFloatAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setFloatAtOffset_BE(seg, woff, value); + } else { + setFloatAtOffset_LE(seg, woff, value); + } return this; } @@ -663,10 +987,23 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeFloatBE(float value) { + setFloatAtOffset_BE(seg, woff, value); + woff += Float.BYTES; + return this; + } + + @Override + public Buf writeFloatBE(int woff, float value) { + setFloatAtOffset_BE(seg, woff, value); + return this; + } + @Override public long readLong() { checkRead(roff, Long.BYTES); - long value = getLongAtOffset_BE(seg, roff); + long value = (isBigEndian? getLongAtOffset_BE(seg, roff) : getLongAtOffset_LE(seg, roff)); roff += Long.BYTES; return value; } @@ -674,7 +1011,7 @@ class BBuf extends RcSupport implements Buf { @Override public long readLong(int roff) { checkRead(roff, Long.BYTES); - return getLongAtOffset_BE(seg, roff); + return (isBigEndian? getLongAtOffset_BE(seg, roff) : getLongAtOffset_LE(seg, roff)); } @Override @@ -691,16 +1028,38 @@ class BBuf extends RcSupport implements Buf { return getLongAtOffset_LE(seg, roff); } + @Override + public long readLongBE() { + checkRead(roff, Long.BYTES); + long value = getLongAtOffset_BE(seg, roff); + roff += Long.BYTES; + return value; + } + + @Override + public long readLongBE(int roff) { + checkRead(roff, Long.BYTES); + return getLongAtOffset_BE(seg, roff); + } + @Override public Buf writeLong(long value) { - setLongAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setLongAtOffset_BE(seg, woff, value); + } else { + setLongAtOffset_LE(seg, woff, value); + } woff += Long.BYTES; return this; } @Override public Buf writeLong(int woff, long value) { - setLongAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setLongAtOffset_BE(seg, woff, value); + } else { + setLongAtOffset_LE(seg, woff, value); + } return this; } @@ -717,10 +1076,23 @@ class BBuf extends RcSupport implements Buf { return this; } + @Override + public Buf writeLongBE(long value) { + setLongAtOffset_BE(seg, woff, value); + woff += Long.BYTES; + return this; + } + + @Override + public Buf writeLongBE(int woff, long value) { + setLongAtOffset_BE(seg, woff, value); + return this; + } + @Override public double readDouble() { checkRead(roff, Double.BYTES); - double value = getDoubleAtOffset_BE(seg, roff); + double value = (isBigEndian? getDoubleAtOffset_BE(seg, roff) : getDoubleAtOffset_LE(seg, roff)); roff += Double.BYTES; return value; } @@ -728,7 +1100,7 @@ class BBuf extends RcSupport implements Buf { @Override public double readDouble(int roff) { checkRead(roff, Double.BYTES); - return getDoubleAtOffset_BE(seg, roff); + return (isBigEndian? getDoubleAtOffset_BE(seg, roff) : getDoubleAtOffset_LE(seg, roff)); } @Override @@ -745,16 +1117,38 @@ class BBuf extends RcSupport implements Buf { return getDoubleAtOffset_LE(seg, roff); } + @Override + public double readDoubleBE() { + checkRead(roff, Double.BYTES); + double value = getDoubleAtOffset_BE(seg, roff); + roff += Double.BYTES; + return value; + } + + @Override + public double readDoubleBE(int roff) { + checkRead(roff, Double.BYTES); + return getDoubleAtOffset_BE(seg, roff); + } + @Override public Buf writeDouble(double value) { - setDoubleAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setDoubleAtOffset_BE(seg, woff, value); + } else { + setDoubleAtOffset_LE(seg, woff, value); + } woff += Double.BYTES; return this; } @Override public Buf writeDouble(int woff, double value) { - setDoubleAtOffset_BE(seg, woff, value); + if (isBigEndian) { + setDoubleAtOffset_BE(seg, woff, value); + } else { + setDoubleAtOffset_LE(seg, woff, value); + } return this; } @@ -770,6 +1164,19 @@ class BBuf extends RcSupport implements Buf { setDoubleAtOffset_LE(seg, woff, value); return this; } + + @Override + public Buf writeDoubleBE(double value) { + setDoubleAtOffset_BE(seg, woff, value); + woff += Double.BYTES; + return this; + } + + @Override + public Buf writeDoubleBE(int woff, double value) { + setDoubleAtOffset_BE(seg, woff, value); + return this; + } // // ### CODEGEN END primitive accessors implementation diff --git a/buffer/src/main/java/io/netty/buffer/b2/Buf.java b/buffer/src/main/java/io/netty/buffer/b2/Buf.java index 994214e..c26d653 100644 --- a/buffer/src/main/java/io/netty/buffer/b2/Buf.java +++ b/buffer/src/main/java/io/netty/buffer/b2/Buf.java @@ -117,7 +117,8 @@ public interface Buf extends Rc { /** * Get the byte value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Byte#BYTES}. - * The value is read using a two's complement 8-bit encoding, with big-endian byte order. + * The value is read using a two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The byte value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Byte#BYTES}. @@ -127,7 +128,8 @@ public interface Buf extends Rc { /** * Get the byte value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 8-bit encoding, with big-endian byte order. + * The value is read using a two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The byte value at the given offset. @@ -139,7 +141,8 @@ public interface Buf extends Rc { /** * Get the unsigned byte value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Byte#BYTES}. - * The value is read using an unsigned two's complement 8-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The unsigned byte value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Byte#BYTES}. @@ -149,7 +152,8 @@ public interface Buf extends Rc { /** * Get the unsigned byte value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 8-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned byte value at the given offset. @@ -161,7 +165,8 @@ public interface Buf extends Rc { /** * Set the given byte value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Byte#BYTES}. - * The value is written using a two's complement 8-bit encoding, with big-endian byte order. + * The value is written using a two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The byte value to write. * @return This Buf. @@ -171,7 +176,8 @@ public interface Buf extends Rc { /** * Set the given byte value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 8-bit encoding, with big-endian byte order. + * The value is written using a two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The byte value to write. @@ -184,7 +190,8 @@ public interface Buf extends Rc { /** * Set the given unsigned byte value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Byte#BYTES}. - * The value is written using an unsigned two's complement 8-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The int value to write. * @return This Buf. @@ -194,7 +201,8 @@ public interface Buf extends Rc { /** * Set the given unsigned byte value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 8-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 8-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -207,7 +215,8 @@ public interface Buf extends Rc { /** * Get the char value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 2. - * The value is read using a 2-byte UTF-16 encoding, with big-endian byte order. + * The value is read using a 2-byte UTF-16 encoding, + * with the {@link #order() configured} default byte order. * * @return The char value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 2. @@ -217,7 +226,8 @@ public interface Buf extends Rc { /** * Get the char value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 2-byte UTF-16 encoding, with big-endian byte order. + * The value is read using a 2-byte UTF-16 encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The char value at the given offset. @@ -229,7 +239,8 @@ public interface Buf extends Rc { /** * Get the char value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 2. - * The value is read using a 2-byte UTF-16 encoding, with little-endian byte order. + * The value is read using a 2-byte UTF-16 encoding, + * with little-endian byte order. * * @return The char value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 2. @@ -239,7 +250,8 @@ public interface Buf extends Rc { /** * Get the char value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 2-byte UTF-16 encoding, with little-endian byte order. + * The value is read using a 2-byte UTF-16 encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The char value at the given offset. @@ -248,10 +260,35 @@ public interface Buf extends Rc { */ char readCharLE(int roff); + /** + * Get the char value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by 2. + * The value is read using a 2-byte UTF-16 encoding, + * with big-endian byte order. + * + * @return The char value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 2. + */ + char readCharBE(); + + /** + * Get the char value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a 2-byte UTF-16 encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The char value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 2. + */ + char readCharBE(int roff); + /** * Set the given char value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 2. - * The value is written using a 2-byte UTF-16 encoding, with big-endian byte order. + * The value is written using a 2-byte UTF-16 encoding, + * with the {@link #order() configured} default byte order. * * @param value The char value to write. * @return This Buf. @@ -261,7 +298,8 @@ public interface Buf extends Rc { /** * Set the given char value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 2-byte UTF-16 encoding, with big-endian byte order. + * The value is written using a 2-byte UTF-16 encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The char value to write. @@ -274,7 +312,8 @@ public interface Buf extends Rc { /** * Set the given char value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 2. - * The value is written using a 2-byte UTF-16 encoding, with little-endian byte order. + * The value is written using a 2-byte UTF-16 encoding, + * with little-endian byte order. * * @param value The char value to write. * @return This Buf. @@ -284,7 +323,8 @@ public interface Buf extends Rc { /** * Set the given char value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 2-byte UTF-16 encoding, with little-endian byte order. + * The value is written using a 2-byte UTF-16 encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The char value to write. @@ -294,10 +334,36 @@ public interface Buf extends Rc { */ Buf writeCharLE(int woff, char value); + /** + * Set the given char value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by 2. + * The value is written using a 2-byte UTF-16 encoding, + * with big-endian byte order. + * + * @param value The char value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than 2. + */ + Buf writeCharBE(char value); + + /** + * Set the given char value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a 2-byte UTF-16 encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The char value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 2. + */ + Buf writeCharBE(int woff, char value); + /** * Get the short value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Short#BYTES}. - * The value is read using a two's complement 16-bit encoding, with big-endian byte order. + * The value is read using a two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The short value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. @@ -307,7 +373,8 @@ public interface Buf extends Rc { /** * Get the short value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 16-bit encoding, with big-endian byte order. + * The value is read using a two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The short value at the given offset. @@ -319,7 +386,8 @@ public interface Buf extends Rc { /** * Get the short value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Short#BYTES}. - * The value is read using a two's complement 16-bit encoding, with little-endian byte order. + * The value is read using a two's complement 16-bit encoding, + * with little-endian byte order. * * @return The short value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. @@ -329,7 +397,8 @@ public interface Buf extends Rc { /** * Get the short value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 16-bit encoding, with little-endian byte order. + * The value is read using a two's complement 16-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The short value at the given offset. @@ -338,10 +407,35 @@ public interface Buf extends Rc { */ short readShortLE(int roff); + /** + * Get the short value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Short#BYTES}. + * The value is read using a two's complement 16-bit encoding, + * with big-endian byte order. + * + * @return The short value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. + */ + short readShortBE(); + + /** + * Get the short value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The short value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Short#BYTES}. + */ + short readShortBE(int roff); + /** * Get the unsigned short value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Short#BYTES}. - * The value is read using an unsigned two's complement 16-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The unsigned short value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. @@ -351,7 +445,8 @@ public interface Buf extends Rc { /** * Get the unsigned short value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 16-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned short value at the given offset. @@ -363,7 +458,8 @@ public interface Buf extends Rc { /** * Get the unsigned short value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Short#BYTES}. - * The value is read using an unsigned two's complement 16-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 16-bit encoding, + * with little-endian byte order. * * @return The unsigned short value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. @@ -373,7 +469,8 @@ public interface Buf extends Rc { /** * Get the unsigned short value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 16-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 16-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned short value at the given offset. @@ -382,10 +479,35 @@ public interface Buf extends Rc { */ int readUnsignedShortLE(int roff); + /** + * Get the unsigned short value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Short#BYTES}. + * The value is read using an unsigned two's complement 16-bit encoding, + * with big-endian byte order. + * + * @return The unsigned short value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Short#BYTES}. + */ + int readUnsignedShortBE(); + + /** + * Get the unsigned short value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using an unsigned two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The unsigned short value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Short#BYTES}. + */ + int readUnsignedShortBE(int roff); + /** * Set the given short value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Short#BYTES}. - * The value is written using a two's complement 16-bit encoding, with big-endian byte order. + * The value is written using a two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The short value to write. * @return This Buf. @@ -395,7 +517,8 @@ public interface Buf extends Rc { /** * Set the given short value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 16-bit encoding, with big-endian byte order. + * The value is written using a two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The short value to write. @@ -408,7 +531,8 @@ public interface Buf extends Rc { /** * Set the given short value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Short#BYTES}. - * The value is written using a two's complement 16-bit encoding, with little-endian byte order. + * The value is written using a two's complement 16-bit encoding, + * with little-endian byte order. * * @param value The short value to write. * @return This Buf. @@ -418,7 +542,8 @@ public interface Buf extends Rc { /** * Set the given short value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 16-bit encoding, with little-endian byte order. + * The value is written using a two's complement 16-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The short value to write. @@ -428,10 +553,36 @@ public interface Buf extends Rc { */ Buf writeShortLE(int woff, short value); + /** + * Set the given short value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Short#BYTES}. + * The value is written using a two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param value The short value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Short#BYTES}. + */ + Buf writeShortBE(short value); + + /** + * Set the given short value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The short value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Short#BYTES}. + */ + Buf writeShortBE(int woff, short value); + /** * Set the given unsigned short value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Short#BYTES}. - * The value is written using an unsigned two's complement 16-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The int value to write. * @return This Buf. @@ -441,7 +592,8 @@ public interface Buf extends Rc { /** * Set the given unsigned short value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 16-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 16-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -454,7 +606,8 @@ public interface Buf extends Rc { /** * Set the given unsigned short value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Short#BYTES}. - * The value is written using an unsigned two's complement 16-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 16-bit encoding, + * with little-endian byte order. * * @param value The int value to write. * @return This Buf. @@ -464,7 +617,8 @@ public interface Buf extends Rc { /** * Set the given unsigned short value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 16-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 16-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -474,10 +628,36 @@ public interface Buf extends Rc { */ Buf writeUnsignedShortLE(int woff, int value); + /** + * Set the given unsigned short value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Short#BYTES}. + * The value is written using an unsigned two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Short#BYTES}. + */ + Buf writeUnsignedShortBE(int value); + + /** + * Set the given unsigned short value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using an unsigned two's complement 16-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Short#BYTES}. + */ + Buf writeUnsignedShortBE(int woff, int value); + /** * Get the int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 3. - * The value is read using a two's complement 24-bit encoding, with big-endian byte order. + * The value is read using a two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. @@ -487,7 +667,8 @@ public interface Buf extends Rc { /** * Get the int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 24-bit encoding, with big-endian byte order. + * The value is read using a two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The int value at the given offset. @@ -499,7 +680,8 @@ public interface Buf extends Rc { /** * Get the int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 3. - * The value is read using a two's complement 24-bit encoding, with little-endian byte order. + * The value is read using a two's complement 24-bit encoding, + * with little-endian byte order. * * @return The int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. @@ -509,7 +691,8 @@ public interface Buf extends Rc { /** * Get the int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 24-bit encoding, with little-endian byte order. + * The value is read using a two's complement 24-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The int value at the given offset. @@ -518,10 +701,35 @@ public interface Buf extends Rc { */ int readMediumLE(int roff); + /** + * Get the int value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by 3. + * The value is read using a two's complement 24-bit encoding, + * with big-endian byte order. + * + * @return The int value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. + */ + int readMediumBE(); + + /** + * Get the int value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The int value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 3. + */ + int readMediumBE(int roff); + /** * Get the unsigned int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 3. - * The value is read using an unsigned two's complement 24-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The unsigned int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. @@ -531,7 +739,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 24-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned int value at the given offset. @@ -543,7 +752,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by 3. - * The value is read using an unsigned two's complement 24-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 24-bit encoding, + * with little-endian byte order. * * @return The unsigned int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. @@ -553,7 +763,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 24-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 24-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned int value at the given offset. @@ -562,10 +773,35 @@ public interface Buf extends Rc { */ int readUnsignedMediumLE(int roff); + /** + * Get the unsigned int value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by 3. + * The value is read using an unsigned two's complement 24-bit encoding, + * with big-endian byte order. + * + * @return The unsigned int value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than 3. + */ + int readUnsignedMediumBE(); + + /** + * Get the unsigned int value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using an unsigned two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The unsigned int value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 3. + */ + int readUnsignedMediumBE(int roff); + /** * Set the given int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 3. - * The value is written using a two's complement 24-bit encoding, with big-endian byte order. + * The value is written using a two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The int value to write. * @return This Buf. @@ -575,7 +811,8 @@ public interface Buf extends Rc { /** * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 24-bit encoding, with big-endian byte order. + * The value is written using a two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -588,7 +825,8 @@ public interface Buf extends Rc { /** * Set the given int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 3. - * The value is written using a two's complement 24-bit encoding, with little-endian byte order. + * The value is written using a two's complement 24-bit encoding, + * with little-endian byte order. * * @param value The int value to write. * @return This Buf. @@ -598,7 +836,8 @@ public interface Buf extends Rc { /** * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 24-bit encoding, with little-endian byte order. + * The value is written using a two's complement 24-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -608,10 +847,36 @@ public interface Buf extends Rc { */ Buf writeMediumLE(int woff, int value); + /** + * Set the given int value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by 3. + * The value is written using a two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than 3. + */ + Buf writeMediumBE(int value); + + /** + * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 3. + */ + Buf writeMediumBE(int woff, int value); + /** * Set the given unsigned int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 3. - * The value is written using an unsigned two's complement 24-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The int value to write. * @return This Buf. @@ -621,7 +886,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 24-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 24-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -634,7 +900,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by 3. - * The value is written using an unsigned two's complement 24-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 24-bit encoding, + * with little-endian byte order. * * @param value The int value to write. * @return This Buf. @@ -644,7 +911,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 24-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 24-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -654,10 +922,36 @@ public interface Buf extends Rc { */ Buf writeUnsignedMediumLE(int woff, int value); + /** + * Set the given unsigned int value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by 3. + * The value is written using an unsigned two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than 3. + */ + Buf writeUnsignedMediumBE(int value); + + /** + * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using an unsigned two's complement 24-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus 3. + */ + Buf writeUnsignedMediumBE(int woff, int value); + /** * Get the int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Integer#BYTES}. - * The value is read using a two's complement 32-bit encoding, with big-endian byte order. + * The value is read using a two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. @@ -667,7 +961,8 @@ public interface Buf extends Rc { /** * Get the int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 32-bit encoding, with big-endian byte order. + * The value is read using a two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The int value at the given offset. @@ -679,7 +974,8 @@ public interface Buf extends Rc { /** * Get the int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Integer#BYTES}. - * The value is read using a two's complement 32-bit encoding, with little-endian byte order. + * The value is read using a two's complement 32-bit encoding, + * with little-endian byte order. * * @return The int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. @@ -689,7 +985,8 @@ public interface Buf extends Rc { /** * Get the int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 32-bit encoding, with little-endian byte order. + * The value is read using a two's complement 32-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The int value at the given offset. @@ -698,10 +995,35 @@ public interface Buf extends Rc { */ int readIntLE(int roff); + /** + * Get the int value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Integer#BYTES}. + * The value is read using a two's complement 32-bit encoding, + * with big-endian byte order. + * + * @return The int value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. + */ + int readIntBE(); + + /** + * Get the int value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The int value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Integer#BYTES}. + */ + int readIntBE(int roff); + /** * Get the unsigned int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Integer#BYTES}. - * The value is read using an unsigned two's complement 32-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The unsigned int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. @@ -711,7 +1033,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 32-bit encoding, with big-endian byte order. + * The value is read using an unsigned two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned int value at the given offset. @@ -723,7 +1046,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Integer#BYTES}. - * The value is read using an unsigned two's complement 32-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 32-bit encoding, + * with little-endian byte order. * * @return The unsigned int value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. @@ -733,7 +1057,8 @@ public interface Buf extends Rc { /** * Get the unsigned int value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using an unsigned two's complement 32-bit encoding, with little-endian byte order. + * The value is read using an unsigned two's complement 32-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The unsigned int value at the given offset. @@ -742,10 +1067,35 @@ public interface Buf extends Rc { */ long readUnsignedIntLE(int roff); + /** + * Get the unsigned int value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Integer#BYTES}. + * The value is read using an unsigned two's complement 32-bit encoding, + * with big-endian byte order. + * + * @return The unsigned int value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Integer#BYTES}. + */ + long readUnsignedIntBE(); + + /** + * Get the unsigned int value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using an unsigned two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The unsigned int value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Integer#BYTES}. + */ + long readUnsignedIntBE(int roff); + /** * Set the given int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Integer#BYTES}. - * The value is written using a two's complement 32-bit encoding, with big-endian byte order. + * The value is written using a two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The int value to write. * @return This Buf. @@ -755,7 +1105,8 @@ public interface Buf extends Rc { /** * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 32-bit encoding, with big-endian byte order. + * The value is written using a two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -768,7 +1119,8 @@ public interface Buf extends Rc { /** * Set the given int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Integer#BYTES}. - * The value is written using a two's complement 32-bit encoding, with little-endian byte order. + * The value is written using a two's complement 32-bit encoding, + * with little-endian byte order. * * @param value The int value to write. * @return This Buf. @@ -778,7 +1130,8 @@ public interface Buf extends Rc { /** * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 32-bit encoding, with little-endian byte order. + * The value is written using a two's complement 32-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The int value to write. @@ -788,10 +1141,36 @@ public interface Buf extends Rc { */ Buf writeIntLE(int woff, int value); + /** + * Set the given int value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Integer#BYTES}. + * The value is written using a two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Integer#BYTES}. + */ + Buf writeIntBE(int value); + + /** + * Set the given int value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The int value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Integer#BYTES}. + */ + Buf writeIntBE(int woff, int value); + /** * Set the given unsigned int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Integer#BYTES}. - * The value is written using an unsigned two's complement 32-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The long value to write. * @return This Buf. @@ -801,7 +1180,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 32-bit encoding, with big-endian byte order. + * The value is written using an unsigned two's complement 32-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The long value to write. @@ -814,7 +1194,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Integer#BYTES}. - * The value is written using an unsigned two's complement 32-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 32-bit encoding, + * with little-endian byte order. * * @param value The long value to write. * @return This Buf. @@ -824,7 +1205,8 @@ public interface Buf extends Rc { /** * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using an unsigned two's complement 32-bit encoding, with little-endian byte order. + * The value is written using an unsigned two's complement 32-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The long value to write. @@ -834,10 +1216,36 @@ public interface Buf extends Rc { */ Buf writeUnsignedIntLE(int woff, long value); + /** + * Set the given unsigned int value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Integer#BYTES}. + * The value is written using an unsigned two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param value The long value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Integer#BYTES}. + */ + Buf writeUnsignedIntBE(long value); + + /** + * Set the given unsigned int value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using an unsigned two's complement 32-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The long value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Integer#BYTES}. + */ + Buf writeUnsignedIntBE(int woff, long value); + /** * Get the float value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Float#BYTES}. - * The value is read using a 32-bit IEEE floating point encoding, with big-endian byte order. + * The value is read using a 32-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @return The float value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Float#BYTES}. @@ -847,7 +1255,8 @@ public interface Buf extends Rc { /** * Get the float value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 32-bit IEEE floating point encoding, with big-endian byte order. + * The value is read using a 32-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The float value at the given offset. @@ -859,7 +1268,8 @@ public interface Buf extends Rc { /** * Get the float value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Float#BYTES}. - * The value is read using a 32-bit IEEE floating point encoding, with little-endian byte order. + * The value is read using a 32-bit IEEE floating point encoding, + * with little-endian byte order. * * @return The float value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Float#BYTES}. @@ -869,7 +1279,8 @@ public interface Buf extends Rc { /** * Get the float value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 32-bit IEEE floating point encoding, with little-endian byte order. + * The value is read using a 32-bit IEEE floating point encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The float value at the given offset. @@ -878,10 +1289,35 @@ public interface Buf extends Rc { */ float readFloatLE(int roff); + /** + * Get the float value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Float#BYTES}. + * The value is read using a 32-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @return The float value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Float#BYTES}. + */ + float readFloatBE(); + + /** + * Get the float value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a 32-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The float value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Float#BYTES}. + */ + float readFloatBE(int roff); + /** * Set the given float value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Float#BYTES}. - * The value is written using a 32-bit IEEE floating point encoding, with big-endian byte order. + * The value is written using a 32-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param value The float value to write. * @return This Buf. @@ -891,7 +1327,8 @@ public interface Buf extends Rc { /** * Set the given float value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 32-bit IEEE floating point encoding, with big-endian byte order. + * The value is written using a 32-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The float value to write. @@ -904,7 +1341,8 @@ public interface Buf extends Rc { /** * Set the given float value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Float#BYTES}. - * The value is written using a 32-bit IEEE floating point encoding, with little-endian byte order. + * The value is written using a 32-bit IEEE floating point encoding, + * with little-endian byte order. * * @param value The float value to write. * @return This Buf. @@ -914,7 +1352,8 @@ public interface Buf extends Rc { /** * Set the given float value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 32-bit IEEE floating point encoding, with little-endian byte order. + * The value is written using a 32-bit IEEE floating point encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The float value to write. @@ -924,10 +1363,36 @@ public interface Buf extends Rc { */ Buf writeFloatLE(int woff, float value); + /** + * Set the given float value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Float#BYTES}. + * The value is written using a 32-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param value The float value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Float#BYTES}. + */ + Buf writeFloatBE(float value); + + /** + * Set the given float value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a 32-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The float value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Float#BYTES}. + */ + Buf writeFloatBE(int woff, float value); + /** * Get the long value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Long#BYTES}. - * The value is read using a two's complement 64-bit encoding, with big-endian byte order. + * The value is read using a two's complement 64-bit encoding, + * with the {@link #order() configured} default byte order. * * @return The long value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Long#BYTES}. @@ -937,7 +1402,8 @@ public interface Buf extends Rc { /** * Get the long value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 64-bit encoding, with big-endian byte order. + * The value is read using a two's complement 64-bit encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The long value at the given offset. @@ -949,7 +1415,8 @@ public interface Buf extends Rc { /** * Get the long value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Long#BYTES}. - * The value is read using a two's complement 64-bit encoding, with little-endian byte order. + * The value is read using a two's complement 64-bit encoding, + * with little-endian byte order. * * @return The long value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Long#BYTES}. @@ -959,7 +1426,8 @@ public interface Buf extends Rc { /** * Get the long value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a two's complement 64-bit encoding, with little-endian byte order. + * The value is read using a two's complement 64-bit encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The long value at the given offset. @@ -968,10 +1436,35 @@ public interface Buf extends Rc { */ long readLongLE(int roff); + /** + * Get the long value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Long#BYTES}. + * The value is read using a two's complement 64-bit encoding, + * with big-endian byte order. + * + * @return The long value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Long#BYTES}. + */ + long readLongBE(); + + /** + * Get the long value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a two's complement 64-bit encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The long value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Long#BYTES}. + */ + long readLongBE(int roff); + /** * Set the given long value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Long#BYTES}. - * The value is written using a two's complement 64-bit encoding, with big-endian byte order. + * The value is written using a two's complement 64-bit encoding, + * with the {@link #order() configured} default byte order. * * @param value The long value to write. * @return This Buf. @@ -981,7 +1474,8 @@ public interface Buf extends Rc { /** * Set the given long value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 64-bit encoding, with big-endian byte order. + * The value is written using a two's complement 64-bit encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The long value to write. @@ -994,7 +1488,8 @@ public interface Buf extends Rc { /** * Set the given long value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Long#BYTES}. - * The value is written using a two's complement 64-bit encoding, with little-endian byte order. + * The value is written using a two's complement 64-bit encoding, + * with little-endian byte order. * * @param value The long value to write. * @return This Buf. @@ -1004,7 +1499,8 @@ public interface Buf extends Rc { /** * Set the given long value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a two's complement 64-bit encoding, with little-endian byte order. + * The value is written using a two's complement 64-bit encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The long value to write. @@ -1014,10 +1510,36 @@ public interface Buf extends Rc { */ Buf writeLongLE(int woff, long value); + /** + * Set the given long value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Long#BYTES}. + * The value is written using a two's complement 64-bit encoding, + * with big-endian byte order. + * + * @param value The long value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Long#BYTES}. + */ + Buf writeLongBE(long value); + + /** + * Set the given long value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a two's complement 64-bit encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The long value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Long#BYTES}. + */ + Buf writeLongBE(int woff, long value); + /** * Get the double value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Double#BYTES}. - * The value is read using a 64-bit IEEE floating point encoding, with big-endian byte order. + * The value is read using a 64-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @return The double value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Double#BYTES}. @@ -1027,7 +1549,8 @@ public interface Buf extends Rc { /** * Get the double value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 64-bit IEEE floating point encoding, with big-endian byte order. + * The value is read using a 64-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The double value at the given offset. @@ -1039,7 +1562,8 @@ public interface Buf extends Rc { /** * Get the double value at the current {@link Buf#readerIndex()}, * and increases the reader offset by {@link Double#BYTES}. - * The value is read using a 64-bit IEEE floating point encoding, with little-endian byte order. + * The value is read using a 64-bit IEEE floating point encoding, + * with little-endian byte order. * * @return The double value at the current reader offset. * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Double#BYTES}. @@ -1049,7 +1573,8 @@ public interface Buf extends Rc { /** * Get the double value at the given reader offset. * The {@link Buf#readerIndex()} is not modified. - * The value is read using a 64-bit IEEE floating point encoding, with little-endian byte order. + * The value is read using a 64-bit IEEE floating point encoding, + * with little-endian byte order. * * @param roff The read offset, an absolute index into this buffer, to read from. * @return The double value at the given offset. @@ -1058,10 +1583,35 @@ public interface Buf extends Rc { */ double readDoubleLE(int roff); + /** + * Get the double value at the current {@link Buf#readerIndex()}, + * and increases the reader offset by {@link Double#BYTES}. + * The value is read using a 64-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @return The double value at the current reader offset. + * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than {@link Double#BYTES}. + */ + double readDoubleBE(); + + /** + * Get the double value at the given reader offset. + * The {@link Buf#readerIndex()} is not modified. + * The value is read using a 64-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param roff The read offset, an absolute index into this buffer, to read from. + * @return The double value at the given offset. + * @throws IndexOutOfBoundsException if the given index is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Double#BYTES}. + */ + double readDoubleBE(int roff); + /** * Set the given double value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Double#BYTES}. - * The value is written using a 64-bit IEEE floating point encoding, with big-endian byte order. + * The value is written using a 64-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param value The double value to write. * @return This Buf. @@ -1071,7 +1621,8 @@ public interface Buf extends Rc { /** * Set the given double value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 64-bit IEEE floating point encoding, with big-endian byte order. + * The value is written using a 64-bit IEEE floating point encoding, + * with the {@link #order() configured} default byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The double value to write. @@ -1084,7 +1635,8 @@ public interface Buf extends Rc { /** * Set the given double value at the current {@link Buf#writerIndex()}, * and increase the writer offset by {@link Double#BYTES}. - * The value is written using a 64-bit IEEE floating point encoding, with little-endian byte order. + * The value is written using a 64-bit IEEE floating point encoding, + * with little-endian byte order. * * @param value The double value to write. * @return This Buf. @@ -1094,7 +1646,8 @@ public interface Buf extends Rc { /** * Set the given double value at the given write offset. The {@link Buf#writerIndex()} is not modified. - * The value is written using a 64-bit IEEE floating point encoding, with little-endian byte order. + * The value is written using a 64-bit IEEE floating point encoding, + * with little-endian byte order. * * @param woff The write offset, an absolute index into this buffer to write to. * @param value The double value to write. @@ -1103,6 +1656,31 @@ public interface Buf extends Rc { * greater than or equal to {@link Buf#capacity()} minus {@link Double#BYTES}. */ Buf writeDoubleLE(int woff, double value); + + /** + * Set the given double value at the current {@link Buf#writerIndex()}, + * and increase the writer offset by {@link Double#BYTES}. + * The value is written using a 64-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param value The double value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than {@link Double#BYTES}. + */ + Buf writeDoubleBE(double value); + + /** + * Set the given double value at the given write offset. The {@link Buf#writerIndex()} is not modified. + * The value is written using a 64-bit IEEE floating point encoding, + * with big-endian byte order. + * + * @param woff The write offset, an absolute index into this buffer to write to. + * @param value The double value to write. + * @return This Buf. + * @throws IndexOutOfBoundsException if the given offset is out of bounds of the buffer, that is, less than 0 or + * greater than or equal to {@link Buf#capacity()} minus {@link Double#BYTES}. + */ + Buf writeDoubleBE(int woff, double value); // // ### CODEGEN END primitive accessors interface } \ No newline at end of file diff --git a/buffer/src/test/java/io/netty/buffer/b2/BBufTest.java b/buffer/src/test/java/io/netty/buffer/b2/BBufTest.java index e1995f5..899e65d 100644 --- a/buffer/src/test/java/io/netty/buffer/b2/BBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/b2/BBufTest.java @@ -20,6 +20,7 @@ import org.junit.AssumptionViolatedException; import org.junit.Before; import org.junit.Test; +import java.nio.ByteOrder; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -311,7 +312,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfByteMustReadWithBigEndianByteOrder() { + public void relativeReadOfByteMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); byte value = 0x01; @@ -359,7 +361,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfByteMustReadWithBigEndianByteOrder() { + public void offsettedReadOfByteMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); byte value = 0x01; buf.writeByte(value); buf.writeByte(0, (byte) 0x10); @@ -401,7 +404,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfUnsignedByteMustReadWithBigEndianByteOrder() { + public void relativeReadOfUnsignedByteMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); int value = 0x01; @@ -449,7 +453,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfUnsignedByteMustReadWithBigEndianByteOrder() { + public void offsettedReadOfUnsignedByteMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01; buf.writeUnsignedByte(value); buf.writeByte(0, (byte) 0x10); @@ -495,7 +500,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfByteMustHaveBigEndianByteOrder() { + public void relativeWriteOfByteMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); byte value = 0x01; buf.writeByte(value); buf.writerIndex(Long.BYTES); @@ -540,7 +546,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfByteMustHaveBigEndianByteOrder() { + public void offsettedWriteOfByteMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); byte value = 0x01; buf.writeByte(0, value); buf.writerIndex(Long.BYTES); @@ -571,7 +578,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfUnsignedByteMustHaveBigEndianByteOrder() { + public void relativeWriteOfUnsignedByteMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01; buf.writeUnsignedByte(value); buf.writerIndex(Long.BYTES); @@ -616,7 +624,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfUnsignedByteMustHaveBigEndianByteOrder() { + public void offsettedWriteOfUnsignedByteMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01; buf.writeUnsignedByte(0, value); buf.writerIndex(Long.BYTES); @@ -643,7 +652,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfCharMustReadWithBigEndianByteOrder() { + public void relativeReadOfCharMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); char value = 0x0102; @@ -691,7 +701,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfCharMustReadWithBigEndianByteOrder() { + public void offsettedReadOfCharMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); char value = 0x0102; buf.writeChar(value); buf.writeByte(0, (byte) 0x10); @@ -810,6 +821,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfCharBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + char value = 0x0102; + buf.writeCharBE(value); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(value, buf.readCharBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfCharBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + char value = 0x0102; + buf.writeCharBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(0x1002, buf.readCharBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfCharBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + char value = 0x0102; + buf.writeCharBE(value); + buf.readerIndex(1); + assertEquals(1, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + try { + buf.readCharBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(1, buf.readableBytes()); + } + + @Test + public void offsettedReadOfCharBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readCharBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfCharBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + char value = 0x0102; + buf.writeCharBE(value); + assertEquals(value, buf.readCharBE(0)); + } + + @Test + public void offsettedReadOfCharBEMustReadWithBigEndianByteOrder() { + char value = 0x0102; + buf.writeCharBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x1002, buf.readCharBE(0)); + } + + @Test + public void offsettedReadOfCharBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + char value = 0x0102; + buf.writeCharBE(value); + try { + buf.readCharBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfCharBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readCharBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfCharMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -827,7 +928,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfCharMustHaveBigEndianByteOrder() { + public void relativeWriteOfCharMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); char value = 0x0102; buf.writeChar(value); buf.writerIndex(Long.BYTES); @@ -872,7 +974,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfCharMustHaveBigEndianByteOrder() { + public void offsettedWriteOfCharMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); char value = 0x0102; buf.writeChar(0, value); buf.writerIndex(Long.BYTES); @@ -962,6 +1065,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfCharBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(7); + try { + char value = 0x0102; + buf.writeCharBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfCharBEMustHaveBigEndianByteOrder() { + char value = 0x0102; + buf.writeCharBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfCharBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + char value = 0x0102; + buf.writeCharBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfCharBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + char value = 0x0102; + buf.writeCharBE(7, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfCharBEMustHaveBigEndianByteOrder() { + char value = 0x0102; + buf.writeCharBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeReadOfShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -975,7 +1154,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfShortMustReadWithBigEndianByteOrder() { + public void relativeReadOfShortMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); short value = 0x0102; @@ -1023,7 +1203,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfShortMustReadWithBigEndianByteOrder() { + public void offsettedReadOfShortMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); short value = 0x0102; buf.writeShort(value); buf.writeByte(0, (byte) 0x10); @@ -1142,6 +1323,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfShortBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + short value = 0x0102; + buf.writeShortBE(value); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(value, buf.readShortBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfShortBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + short value = 0x0102; + buf.writeShortBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(0x1002, buf.readShortBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfShortBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + short value = 0x0102; + buf.writeShortBE(value); + buf.readerIndex(1); + assertEquals(1, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + try { + buf.readShortBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(1, buf.readableBytes()); + } + + @Test + public void offsettedReadOfShortBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readShortBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfShortBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + short value = 0x0102; + buf.writeShortBE(value); + assertEquals(value, buf.readShortBE(0)); + } + + @Test + public void offsettedReadOfShortBEMustReadWithBigEndianByteOrder() { + short value = 0x0102; + buf.writeShortBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x1002, buf.readShortBE(0)); + } + + @Test + public void offsettedReadOfShortBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + short value = 0x0102; + buf.writeShortBE(value); + try { + buf.readShortBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfShortBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readShortBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeReadOfUnsignedShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -1155,7 +1426,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfUnsignedShortMustReadWithBigEndianByteOrder() { + public void relativeReadOfUnsignedShortMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); int value = 0x0102; @@ -1203,7 +1475,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfUnsignedShortMustReadWithBigEndianByteOrder() { + public void offsettedReadOfUnsignedShortMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x0102; buf.writeUnsignedShort(value); buf.writeByte(0, (byte) 0x10); @@ -1322,6 +1595,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfUnsignedShortBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x0102; + buf.writeUnsignedShortBE(value); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(value, buf.readUnsignedShortBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedShortBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x0102; + buf.writeUnsignedShortBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(2, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + assertEquals(0x1002, buf.readUnsignedShortBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedShortBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x0102; + buf.writeUnsignedShortBE(value); + buf.readerIndex(1); + assertEquals(1, buf.readableBytes()); + assertEquals(6, buf.writableBytes()); + try { + buf.readUnsignedShortBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(1, buf.readableBytes()); + } + + @Test + public void offsettedReadOfUnsignedShortBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readUnsignedShortBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedShortBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + int value = 0x0102; + buf.writeUnsignedShortBE(value); + assertEquals(value, buf.readUnsignedShortBE(0)); + } + + @Test + public void offsettedReadOfUnsignedShortBEMustReadWithBigEndianByteOrder() { + int value = 0x0102; + buf.writeUnsignedShortBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x1002, buf.readUnsignedShortBE(0)); + } + + @Test + public void offsettedReadOfUnsignedShortBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + int value = 0x0102; + buf.writeUnsignedShortBE(value); + try { + buf.readUnsignedShortBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedShortBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readUnsignedShortBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -1339,7 +1702,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfShortMustHaveBigEndianByteOrder() { + public void relativeWriteOfShortMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); short value = 0x0102; buf.writeShort(value); buf.writerIndex(Long.BYTES); @@ -1384,7 +1748,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfShortMustHaveBigEndianByteOrder() { + public void offsettedWriteOfShortMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); short value = 0x0102; buf.writeShort(0, value); buf.writerIndex(Long.BYTES); @@ -1474,6 +1839,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfShortBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(7); + try { + short value = 0x0102; + buf.writeShortBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfShortBEMustHaveBigEndianByteOrder() { + short value = 0x0102; + buf.writeShortBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfShortBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + short value = 0x0102; + buf.writeShortBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfShortBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + short value = 0x0102; + buf.writeShortBE(7, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfShortBEMustHaveBigEndianByteOrder() { + short value = 0x0102; + buf.writeShortBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeWriteOfUnsignedShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -1491,7 +1932,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfUnsignedShortMustHaveBigEndianByteOrder() { + public void relativeWriteOfUnsignedShortMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x0102; buf.writeUnsignedShort(value); buf.writerIndex(Long.BYTES); @@ -1536,7 +1978,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfUnsignedShortMustHaveBigEndianByteOrder() { + public void offsettedWriteOfUnsignedShortMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x0102; buf.writeUnsignedShort(0, value); buf.writerIndex(Long.BYTES); @@ -1626,6 +2069,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfUnsignedShortBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(7); + try { + int value = 0x0102; + buf.writeUnsignedShortBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfUnsignedShortBEMustHaveBigEndianByteOrder() { + int value = 0x0102; + buf.writeUnsignedShortBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfUnsignedShortBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x0102; + buf.writeUnsignedShortBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedShortBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x0102; + buf.writeUnsignedShortBE(7, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedShortBEMustHaveBigEndianByteOrder() { + int value = 0x0102; + buf.writeUnsignedShortBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeReadOfMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -1639,7 +2158,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfMediumMustReadWithBigEndianByteOrder() { + public void relativeReadOfMediumMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); int value = 0x010203; @@ -1687,7 +2207,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfMediumMustReadWithBigEndianByteOrder() { + public void offsettedReadOfMediumMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeMedium(value); buf.writeByte(0, (byte) 0x10); @@ -1806,6 +2327,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfMediumBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeMediumBE(value); + assertEquals(3, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + assertEquals(value, buf.readMediumBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfMediumBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeMediumBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(3, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + assertEquals(0x100203, buf.readMediumBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfMediumBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeMediumBE(value); + buf.readerIndex(1); + assertEquals(2, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + try { + buf.readMediumBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(2, buf.readableBytes()); + } + + @Test + public void offsettedReadOfMediumBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readMediumBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfMediumBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + int value = 0x010203; + buf.writeMediumBE(value); + assertEquals(value, buf.readMediumBE(0)); + } + + @Test + public void offsettedReadOfMediumBEMustReadWithBigEndianByteOrder() { + int value = 0x010203; + buf.writeMediumBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x100203, buf.readMediumBE(0)); + } + + @Test + public void offsettedReadOfMediumBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + int value = 0x010203; + buf.writeMediumBE(value); + try { + buf.readMediumBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfMediumBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readMediumBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeReadOfUnsignedMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -1819,7 +2430,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfUnsignedMediumMustReadWithBigEndianByteOrder() { + public void relativeReadOfUnsignedMediumMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); int value = 0x010203; @@ -1867,7 +2479,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfUnsignedMediumMustReadWithBigEndianByteOrder() { + public void offsettedReadOfUnsignedMediumMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeUnsignedMedium(value); buf.writeByte(0, (byte) 0x10); @@ -1986,6 +2599,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfUnsignedMediumBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + assertEquals(3, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + assertEquals(value, buf.readUnsignedMediumBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedMediumBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(3, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + assertEquals(0x100203, buf.readUnsignedMediumBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedMediumBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + buf.readerIndex(1); + assertEquals(2, buf.readableBytes()); + assertEquals(5, buf.writableBytes()); + try { + buf.readUnsignedMediumBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(2, buf.readableBytes()); + } + + @Test + public void offsettedReadOfUnsignedMediumBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readUnsignedMediumBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedMediumBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + assertEquals(value, buf.readUnsignedMediumBE(0)); + } + + @Test + public void offsettedReadOfUnsignedMediumBEMustReadWithBigEndianByteOrder() { + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x100203, buf.readUnsignedMediumBE(0)); + } + + @Test + public void offsettedReadOfUnsignedMediumBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + try { + buf.readUnsignedMediumBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedMediumBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readUnsignedMediumBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -2003,7 +2706,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfMediumMustHaveBigEndianByteOrder() { + public void relativeWriteOfMediumMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeMedium(value); buf.writerIndex(Long.BYTES); @@ -2048,7 +2752,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfMediumMustHaveBigEndianByteOrder() { + public void offsettedWriteOfMediumMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeMedium(0, value); buf.writerIndex(Long.BYTES); @@ -2138,6 +2843,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfMediumBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(6); + try { + int value = 0x010203; + buf.writeMediumBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfMediumBEMustHaveBigEndianByteOrder() { + int value = 0x010203; + buf.writeMediumBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfMediumBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x010203; + buf.writeMediumBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfMediumBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x010203; + buf.writeMediumBE(6, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfMediumBEMustHaveBigEndianByteOrder() { + int value = 0x010203; + buf.writeMediumBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeWriteOfUnsignedMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -2155,7 +2936,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfUnsignedMediumMustHaveBigEndianByteOrder() { + public void relativeWriteOfUnsignedMediumMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeUnsignedMedium(value); buf.writerIndex(Long.BYTES); @@ -2200,7 +2982,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfUnsignedMediumMustHaveBigEndianByteOrder() { + public void offsettedWriteOfUnsignedMediumMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x010203; buf.writeUnsignedMedium(0, value); buf.writerIndex(Long.BYTES); @@ -2290,6 +3073,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfUnsignedMediumBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(6); + try { + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfUnsignedMediumBEMustHaveBigEndianByteOrder() { + int value = 0x010203; + buf.writeUnsignedMediumBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfUnsignedMediumBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x010203; + buf.writeUnsignedMediumBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedMediumBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x010203; + buf.writeUnsignedMediumBE(6, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedMediumBEMustHaveBigEndianByteOrder() { + int value = 0x010203; + buf.writeUnsignedMediumBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeReadOfIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -2303,7 +3162,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfIntMustReadWithBigEndianByteOrder() { + public void relativeReadOfIntMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); int value = 0x01020304; @@ -2351,7 +3211,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfIntMustReadWithBigEndianByteOrder() { + public void offsettedReadOfIntMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01020304; buf.writeInt(value); buf.writeByte(0, (byte) 0x10); @@ -2470,6 +3331,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfIntBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x01020304; + buf.writeIntBE(value); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(value, buf.readIntBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfIntBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x01020304; + buf.writeIntBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(0x10020304, buf.readIntBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfIntBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + int value = 0x01020304; + buf.writeIntBE(value); + buf.readerIndex(1); + assertEquals(3, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + try { + buf.readIntBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(3, buf.readableBytes()); + } + + @Test + public void offsettedReadOfIntBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readIntBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfIntBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + int value = 0x01020304; + buf.writeIntBE(value); + assertEquals(value, buf.readIntBE(0)); + } + + @Test + public void offsettedReadOfIntBEMustReadWithBigEndianByteOrder() { + int value = 0x01020304; + buf.writeIntBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x10020304, buf.readIntBE(0)); + } + + @Test + public void offsettedReadOfIntBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + int value = 0x01020304; + buf.writeIntBE(value); + try { + buf.readIntBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfIntBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readIntBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeReadOfUnsignedIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -2483,7 +3434,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfUnsignedIntMustReadWithBigEndianByteOrder() { + public void relativeReadOfUnsignedIntMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); long value = 0x01020304; @@ -2531,7 +3483,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfUnsignedIntMustReadWithBigEndianByteOrder() { + public void offsettedReadOfUnsignedIntMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x01020304; buf.writeUnsignedInt(value); buf.writeByte(0, (byte) 0x10); @@ -2650,6 +3603,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfUnsignedIntBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(value, buf.readUnsignedIntBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedIntBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(0x10020304, buf.readUnsignedIntBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfUnsignedIntBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + buf.readerIndex(1); + assertEquals(3, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + try { + buf.readUnsignedIntBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(3, buf.readableBytes()); + } + + @Test + public void offsettedReadOfUnsignedIntBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readUnsignedIntBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedIntBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + assertEquals(value, buf.readUnsignedIntBE(0)); + } + + @Test + public void offsettedReadOfUnsignedIntBEMustReadWithBigEndianByteOrder() { + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x10020304, buf.readUnsignedIntBE(0)); + } + + @Test + public void offsettedReadOfUnsignedIntBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + try { + buf.readUnsignedIntBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfUnsignedIntBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readUnsignedIntBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -2667,7 +3710,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfIntMustHaveBigEndianByteOrder() { + public void relativeWriteOfIntMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01020304; buf.writeInt(value); buf.writerIndex(Long.BYTES); @@ -2712,7 +3756,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfIntMustHaveBigEndianByteOrder() { + public void offsettedWriteOfIntMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); int value = 0x01020304; buf.writeInt(0, value); buf.writerIndex(Long.BYTES); @@ -2802,6 +3847,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfIntBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(5); + try { + int value = 0x01020304; + buf.writeIntBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfIntBEMustHaveBigEndianByteOrder() { + int value = 0x01020304; + buf.writeIntBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfIntBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x01020304; + buf.writeIntBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfIntBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + int value = 0x01020304; + buf.writeIntBE(5, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfIntBEMustHaveBigEndianByteOrder() { + int value = 0x01020304; + buf.writeIntBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeWriteOfUnsignedIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -2819,7 +3940,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfUnsignedIntMustHaveBigEndianByteOrder() { + public void relativeWriteOfUnsignedIntMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x01020304; buf.writeUnsignedInt(value); buf.writerIndex(Long.BYTES); @@ -2864,7 +3986,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfUnsignedIntMustHaveBigEndianByteOrder() { + public void offsettedWriteOfUnsignedIntMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x01020304; buf.writeUnsignedInt(0, value); buf.writerIndex(Long.BYTES); @@ -2954,6 +4077,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfUnsignedIntBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(5); + try { + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfUnsignedIntBEMustHaveBigEndianByteOrder() { + long value = 0x01020304; + buf.writeUnsignedIntBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfUnsignedIntBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + long value = 0x01020304; + buf.writeUnsignedIntBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedIntBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + long value = 0x01020304; + buf.writeUnsignedIntBE(5, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfUnsignedIntBEMustHaveBigEndianByteOrder() { + long value = 0x01020304; + buf.writeUnsignedIntBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeReadOfFloatMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -2967,7 +4166,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfFloatMustReadWithBigEndianByteOrder() { + public void relativeReadOfFloatMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); float value = Float.intBitsToFloat(0x01020304); @@ -3015,7 +4215,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfFloatMustReadWithBigEndianByteOrder() { + public void offsettedReadOfFloatMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); float value = Float.intBitsToFloat(0x01020304); buf.writeFloat(value); buf.writeByte(0, (byte) 0x10); @@ -3134,6 +4335,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfFloatBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(value, buf.readFloatBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfFloatBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(4, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + assertEquals(Float.intBitsToFloat(0x10020304), buf.readFloatBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfFloatBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + buf.readerIndex(1); + assertEquals(3, buf.readableBytes()); + assertEquals(4, buf.writableBytes()); + try { + buf.readFloatBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(3, buf.readableBytes()); + } + + @Test + public void offsettedReadOfFloatBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readFloatBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfFloatBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + assertEquals(value, buf.readFloatBE(0)); + } + + @Test + public void offsettedReadOfFloatBEMustReadWithBigEndianByteOrder() { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(Float.intBitsToFloat(0x10020304), buf.readFloatBE(0)); + } + + @Test + public void offsettedReadOfFloatBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + try { + buf.readFloatBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfFloatBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readFloatBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfFloatMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -3151,7 +4442,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfFloatMustHaveBigEndianByteOrder() { + public void relativeWriteOfFloatMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); float value = Float.intBitsToFloat(0x01020304); buf.writeFloat(value); buf.writerIndex(Long.BYTES); @@ -3196,7 +4488,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfFloatMustHaveBigEndianByteOrder() { + public void offsettedWriteOfFloatMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); float value = Float.intBitsToFloat(0x01020304); buf.writeFloat(0, value); buf.writerIndex(Long.BYTES); @@ -3286,6 +4579,82 @@ public abstract class BBufTest { assertEquals((byte) 0x00, buf.readByte()); } + @Test + public void relativeWriteOfFloatBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(5); + try { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfFloatBEMustHaveBigEndianByteOrder() { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + + @Test + public void offsettedWriteOfFloatBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfFloatBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(5, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfFloatBEMustHaveBigEndianByteOrder() { + float value = Float.intBitsToFloat(0x01020304); + buf.writeFloatBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + assertEquals((byte) 0x00, buf.readByte()); + } + @Test public void relativeReadOfLongMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -3299,7 +4668,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfLongMustReadWithBigEndianByteOrder() { + public void relativeReadOfLongMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); long value = 0x0102030405060708L; @@ -3347,7 +4717,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfLongMustReadWithBigEndianByteOrder() { + public void offsettedReadOfLongMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x0102030405060708L; buf.writeLong(value); buf.writeByte(0, (byte) 0x10); @@ -3466,6 +4837,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfLongBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x0102030405060708L; + buf.writeLongBE(value); + assertEquals(8, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + assertEquals(value, buf.readLongBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfLongBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x0102030405060708L; + buf.writeLongBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(8, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + assertEquals(0x1002030405060708L, buf.readLongBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfLongBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + long value = 0x0102030405060708L; + buf.writeLongBE(value); + buf.readerIndex(1); + assertEquals(7, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + try { + buf.readLongBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(7, buf.readableBytes()); + } + + @Test + public void offsettedReadOfLongBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readLongBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfLongBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + long value = 0x0102030405060708L; + buf.writeLongBE(value); + assertEquals(value, buf.readLongBE(0)); + } + + @Test + public void offsettedReadOfLongBEMustReadWithBigEndianByteOrder() { + long value = 0x0102030405060708L; + buf.writeLongBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(0x1002030405060708L, buf.readLongBE(0)); + } + + @Test + public void offsettedReadOfLongBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + long value = 0x0102030405060708L; + buf.writeLongBE(value); + try { + buf.readLongBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfLongBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readLongBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfLongMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -3483,7 +4944,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfLongMustHaveBigEndianByteOrder() { + public void relativeWriteOfLongMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x0102030405060708L; buf.writeLong(value); buf.writerIndex(Long.BYTES); @@ -3528,7 +4990,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfLongMustHaveBigEndianByteOrder() { + public void offsettedWriteOfLongMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); long value = 0x0102030405060708L; buf.writeLong(0, value); buf.writerIndex(Long.BYTES); @@ -3618,6 +5081,82 @@ public abstract class BBufTest { assertEquals((byte) 0x01, buf.readByte()); } + @Test + public void relativeWriteOfLongBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(1); + try { + long value = 0x0102030405060708L; + buf.writeLongBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfLongBEMustHaveBigEndianByteOrder() { + long value = 0x0102030405060708L; + buf.writeLongBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x05, buf.readByte()); + assertEquals((byte) 0x06, buf.readByte()); + assertEquals((byte) 0x07, buf.readByte()); + assertEquals((byte) 0x08, buf.readByte()); + } + + @Test + public void offsettedWriteOfLongBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + long value = 0x0102030405060708L; + buf.writeLongBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfLongBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + long value = 0x0102030405060708L; + buf.writeLongBE(1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfLongBEMustHaveBigEndianByteOrder() { + long value = 0x0102030405060708L; + buf.writeLongBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x05, buf.readByte()); + assertEquals((byte) 0x06, buf.readByte()); + assertEquals((byte) 0x07, buf.readByte()); + assertEquals((byte) 0x08, buf.readByte()); + } + @Test public void relativeReadOfDoubleMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { assertEquals(0, buf.readableBytes()); @@ -3631,7 +5170,8 @@ public abstract class BBufTest { } @Test - public void relativeReadOfDoubleMustReadWithBigEndianByteOrder() { + public void relativeReadOfDoubleMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); assertEquals(0, buf.readableBytes()); assertEquals(Long.BYTES, buf.writableBytes()); double value = Double.longBitsToDouble(0x0102030405060708L); @@ -3679,7 +5219,8 @@ public abstract class BBufTest { } @Test - public void offsettedReadOfDoubleMustReadWithBigEndianByteOrder() { + public void offsettedReadOfDoubleMustReadWithDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); double value = Double.longBitsToDouble(0x0102030405060708L); buf.writeDouble(value); buf.writeByte(0, (byte) 0x10); @@ -3798,6 +5339,96 @@ public abstract class BBufTest { } } + @Test + public void relativeReadOfDoubleBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + assertEquals(8, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + assertEquals(value, buf.readDoubleBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfDoubleBEMustReadWithBigEndianByteOrder() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(8, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + assertEquals(Double.longBitsToDouble(0x1002030405060708L), buf.readDoubleBE()); + assertEquals(0, buf.readableBytes()); + } + + @Test + public void relativeReadOfDoubleBEMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset() { + assertEquals(0, buf.readableBytes()); + assertEquals(Long.BYTES, buf.writableBytes()); + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + buf.readerIndex(1); + assertEquals(7, buf.readableBytes()); + assertEquals(0, buf.writableBytes()); + try { + buf.readDoubleBE(); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + assertEquals(7, buf.readableBytes()); + } + + @Test + public void offsettedReadOfDoubleBEMustBoundsCheckOnNegativeOffset() { + try { + buf.readDoubleBE(-1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfDoubleBEMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset() { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + assertEquals(value, buf.readDoubleBE(0)); + } + + @Test + public void offsettedReadOfDoubleBEMustReadWithBigEndianByteOrder() { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + buf.writeByte(0, (byte) 0x10); + assertEquals(Double.longBitsToDouble(0x1002030405060708L), buf.readDoubleBE(0)); + } + + @Test + public void offsettedReadOfDoubleBEMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset() { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + try { + buf.readDoubleBE(1); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + + @Test + public void offsettedReadOfDoubleBEMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset() { + try { + buf.readDoubleBE(0); + fail("Expected a bounds check."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + } + @Test public void relativeWriteOfDoubleMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { assertEquals(Long.BYTES, buf.capacity()); @@ -3815,7 +5446,8 @@ public abstract class BBufTest { } @Test - public void relativeWriteOfDoubleMustHaveBigEndianByteOrder() { + public void relativeWriteOfDoubleMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); double value = Double.longBitsToDouble(0x0102030405060708L); buf.writeDouble(value); buf.writerIndex(Long.BYTES); @@ -3860,7 +5492,8 @@ public abstract class BBufTest { } @Test - public void offsettedWriteOfDoubleMustHaveBigEndianByteOrder() { + public void offsettedWriteOfDoubleMustHaveDefaultEndianByteOrder() { + buf.order(ByteOrder.BIG_ENDIAN); double value = Double.longBitsToDouble(0x0102030405060708L); buf.writeDouble(0, value); buf.writerIndex(Long.BYTES); @@ -3949,6 +5582,82 @@ public abstract class BBufTest { assertEquals((byte) 0x02, buf.readByte()); assertEquals((byte) 0x01, buf.readByte()); } + + @Test + public void relativeWriteOfDoubleBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + buf.writerIndex(1); + try { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void relativeWriteOfDoubleBEMustHaveBigEndianByteOrder() { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x05, buf.readByte()); + assertEquals((byte) 0x06, buf.readByte()); + assertEquals((byte) 0x07, buf.readByte()); + assertEquals((byte) 0x08, buf.readByte()); + } + + @Test + public void offsettedWriteOfDoubleBEMustBoundsCheckWhenWriteOffsetIsNegative() { + assertEquals(Long.BYTES, buf.capacity()); + try { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(-1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfDoubleBEMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity() { + assertEquals(Long.BYTES, buf.capacity()); + try { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(1, value); + fail("Should have bounds checked."); + } catch (IndexOutOfBoundsException ignore) { + // Good. + } + buf.writerIndex(Long.BYTES); + // Verify contents are unchanged. + assertEquals(0, buf.readLong()); + } + + @Test + public void offsettedWriteOfDoubleBEMustHaveBigEndianByteOrder() { + double value = Double.longBitsToDouble(0x0102030405060708L); + buf.writeDoubleBE(0, value); + buf.writerIndex(Long.BYTES); + assertEquals((byte) 0x01, buf.readByte()); + assertEquals((byte) 0x02, buf.readByte()); + assertEquals((byte) 0x03, buf.readByte()); + assertEquals((byte) 0x04, buf.readByte()); + assertEquals((byte) 0x05, buf.readByte()); + assertEquals((byte) 0x06, buf.readByte()); + assertEquals((byte) 0x07, buf.readByte()); + assertEquals((byte) 0x08, buf.readByte()); + } // // ### CODEGEN END primitive accessors tests diff --git a/buffer/src/test/java/io/netty/buffer/b2/Codegen.java b/buffer/src/test/java/io/netty/buffer/b2/Codegen.java index 9d38eca..c65eede 100644 --- a/buffer/src/test/java/io/netty/buffer/b2/Codegen.java +++ b/buffer/src/test/java/io/netty/buffer/b2/Codegen.java @@ -22,50 +22,103 @@ public final class Codegen { "primitive accessors implementation", Codegen::primitiveAccessorsImplementation, "primitive accessors tests", Codegen::primitiveAccessorsTests); + enum Order { + DF /*default as configured for buffer*/, BE, LE; + + public String suffix() { + return this == DF? "" : name(); + } + + public String title() { + switch (this) { + case BE: return "Big"; + case LE: return "Little"; + } + return "Default"; + } + + public String endianDesc() { + switch (this) { + case BE: return "big-endian"; + case LE: return "little-endian"; + } + return "the {@link #order() configured} default"; + } + } + enum Type { BYTE("byte", "Byte", "Byte.BYTES", Byte.BYTES, false, "two's complement 8-bit", "int"), CHAR("char", "Char", "2", 2, true, "2-byte UTF-16", null), SHORT("short", "Short", "Short.BYTES", Short.BYTES, true, "two's complement 16-bit", "int"), MED("int", "Medium", "3", 3, true, "two's complement 24-bit", "int") { @Override - public String load(boolean le, boolean unsigned) { - String indent = " "; + public String load(Order ord, boolean unsigned) { + String indent = " ".repeat(ord == Order.DF? 16 : 20); String tailPart = unsigned? ") & 0x" + "FF".repeat(actualSize) : ""; - if (le) { - return (unsigned? "(" : "") + - "getByteAtOffset_BE(seg, roff) & 0xFF |\n" + - indent + - "(getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 |\n" + - indent + - "getByteAtOffset_BE(seg, roff + 2) << 16" + - tailPart; - } else { - return (unsigned? "(" : "") + - "getByteAtOffset_BE(seg, roff) << 16 |\n" + - indent + - "(getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 |\n" + - indent + - "getByteAtOffset_BE(seg, roff + 2) & 0xFF" + - tailPart; + switch (ord) { + case BE: return loadBE(unsigned, indent, tailPart); + case LE: return loadLE(unsigned, indent, tailPart); } + return "isBigEndian?\n" + + loadBE(unsigned, indent, tailPart) + " : \n" + + loadLE(unsigned, indent, tailPart); + } + + private String loadBE(boolean unsigned, String indent, String tailPart) { + return (unsigned? "(" : "") + + "getByteAtOffset_BE(seg, roff) << 16 |\n" + + indent + + "(getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 |\n" + + indent + + "getByteAtOffset_BE(seg, roff + 2) & 0xFF" + + tailPart; + } + + private String loadLE(boolean unsigned, String indent, String tailPart) { + return (unsigned? "(" : "") + + "getByteAtOffset_BE(seg, roff) & 0xFF |\n" + + indent + + "(getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 |\n" + + indent + + "getByteAtOffset_BE(seg, roff + 2) << 16" + + tailPart; } @Override - public String store(boolean le, boolean unsigned) { - String indent = " "; - if (le) { - return "setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF));\n" + - indent + - "setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF));\n" + - indent + - "setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF))"; - } else { - return "setByteAtOffset_BE(seg, woff, (byte) (value >> 16));\n" + - indent + - "setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF));\n" + - indent + - "setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF))"; + public String store(Order ord, boolean unsigned) { + String indent = " ".repeat(ord == Order.DF? 12 : 8); + switch (ord) { + case BE: return storeBE(indent); + case LE: return storeLE(indent); } + String indentOuter = " ".repeat(8); + return "if (isBigEndian) {\n" + + indent + + storeBE(indent) + + '\n' + + indentOuter + + "} else {\n" + + indent + + storeLE(indent) + + '\n' + + indentOuter + + '}'; + } + + private String storeBE(String indent) { + return "setByteAtOffset_BE(seg, woff, (byte) (value >> 16));\n" + + indent + + "setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF));\n" + + indent + + "setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF));"; + } + + private String storeLE(String indent) { + return "setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF));\n" + + indent + + "setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF));\n" + + indent + + "setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF));"; } }, INT("int", "Int", "Integer.BYTES", Integer.BYTES, true, "two's complement 32-bit", "long"), @@ -78,68 +131,90 @@ public final class Codegen { protected final String title; protected final String size; protected final int actualSize; - protected final boolean includeLE; + protected final boolean includeLEBE; protected final String extra; protected final String unsignedCarrier; - Type(String type, String title, String size, int actualSize, boolean includeLE, String extra, String unsignedCarrier) { + Type(String type, String title, String size, int actualSize, boolean includeLEBE, String extra, String unsignedCarrier) { this.type = type; this.title = title; this.size = size; this.actualSize = actualSize; - this.includeLE = includeLE; + this.includeLEBE = includeLEBE; this.extra = extra; this.unsignedCarrier = unsignedCarrier; } - public String title(boolean le, boolean unsigned) { - return (unsigned? "Unsigned" + title : title) + (le? "LE" : ""); + public String title(Order ord, boolean unsigned) { + return (unsigned? "Unsigned" + title : title) + ord.suffix(); } - public String extraRead(boolean le, boolean unsigned) { - return getExtra("read", le, unsigned); + public String extraRead(Order ord, boolean unsigned) { + return getExtra("read", ord, unsigned); } - public String extraWrite(boolean le, boolean unsigned) { - return getExtra("written", le, unsigned); + public String extraWrite(Order ord, boolean unsigned) { + return getExtra("written", ord, unsigned); } - private String getExtra(String op, boolean le, boolean unsigned) { + private String getExtra(String op, Order ord, boolean unsigned) { return "The value is " + op + " using " + (unsigned? "an unsigned " : "a ") + extra + - " encoding, with " + - (le? "little" : "big") + - "-endian byte order."; + " encoding,\n" + + " * with " + + ord.endianDesc() + + " byte order."; } public String type(boolean unsigned) { return unsigned? unsignedCarrier : type; } - public String load(boolean le, boolean unsigned) { + public String load(Order ord, boolean unsigned) { boolean longCarrier = "long".equals(unsignedCarrier); boolean intCarrier = "int".equals(unsignedCarrier); return (unsigned && !longCarrier && !intCarrier? '(' + unsignedCarrier + ") (" : "") + - "get" + - title + - "AtOffset_" + - (le? "LE" : "BE") + - "(seg, roff)" + + getCall(ord) + (unsigned? " & 0x" + "FF".repeat(actualSize) + (longCarrier? 'L' : intCarrier? "" : ')') : ""); } - public String store(boolean le, boolean unsigned) { + private String getCall(Order ord) { + if (ord == Order.DF) { + return "(isBigEndian? " + getCall(Order.BE) + " : " + getCall(Order.LE) + ')'; + } + return "get" + + title + + "AtOffset_" + + ord.suffix() + + "(seg, roff)"; + } + + public String store(Order ord, boolean unsigned) { + if (ord == Order.DF) { + String indent = " "; + return "if (isBigEndian) {\n" + + indent + " " + + store(Order.BE, unsigned) + + '\n' + + indent + + "} else {\n" + + indent + " " + + store(Order.LE, unsigned) + + '\n' + + indent + + '}'; + } boolean longCarrier = "long".equals(unsignedCarrier); return "set" + title + "AtOffset_" + - (le? "LE" : "BE") + + ord.suffix() + "(seg, woff, " + (unsigned? '(' + type + ") (value & 0x" + "FF".repeat(actualSize) + (longCarrier? "L)" : ")") : "value") + - ')'; + ");"; } public String realType(boolean unsigned) { @@ -150,7 +225,7 @@ public final class Codegen { enum Template { INTERFACE { @Override - public String relativeRead(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeRead(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " /**\n" + " * Get the %8$s value at the current {@link Buf#readerIndex()},\n" + @@ -161,11 +236,11 @@ public final class Codegen { " * @throws IndexOutOfBoundsException If {@link Buf#readableBytes} is less than %3$s.\n" + " */\n" + " %1$s read%2$s();"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetRead(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetRead(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " /**\n" + " * Get the %8$s value at the given reader offset.\n" + @@ -178,11 +253,11 @@ public final class Codegen { " * greater than or equal to {@link Buf#capacity()} minus %3$s.\n" + " */\n" + " %1$s read%2$s(int roff);"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String relativeWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeWrite(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " /**\n" + " * Set the given %8$s value at the current {@link Buf#writerIndex()},\n" + @@ -194,11 +269,11 @@ public final class Codegen { " * @throws IndexOutOfBoundsException If {@link Buf#writableBytes} is less than %3$s.\n" + " */\n" + " Buf write%2$s(%1$s value);"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetWrite(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " /**\n" + " * Set the given %8$s value at the given write offset. The {@link Buf#writerIndex()} is not modified.\n" + @@ -211,12 +286,12 @@ public final class Codegen { " * greater than or equal to {@link Buf#capacity()} minus %3$s.\n" + " */\n" + " Buf write%2$s(int woff, %1$s value);"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } }, IMPLEMENTATION { @Override - public String relativeRead(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeRead(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " @Override\n" + " public %1$s read%2$s() {\n" + @@ -225,43 +300,43 @@ public final class Codegen { " roff += %5$s;\n" + " return value;\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetRead(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetRead(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " @Override\n" + " public %1$s read%2$s(int roff) {\n" + " checkRead(roff, %5$s);\n" + " return %6$s;\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String relativeWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeWrite(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " @Override\n" + " public Buf write%2$s(%1$s value) {\n" + (type == Type.MED? " checkWrite(woff, %5$s);\n" : "") + - " %7$s;\n" + + " %7$s\n" + " woff += %5$s;\n" + " return this;\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetWrite(Type type, Order ord, boolean unsigned, boolean read) { var tmpl = '\n' + " @Override\n" + " public Buf write%2$s(int woff, %1$s value) {\n" + (type == Type.MED? " checkWrite(woff, %5$s);\n" : "") + - " %7$s;\n" + + " %7$s\n" + " return this;\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } }, TESTS { @@ -270,7 +345,7 @@ public final class Codegen { int bytesAvailAfter; @Override - public String relativeRead(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeRead(Type type, Order ord, boolean unsigned, boolean read) { prepare(type); var tmpl = '\n' + " @Test\n" + @@ -286,12 +361,13 @@ public final class Codegen { " }\n" + '\n' + " @Test\n" + - " public void relativeReadOf%2$sMustReadWith" + (le? "Little" : "Big") + "EndianByteOrder() {\n" + + " public void relativeReadOf%2$sMustReadWith" + ord.title() + "EndianByteOrder() {\n" + + (ord == Order.DF? " buf.order(ByteOrder.BIG_ENDIAN);\n" : "") + " assertEquals(0, buf.readableBytes());\n" + " assertEquals(Long.BYTES, buf.writableBytes());\n" + " %1$s value = " + testValue + ";\n" + " buf.write%2$s(value);\n" + - " buf.writeByte(" + (le? type.actualSize - 1 : 0) + ", (byte) 0x10);\n" + + " buf.writeByte(" + (ord == Order.LE? type.actualSize - 1 : 0) + ", (byte) 0x10);\n" + " assertEquals(" + type.actualSize + ", buf.readableBytes());\n" + " assertEquals(" + bytesAvailAfter + ", buf.writableBytes());\n" + " assertEquals(" + testValueByteOrder + ", buf.read%2$s());\n" + @@ -315,11 +391,11 @@ public final class Codegen { " }\n" + " assertEquals(" + (type.actualSize - 1) + ", buf.readableBytes());\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetRead(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetRead(Type type, Order ord, boolean unsigned, boolean read) { prepare(type); var tmpl = '\n' + " @Test\n" + @@ -340,10 +416,11 @@ public final class Codegen { " }\n" + '\n' + " @Test\n" + - " public void offsettedReadOf%2$sMustReadWith" + (le? "Little" : "Big") + "EndianByteOrder() {\n" + + " public void offsettedReadOf%2$sMustReadWith" + ord.title() + "EndianByteOrder() {\n" + + (ord == Order.DF? " buf.order(ByteOrder.BIG_ENDIAN);\n" : "") + " %1$s value = " + testValue + ";\n" + " buf.write%2$s(value);\n" + - " buf.writeByte(" + (le? type.actualSize - 1 : 0) + ", (byte) 0x10);\n" + + " buf.writeByte(" + (ord == Order.LE? type.actualSize - 1 : 0) + ", (byte) 0x10);\n" + " assertEquals(" + testValueByteOrder + ", buf.read%2$s(0));\n" + " }\n" + '\n' + @@ -368,13 +445,14 @@ public final class Codegen { " // Good.\n" + " }\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String relativeWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String relativeWrite(Type type, Order ord, boolean unsigned, boolean read) { prepare(type); int size = type.actualSize; + boolean le = ord == Order.LE; int r = le? size : 1; var tmpl = '\n' + " @Test\n" + @@ -394,7 +472,8 @@ public final class Codegen { " }\n" + '\n' + " @Test\n" + - " public void relativeWriteOf%2$sMustHave" + (le? "Little" : "Big") + "EndianByteOrder() {\n" + + " public void relativeWriteOf%2$sMustHave" + ord.title() + "EndianByteOrder() {\n" + + (ord == Order.DF? " buf.order(ByteOrder.BIG_ENDIAN);\n" : "") + " %1$s value = " + testValue + ";\n" + " buf.write%2$s(value);\n" + " buf.writerIndex(Long.BYTES);\n" + @@ -407,13 +486,14 @@ public final class Codegen { " assertEquals((byte) 0x0" + (size < 7? 0 : le? r-- : r++) + ", buf.readByte());\n" + " assertEquals((byte) 0x0" + (size < 8? 0 : r) + ", buf.readByte());\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } @Override - public String offsetWrite(Type type, boolean le, boolean unsigned, boolean read) { + public String offsetWrite(Type type, Order ord, boolean unsigned, boolean read) { prepare(type); int size = type.actualSize; + boolean le = ord == Order.LE; int r = le? size : 1; var tmpl = '\n' + " @Test\n" + @@ -447,7 +527,8 @@ public final class Codegen { " }\n" + '\n' + " @Test\n" + - " public void offsettedWriteOf%2$sMustHave" + (le? "Little" : "Big") + "EndianByteOrder() {\n" + + " public void offsettedWriteOf%2$sMustHave" + ord.title() + "EndianByteOrder() {\n" + + (ord == Order.DF? " buf.order(ByteOrder.BIG_ENDIAN);\n" : "") + " %1$s value = " + testValue + ";\n" + " buf.write%2$s(0, value);\n" + " buf.writerIndex(Long.BYTES);\n" + @@ -460,7 +541,7 @@ public final class Codegen { " assertEquals((byte) 0x0" + (size < 7? 0 : le? r-- : r++) + ", buf.readByte());\n" + " assertEquals((byte) 0x0" + (size < 8? 0 : r) + ", buf.readByte());\n" + " }"; - return format(tmpl, type, le, unsigned, read); + return format(tmpl, type, ord, unsigned, read); } private void prepare(Type type) { @@ -481,10 +562,10 @@ public final class Codegen { } }; - public abstract String relativeRead(Type type, boolean le, boolean unsigned, boolean read); - public abstract String offsetRead(Type type, boolean le, boolean unsigned, boolean read); - public abstract String relativeWrite(Type type, boolean le, boolean unsigned, boolean read); - public abstract String offsetWrite(Type type, boolean le, boolean unsigned, boolean read); + public abstract String relativeRead(Type type, Order ord, boolean unsigned, boolean read); + public abstract String offsetRead(Type type, Order ord, boolean unsigned, boolean read); + public abstract String relativeWrite(Type type, Order ord, boolean unsigned, boolean read); + public abstract String offsetWrite(Type type, Order ord, boolean unsigned, boolean read); } public static void main(String[] args) throws Exception { @@ -552,47 +633,55 @@ public final class Codegen { private static Stream generateAccessors(Template template, Type type) { Builder builder = Stream.builder(); - builder.add(template.relativeRead(type, false, false, true)); - builder.add(template.offsetRead(type, false, false, true)); - if (type.includeLE) { - builder.add(template.relativeRead(type, true, false, true)); - builder.add(template.offsetRead(type, true, false, true)); + builder.add(template.relativeRead(type, Order.DF, false, true)); + builder.add(template.offsetRead(type, Order.DF, false, true)); + if (type.includeLEBE) { + builder.add(template.relativeRead(type, Order.LE, false, true)); + builder.add(template.offsetRead(type, Order.LE, false, true)); + builder.add(template.relativeRead(type, Order.BE, false, true)); + builder.add(template.offsetRead(type, Order.BE, false, true)); } if (type.unsignedCarrier != null) { - builder.add(template.relativeRead(type, false, true, true)); - builder.add(template.offsetRead(type, false, true, true)); - if (type.includeLE) { - builder.add(template.relativeRead(type, true, true, true)); - builder.add(template.offsetRead(type, true, true, true)); + builder.add(template.relativeRead(type, Order.DF, true, true)); + builder.add(template.offsetRead(type, Order.DF, true, true)); + if (type.includeLEBE) { + builder.add(template.relativeRead(type, Order.LE, true, true)); + builder.add(template.offsetRead(type, Order.LE, true, true)); + builder.add(template.relativeRead(type, Order.BE, true, true)); + builder.add(template.offsetRead(type, Order.BE, true, true)); } } - builder.add(template.relativeWrite(type, false, false, false)); - builder.add(template.offsetWrite(type, false, false, false)); - if (type.includeLE) { - builder.add(template.relativeWrite(type, true, false, false)); - builder.add(template.offsetWrite(type, true, false, false)); + builder.add(template.relativeWrite(type, Order.DF, false, false)); + builder.add(template.offsetWrite(type, Order.DF, false, false)); + if (type.includeLEBE) { + builder.add(template.relativeWrite(type, Order.LE, false, false)); + builder.add(template.offsetWrite(type, Order.LE, false, false)); + builder.add(template.relativeWrite(type, Order.BE, false, false)); + builder.add(template.offsetWrite(type, Order.BE, false, false)); } if (type.unsignedCarrier != null) { - builder.add(template.relativeWrite(type, false, true, false)); - builder.add(template.offsetWrite(type, false, true, false)); - if (type.includeLE) { - builder.add(template.relativeWrite(type, true, true, false)); - builder.add(template.offsetWrite(type, true, true, false)); + builder.add(template.relativeWrite(type, Order.DF, true, false)); + builder.add(template.offsetWrite(type, Order.DF, true, false)); + if (type.includeLEBE) { + builder.add(template.relativeWrite(type, Order.LE, true, false)); + builder.add(template.offsetWrite(type, Order.LE, true, false)); + builder.add(template.relativeWrite(type, Order.BE, true, false)); + builder.add(template.offsetWrite(type, Order.BE, true, false)); } } return builder.build(); } - private static String format(String format, Type type, boolean le, boolean unsigned, boolean read) { + private static String format(String format, Type type, Order ord, boolean unsigned, boolean read) { var carrier = type.type(unsigned); - var title = type.title(le, unsigned); + var title = type.title(ord, unsigned); var size = ALL_DIGITS.matcher(type.size).matches()? type.size : "{@link " + type.size.replace('.', '#') + '}'; - var extra = read? type.extraRead(le, unsigned) : type.extraWrite(le, unsigned); + var extra = read? type.extraRead(ord, unsigned) : type.extraWrite(ord, unsigned); var realSize = type.size; return String.format(format, carrier, title, size, extra, realSize, - type.load(le, unsigned), type.store(le, unsigned), + type.load(ord, unsigned), type.store(ord, unsigned), type.realType(unsigned)); } }