{ // Merge of Native{Big,Little}Endian. Should also be merged up to Unsafe. private static class NativeEitherEndian extends NativeAccess { private final boolean LE = true; private int pickPos(int top, int pos) { return LE ? pos : top - pos; } long makeLong(byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { return ((toUnsignedLong(i0) << pickPos(56, 0)) | (toUnsignedLong(i1) << pickPos(56, 8)) | (toUnsignedLong(i2) << pickPos(56, 16)) | (toUnsignedLong(i3) << pickPos(56, 24)) | (toUnsignedLong(i4) << pickPos(56, 32)) | (toUnsignedLong(i5) << pickPos(56, 40)) | (toUnsignedLong(i6) << pickPos(56, 48)) | (toUnsignedLong(i7) << pickPos(56, 56))); } long makeLong(short i0, short i1, short i2, short i3) { return ((toUnsignedLong(i0) << pickPos(48, 0)) | (toUnsignedLong(i1) << pickPos(48, 16)) | (toUnsignedLong(i2) << pickPos(48, 32)) | (toUnsignedLong(i3) << pickPos(48, 48))); } long makeLong(int i0, int i1) { return (toUnsignedLong(i0) << pickPos(32, 0)) | (toUnsignedLong(i1) << pickPos(32, 32)); } int makeInt(short i0, short i1) { return (toUnsignedInt(i0) << pickPos(16, 0)) | (toUnsignedInt(i1) << pickPos(16, 16)); } int makeInt(byte i0, byte i1, byte i2, byte i3) { return ((toUnsignedInt(i0) << pickPos(24, 0)) | (toUnsignedInt(i1) << pickPos(24, 8)) | (toUnsignedInt(i2) << pickPos(24, 16)) | (toUnsignedInt(i3) << pickPos(24, 24))); } short makeShort(byte i0, byte i1) { return (short)((toUnsignedInt(i0) << pickPos(8, 0)) | (toUnsignedInt(i1) << pickPos(8, 8))); } private byte pick(byte le, byte be) { return LE ? le : be; } private short pick(short le, short be) { return LE ? le : be; } private int pick(int le, int be) { return LE ? le : be; } void putLongParts(Unsafe theUnsafe, Object o, long offset, byte i0, byte i1, byte i2, byte i3, byte i4, byte i5, byte i6, byte i7) { theUnsafe.putByte(o, offset + 0, pick(i0, i7)); theUnsafe.putByte(o, offset + 1, pick(i1, i6)); theUnsafe.putByte(o, offset + 2, pick(i2, i5)); theUnsafe.putByte(o, offset + 3, pick(i3, i4)); theUnsafe.putByte(o, offset + 4, pick(i4, i3)); theUnsafe.putByte(o, offset + 5, pick(i5, i2)); theUnsafe.putByte(o, offset + 6, pick(i6, i1)); theUnsafe.putByte(o, offset + 7, pick(i7, i0)); } void putLongParts(Unsafe theUnsafe, Object o, long offset, short i0, short i1, short i2, short i3) { theUnsafe.putShort(o, offset + 0, pick(i0, i3)); theUnsafe.putShort(o, offset + 2, pick(i1, i2)); theUnsafe.putShort(o, offset + 4, pick(i2, i1)); theUnsafe.putShort(o, offset + 6, pick(i3, i0)); } void putLongParts(Unsafe theUnsafe, Object o, long offset, int i0, int i1) { theUnsafe.putInt(o, offset + 0, pick(i0, i1)); theUnsafe.putInt(o, offset + 4, pick(i1, i0)); } void putIntParts(Unsafe theUnsafe, Object o, long offset, short i0, short i1) { theUnsafe.putShort(o, offset + 0, pick(i0, i1)); theUnsafe.putShort(o, offset + 2, pick(i1, i0)); } void putIntParts(Unsafe theUnsafe, Object o, long offset, byte i0, byte i1, byte i2, byte i3) { theUnsafe.putByte(o, offset + 0, pick(i0, i3)); theUnsafe.putByte(o, offset + 1, pick(i1, i2)); theUnsafe.putByte(o, offset + 2, pick(i2, i1)); theUnsafe.putByte(o, offset + 3, pick(i3, i0)); } void putShortParts(Unsafe theUnsafe, Object o, long offset, byte i0, byte i1) { theUnsafe.putByte(o, offset + 0, pick(i0, i1)); theUnsafe.putByte(o, offset + 1, pick(i1, i0)); } char fromEndian(boolean big, char n) { return big ? Character.reverseBytes(n) : n; } short fromEndian(boolean big, short n) { return big ? Short.reverseBytes(n) : n; } int fromEndian(boolean big, int n) { return big ? Integer.reverseBytes(n) : n; } long fromEndian(boolean big, long n) { return big ? Long.reverseBytes(n) : n; } } }