@Override public long getLongLittleEndian(byte[] source, int offset) { return Longs.fromBytes( source[offset + 7], source[offset + 6], source[offset + 5], source[offset + 4], source[offset + 3], source[offset + 2], source[offset + 1], source[offset]); }
private /* static */ long upperEight(byte[] bytes) { return Longs.fromBytes( bytes[15], bytes[14], bytes[13], bytes[12], bytes[11], bytes[10], bytes[9], bytes[8]); } };
private /* static */ long lowerEight(byte[] bytes) { return Longs.fromBytes( bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); }
private /* static */ long upperEight(byte[] bytes) { return Longs.fromBytes( bytes[15], bytes[14], bytes[13], bytes[12], bytes[11], bytes[10], bytes[9], bytes[8]); } };
@Override public long getLongLittleEndian(byte[] source, int offset) { return Longs.fromBytes( source[offset + 7], source[offset + 6], source[offset + 5], source[offset + 4], source[offset + 3], source[offset + 2], source[offset + 1], source[offset]); }
private /* static */ long lowerEight(byte[] bytes) { return Longs.fromBytes( bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); }
/** * @param bytes an array of bytes * @param start the place in the array to read the long from * @return the long */ public static long readLong(byte[] bytes, int start) { return Longs.fromBytes(bytes[start], bytes[start + 1], bytes[start + 2], bytes[start + 3], bytes[start + 4], bytes[start + 5], bytes[start + 6], bytes[start + 7]); } }
/** * Returns the {@code long} value whose big-endian representation is stored in the first 8 bytes * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getLong()}. For example, the * input byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}} would yield the * {@code long} value {@code 0x1213141516171819L}. * * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more * flexibility at little cost in readability. * * @throws IllegalArgumentException if {@code bytes} has fewer than 8 elements */ public static long fromByteArray(byte[] bytes) { checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); return fromBytes( bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]); }
private /* static */ long upperEight(byte[] bytes) { return Longs.fromBytes( bytes[15], bytes[14], bytes[13], bytes[12], bytes[11], bytes[10], bytes[9], bytes[8]); } };
@Override public long getLongLittleEndian(byte[] source, int offset) { return Longs.fromBytes( source[offset + 7], source[offset + 6], source[offset + 5], source[offset + 4], source[offset + 3], source[offset + 2], source[offset + 1], source[offset]); }
private /* static */ long lowerEight(byte[] bytes) { return Longs.fromBytes( bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); }
/** * Returns the {@code long} value whose big-endian representation is stored in the first 8 bytes * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getLong()}. For example, the * input byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}} would yield the * {@code long} value {@code 0x1213141516171819L}. * * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more * flexibility at little cost in readability. * * @throws IllegalArgumentException if {@code bytes} has fewer than 8 elements */ public static long fromByteArray(byte[] bytes) { checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); return fromBytes( bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]); }
/** * Returns the {@code long} value whose big-endian representation is stored in the first 8 bytes * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getLong()}. For example, the * input byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}} would yield the * {@code long} value {@code 0x1213141516171819L}. * * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more * flexibility at little cost in readability. * * @throws IllegalArgumentException if {@code bytes} has fewer than 8 elements */ public static long fromByteArray(byte[] bytes) { checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); return fromBytes( bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]); }
/** * convert deprecated parquet int96 nanosecond timestamp to a long, based on * https://github.com/prestodb/presto/blob/master/presto-hive/src/main/java/com/facebook/presto/hive/parquet/ParquetTimestampUtils.java#L56 */ private static long convertInt96BinaryToTimestamp(Binary value) { // based on prestodb parquet int96 timestamp conversion byte[] bytes = value.getBytes(); // little endian encoding - need to invert byte order long timeOfDayNanos = Longs.fromBytes(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); int julianDay = Ints.fromBytes(bytes[11], bytes[10], bytes[9], bytes[8]); long ts = ((julianDay - JULIAN_EPOCH_OFFSET_DAYS) * MILLIS_IN_DAY) + (timeOfDayNanos / NANOS_PER_MILLISECOND); return ts; }
public void testFromBytes() { assertEquals( 0x1213141516171819L, Longs.fromBytes( (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19)); assertEquals( 0xFFEEDDCCBBAA9988L, Longs.fromBytes( (byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88)); }
/** * Returns GMT timestamp from binary encoded parquet timestamp (12 bytes - julian date + time of day nanos). * * @param timestampBinary INT96 parquet timestamp * @return timestamp in millis, GMT timezone */ public static long getTimestampMillis(Binary timestampBinary) { if (timestampBinary.length() != 12) { throw new PrestoException(HIVE_BAD_DATA, "Parquet timestamp must be 12 bytes, actual " + timestampBinary.length()); } byte[] bytes = timestampBinary.getBytes(); // little endian encoding - need to invert byte order long timeOfDayNanos = Longs.fromBytes(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); int julianDay = Ints.fromBytes(bytes[11], bytes[10], bytes[9], bytes[8]); return julianDayToMillis(julianDay) + (timeOfDayNanos / NANOS_PER_MILLISECOND); }
/** * Returns GMT timestamp from binary encoded parquet timestamp (12 bytes - julian date + time of day nanos). * * @param timestampBinary INT96 parquet timestamp * @return timestamp in millis, GMT timezone */ public static long getTimestampMillis(Binary timestampBinary) { if (timestampBinary.length() != 12) { throw new PrestoException(NOT_SUPPORTED, "Parquet timestamp must be 12 bytes, actual " + timestampBinary.length()); } byte[] bytes = timestampBinary.getBytes(); // little endian encoding - need to invert byte order long timeOfDayNanos = Longs.fromBytes(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); int julianDay = Ints.fromBytes(bytes[11], bytes[10], bytes[9], bytes[8]); return julianDayToMillis(julianDay) + (timeOfDayNanos / NANOS_PER_MILLISECOND); }
/** * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using * little-endian byte order. * * @return the next eight bytes of the input stream, interpreted as a {@code long} in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public long readLong() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); byte b3 = readAndCheckByte(); byte b4 = readAndCheckByte(); byte b5 = readAndCheckByte(); byte b6 = readAndCheckByte(); byte b7 = readAndCheckByte(); byte b8 = readAndCheckByte(); return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1); }
/** * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using * little-endian byte order. * * @return the next eight bytes of the input stream, interpreted as a {@code long} in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public long readLong() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); byte b3 = readAndCheckByte(); byte b4 = readAndCheckByte(); byte b5 = readAndCheckByte(); byte b6 = readAndCheckByte(); byte b7 = readAndCheckByte(); byte b8 = readAndCheckByte(); return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1); }
/** * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using * little-endian byte order. * * @return the next eight bytes of the input stream, interpreted as a {@code long} in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public long readLong() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); byte b3 = readAndCheckByte(); byte b4 = readAndCheckByte(); byte b5 = readAndCheckByte(); byte b6 = readAndCheckByte(); byte b7 = readAndCheckByte(); byte b8 = readAndCheckByte(); return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1); }