private static int getIntLittleEndian(byte[] input, int offset) { return Ints.fromBytes(input[offset + 3], input[offset + 2], input[offset + 1], input[offset]); }
/** * Returns the {@code int} value whose big-endian representation is stored in the first 4 bytes of * {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getInt()}. For example, the input * byte array {@code {0x12, 0x13, 0x14, 0x15, 0x33}} would yield the {@code int} value {@code * 0x12131415}. * * <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 4 elements */ public static int 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]); }
private static int getIntLittleEndian(byte[] input, int offset) { return Ints.fromBytes(input[offset + 3], input[offset + 2], input[offset + 1], input[offset]); }
public static int pack(short yr, byte m, byte d) { byte byte1 = (byte) ((yr >> 8) & 0xff); byte byte2 = (byte) yr; return Ints.fromBytes( byte1, byte2, m, d); }
public static int pack(int yr, int m, int d) { byte byte1 = (byte) ((yr >> 8) & 0xff); byte byte2 = (byte) yr; return Ints.fromBytes( byte1, byte2, (byte) m, (byte) d); }
private static int create(byte hour, byte minute, char millis) { byte m1 = (byte) (millis >> 8); byte m2 = (byte) millis; return Ints.fromBytes( hour, minute, m1, m2); }
private static int getIntLittleEndian(byte[] input, int offset) { return Ints.fromBytes(input[offset + 3], input[offset + 2], input[offset + 1], input[offset]); }
/** * Returns the {@code int} value whose big-endian representation is stored in the first 4 bytes of * {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getInt()}. For example, the input * byte array {@code {0x12, 0x13, 0x14, 0x15, 0x33}} would yield the {@code int} value {@code * 0x12131415}. * * <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 4 elements */ public static int 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]); }
/** * Returns the string representation of an {@link InetAddress}. * * <p>For IPv4 addresses, this is identical to {@link InetAddress#getHostAddress()}, but for IPv6 * addresses, the output follows <a href="http://tools.ietf.org/html/rfc5952">RFC 5952</a> section * 4. The main difference is that this method uses "::" for zero compression, while Java's version * uses the uncompressed form. * * <p>This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses * such as "::c000:201". The output does not include a Scope ID. * * @param ip {@link InetAddress} to be converted to an address string * @return {@code String} containing the text-formatted IP address * @since 10.0 */ public static String toAddrString(InetAddress ip) { checkNotNull(ip); if (ip instanceof Inet4Address) { // For IPv4, Java's formatting is good enough. return ip.getHostAddress(); } checkArgument(ip instanceof Inet6Address); byte[] bytes = ip.getAddress(); int[] hextets = new int[IPV6_PART_COUNT]; for (int i = 0; i < hextets.length; i++) { hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]); } compressLongestRunOfZeroes(hextets); return hextetsToIPv6String(hextets); }
public static long pack(short yr, byte m, byte d, byte hr, byte min, byte s, byte n) { int date = PackedLocalDate.pack(yr, m, d); int time = Ints.fromBytes(hr, min, s, n); return (((long) date) << 32) | (time & 0xffffffffL); }
/** * Reads an unsigned {@code short} as specified by {@link DataInputStream#readUnsignedShort()}, * except using little-endian byte order. * * @return the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public int readUnsignedShort() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); return Ints.fromBytes((byte) 0, (byte) 0, b2, b1); }
/** * 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; }
/** * Reads an unsigned {@code short} as specified by {@link DataInputStream#readUnsignedShort()}, * except using little-endian byte order. * * @return the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public int readUnsignedShort() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); return Ints.fromBytes((byte) 0, (byte) 0, b2, b1); }
/** * Reads an integer as specified by {@link DataInputStream#readInt()}, except using little-endian * byte order. * * @return the next four bytes of the input stream, interpreted as an {@code int} in little-endian * byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public int readInt() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); byte b3 = readAndCheckByte(); byte b4 = readAndCheckByte(); return Ints.fromBytes(b4, b3, b2, b1); }
/** * Reads an unsigned {@code short} as specified by {@link DataInputStream#readUnsignedShort()}, * except using little-endian byte order. * * @return the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in * little-endian byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public int readUnsignedShort() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); return Ints.fromBytes((byte) 0, (byte) 0, b2, b1); }
/** * Reads an integer as specified by {@link DataInputStream#readInt()}, except using little-endian * byte order. * * @return the next four bytes of the input stream, interpreted as an {@code int} in little-endian * byte order * @throws IOException if an I/O error occurs */ @CanIgnoreReturnValue // to skip some bytes @Override public int readInt() throws IOException { byte b1 = readAndCheckByte(); byte b2 = readAndCheckByte(); byte b3 = readAndCheckByte(); byte b4 = readAndCheckByte(); return Ints.fromBytes(b4, b3, b2, b1); }
/** * 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); }
@Test public void testWriteOutBytes() throws IOException { WriteOutBytes writeOutBytes = segmentWriteOutMedium.makeWriteOutBytes(); writeOutBytes.write('1'); verifyContents(writeOutBytes, "1"); writeOutBytes.writeInt(Ints.fromBytes((byte) '2', (byte) '3', (byte) '4', (byte) '5')); verifyContents(writeOutBytes, "12345"); writeOutBytes.write(new byte[] {'a'}); verifyContents(writeOutBytes, "12345a"); writeOutBytes.write(new byte[] {'a', 'b', 'c'}, 1, 1); verifyContents(writeOutBytes, "12345ab"); ByteBuffer bb = ByteBuffer.wrap(new byte[]{'a', 'b', 'c'}); bb.position(2); writeOutBytes.write(bb); Assert.assertEquals(3, bb.position()); verifyContents(writeOutBytes, "12345abc"); }