Java Utililty Methods ByteBuffer Endian

List of utility methods to do ByteBuffer Endian

Description

The list of methods to do ByteBuffer Endian are organized into topic(s).

Method

voidassertByteOrderLittleEndian(ByteBuffer buffer)
assert Byte Order Little Endian
if (buffer.order() != ByteOrder.LITTLE_ENDIAN) {
    throw new IllegalArgumentException("ByteBuffer byte order must be little endian");
longconvertLittleEndianLong(ByteBuffer buff, int size)
convert Little Endian Long
if (size <= 0 || size > LONG_SIZE) {
    throw new IllegalArgumentException("size MUST be less 8.");
byte[] array = new byte[size];
buff.get(array);
ByteBuffer bbf = ByteBuffer.allocate(LONG_SIZE);
bbf.put(array);
bbf.order(ByteOrder.LITTLE_ENDIAN);
...
longdecodeLittleEndianFixedWidthLong(ByteBuffer buffer)
decode Little Endian Fixed Width Long
return decodeLittleEndianFixedWidthLong(buffer.array(), buffer.arrayOffset() + buffer.position(),
        buffer.remaining());
intdecodeLittleEndianVarInt(ByteBuffer buffer)
decode Little Endian Var Int
byte b = buffer.get();
int result = b & 0x7f;
if ((b & 0x80) > 0) {
    b = buffer.get();
    result |= ((b & 0x7f) << 7);
    if ((b & 0x80) > 0) {
        b = buffer.get();
        result |= ((b & 0x7f) << 14);
...
byte[]extractInt(ByteBuffer header_buf, boolean bigEndian)
extract Int
byte b1, b2, b3, b4;
b1 = header_buf.get();
b2 = header_buf.get();
b3 = header_buf.get();
b4 = header_buf.get();
byte[] result = new byte[4];
if (bigEndian) {
    result[0] = b1;
...
byte[]extractShort(ByteBuffer buffer, boolean bigEndian)
extract Short
byte b1, b2;
byte[] result = new byte[2];
b1 = buffer.get();
b2 = buffer.get();
if (bigEndian) {
    result[0] = b1;
    result[1] = b2;
} else {
...
ByteBufferinLittleEndianOrder(ByteBuffer buf)
in Little Endian Order
if (buf.order().equals(ByteOrder.LITTLE_ENDIAN)) {
    return buf;
} else {
    return buf.duplicate().order(ByteOrder.LITTLE_ENDIAN);
voidswapEndian16(ByteBuffer buf)
swap Endian
int elems = buf.remaining() / 2;
for (int n = 0, t = buf.position(); n < elems; n++, t += 2) {
    short s = buf.getShort(t);
    s = (short) ((s << 8) | (s >>> 8));
    buf.putShort(t, s);