/** * Toggles the endianness of the specified 32-bit integer. */ public static int swapInt(int value) { return Integer.reverseBytes(value); }
@Override public final int getValueLength(Slice slice, int offset) { return Integer.reverseBytes(slice.getInt(offset)); }
@Override public final ByteBuf setInt(int index, int value) { wrapped.checkIndex(index, 4); _setInt(wrapped, index, nativeByteOrder ? value : Integer.reverseBytes(value)); return this; }
@Override public final int getInt(int index) { wrapped.checkIndex(index, 4); int v = _getInt(wrapped, index); return nativeByteOrder ? v : Integer.reverseBytes(v); }
@Override public final ByteBuf writeInt(int value) { wrapped.ensureWritable0(4); _setInt(wrapped, wrapped.writerIndex, nativeByteOrder ? value : Integer.reverseBytes(value)); wrapped.writerIndex += 4; return this; }
@Override public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length) { int intBits = slice.getInt(offset); // the file format uses big endian type.writeLong(builder, Integer.reverseBytes(intBits)); } }
public void testHashIntReverseBytesVsHashBytesIntsToByteArray() { int input = 42; assertEquals( Hashing.md5().hashBytes(Ints.toByteArray(input)), Hashing.md5().hashInt(Integer.reverseBytes(input))); }
static void setInt(long address, int value) { if (UNALIGNED) { PlatformDependent.putInt(address, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value)); } else { PlatformDependent.putByte(address, (byte) (value >>> 24)); PlatformDependent.putByte(address + 1, (byte) (value >>> 16)); PlatformDependent.putByte(address + 2, (byte) (value >>> 8)); PlatformDependent.putByte(address + 3, (byte) value); } }
static void setIntLE(long address, int value) { if (UNALIGNED) { PlatformDependent.putInt(address, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value); } else { PlatformDependent.putByte(address, (byte) value); PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); PlatformDependent.putByte(address + 3, (byte) (value >>> 24)); } }
static void setIntLE(byte[] array, int index, int value) { if (UNALIGNED) { PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value); } else { PlatformDependent.putByte(array, index, (byte) value); PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); PlatformDependent.putByte(array, index + 2, (byte) (value >>> 16)); PlatformDependent.putByte(array, index + 3, (byte) (value >>> 24)); } }
static int getInt(long address) { if (UNALIGNED) { int v = PlatformDependent.getInt(address); return BIG_ENDIAN_NATIVE_ORDER ? v : Integer.reverseBytes(v); } return PlatformDependent.getByte(address) << 24 | (PlatformDependent.getByte(address + 1) & 0xff) << 16 | (PlatformDependent.getByte(address + 2) & 0xff) << 8 | PlatformDependent.getByte(address + 3) & 0xff; }
static int getIntLE(long address) { if (UNALIGNED) { int v = PlatformDependent.getInt(address); return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v; } return PlatformDependent.getByte(address) & 0xff | (PlatformDependent.getByte(address + 1) & 0xff) << 8 | (PlatformDependent.getByte(address + 2) & 0xff) << 16 | PlatformDependent.getByte(address + 3) << 24; }
static int getInt(byte[] array, int index) { if (UNALIGNED) { int v = PlatformDependent.getInt(array, index); return BIG_ENDIAN_NATIVE_ORDER ? v : Integer.reverseBytes(v); } return PlatformDependent.getByte(array, index) << 24 | (PlatformDependent.getByte(array, index + 1) & 0xff) << 16 | (PlatformDependent.getByte(array, index + 2) & 0xff) << 8 | PlatformDependent.getByte(array, index + 3) & 0xff; }
static int getIntLE(byte[] array, int index) { if (UNALIGNED) { int v = PlatformDependent.getInt(array, index); return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v; } return PlatformDependent.getByte(array, index) & 0xff | (PlatformDependent.getByte(array, index + 1) & 0xff) << 8 | (PlatformDependent.getByte(array, index + 2) & 0xff) << 16 | PlatformDependent.getByte(array, index + 3) << 24; }
static void setInt(byte[] array, int index, int value) { if (UNALIGNED) { PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value)); } else { PlatformDependent.putByte(array, index, (byte) (value >>> 24)); PlatformDependent.putByte(array, index + 1, (byte) (value >>> 16)); PlatformDependent.putByte(array, index + 2, (byte) (value >>> 8)); PlatformDependent.putByte(array, index + 3, (byte) value); } }
@Description("encode value as a 32-bit 2's complement big endian varbinary") @ScalarFunction("to_big_endian_32") @SqlType(StandardTypes.VARBINARY) public static Slice toBigEndian32(@SqlType(StandardTypes.INTEGER) long value) { Slice slice = Slices.allocate(Integer.BYTES); slice.setInt(0, Integer.reverseBytes((int) value)); return slice; }
@Description("encode value as a big endian varbinary according to IEEE 754 single-precision floating-point format") @ScalarFunction("to_ieee754_32") @SqlType(StandardTypes.VARBINARY) public static Slice toIEEE754Binary32(@SqlType(StandardTypes.REAL) long value) { Slice slice = Slices.allocate(Float.BYTES); slice.setInt(0, Integer.reverseBytes((int) value)); return slice; }
@Description("decode the 32-bit big-endian binary in IEEE 754 single-precision floating-point format") @ScalarFunction("from_ieee754_32") @SqlType(StandardTypes.REAL) public static long fromIEEE754Binary32(@SqlType(StandardTypes.VARBINARY) Slice slice) { checkCondition(slice.length() == Integer.BYTES, INVALID_FUNCTION_ARGUMENT, "Input floating-point value must be exactly 4 bytes long"); return Integer.reverseBytes(slice.getInt(0)); }
@Description("decode bigint value from a 32-bit 2's complement big endian varbinary") @ScalarFunction("from_big_endian_32") @SqlType(StandardTypes.INTEGER) public static long fromBigEndian32(@SqlType(StandardTypes.VARBINARY) Slice slice) { if (slice.length() != Integer.BYTES) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "expected 4-byte input, but got instead: " + slice.length()); } return Integer.reverseBytes(slice.getInt(0)); }
@Description("compute SpookyHashV2 32-bit hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice spookyHashV2_32(@SqlType(StandardTypes.VARBINARY) Slice slice) { Slice hash = Slices.allocate(Integer.BYTES); hash.setInt(0, Integer.reverseBytes(SpookyHashV2.hash32(slice, 0, slice.length(), 0))); return hash; }