Example usage for com.google.common.primitives Ints fromBytes

List of usage examples for com.google.common.primitives Ints fromBytes

Introduction

In this page you can find the example usage for com.google.common.primitives Ints fromBytes.

Prototype

@GwtIncompatible("doesn't work")
public static int fromBytes(byte b1, byte b2, byte b3, byte b4) 

Source Link

Document

Returns the int value whose byte representation is the given 4 bytes, in big-endian order; equivalent to Ints.fromByteArray(new byte[] {b1, b2, b3, b4})}.

Usage

From source file:org.apache.drill.exec.store.pcap.PcapFormatUtils.java

/**
 *
 * @param byteOrder true for forward file order, false fore revers file order
 * @param buf byte buffer//from   www.  j a  va 2s  .c  o  m
 * @param offset buffer offset
 * @return integer value of specific bytes from buffer
 */
public static int getIntFileOrder(boolean byteOrder, final byte[] buf, final int offset) {
    if (byteOrder) {
        return Ints.fromBytes(buf[offset], buf[offset + 1], buf[offset + 2], buf[offset + 3]);
    } else {
        return Ints.fromBytes(buf[offset + 3], buf[offset + 2], buf[offset + 1], buf[offset]);
    }
}

From source file:io.grpc.services.InetAddressUtil.java

public static String toAddrString(InetAddress ip) {
    checkNotNull(ip);//  w  w w. j av a 2s .  c  o m
    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);
}

From source file:org.apache.hadoop.mapred.nativetask.util.BytesUtil.java

/**
 * Convert a big-endian integer from a byte array to a primitive value.
 * @param bytes the array to parse from//  w ww . j  a  va  2  s . c o m
 * @param offset the offset in the array
 */
public static int toInt(byte[] bytes, int offset) {
    return Ints.fromBytes(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3]);
}

From source file:io.prestosql.parquet.ParquetTimestampUtils.java

/**
 * 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
 */// w ww  .  ja v a  2  s  .  c om
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);
}

From source file:org.apache.drill.exec.store.pcap.PcapFormatUtils.java

public static int getInt(final byte[] buf, final int offset) {
    return Ints.fromBytes(buf[offset], buf[offset + 1], buf[offset + 2], buf[offset + 3]);
}

From source file:com.facebook.presto.hive.parquet.ParquetTimestampUtils.java

/**
 * 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
 *///ww w .  j  av  a2  s. c  om
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);
}

From source file:water.parser.parquet.ParquetInt96TimestampConverter.java

/**
 * 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
 *//*from  w ww . j a  va  2s .com*/
public static long getTimestampMillis(Binary timestampBinary) {
    if (timestampBinary.length() != BYTES_IN_INT96_TIMESTAMP) {
        throw new IllegalArgumentException(
                "Parquet timestamp must be 12 bytes long, actual " + timestampBinary.length());
    }
    byte[] bytes = timestampBinary.getBytes();

    // little endian encoding - bytes are red in inverted 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);
}

From source file:org.locationtech.geogig.storage.datastream.v2_3.DataStreamSerializationFactoryV2_3.java

@Override
public RevObject read(@Nullable ObjectId id, byte[] data, int offset, int length) throws IOException {
    final int type = data[offset] & 0xFF;
    if (RevObject.TYPE.TREE.ordinal() == type) {
        final int size = Ints.fromBytes(data[offset + 1], data[offset + 2], data[offset + 3], data[offset + 4]);
        offset += 1 + Integer.BYTES;// skip size header
        length -= 1 + Integer.BYTES;
        Preconditions.checkState(size == length, "expected %s, got %s", size, length);
        return FormatCommonV2_3.INSTANCE.readTree(id, data, offset, length);
    }// w w  w  .  jav a 2s .  c om
    return super.read(id, new ByteArrayInputStream(data, offset, length));
}

From source file:org.apache.hadoop.hdfs.server.namenode.XAttrFormat.java

/**
 * Unpack byte[] to XAttrs./*from w  w w  .j  a v a  2 s  . c  o m*/
 * 
 * @param attrs the packed bytes of XAttrs
 * @return XAttrs list
 */
static List<XAttr> toXAttrs(byte[] attrs) {
    List<XAttr> xAttrs = new ArrayList<>();
    if (attrs == null || attrs.length == 0) {
        return xAttrs;
    }
    for (int i = 0; i < attrs.length;) {
        XAttr.Builder builder = new XAttr.Builder();
        // big-endian
        int v = Ints.fromBytes(attrs[i], attrs[i + 1], attrs[i + 2], attrs[i + 3]);
        i += 4;
        int ns = (v >> XATTR_NAMESPACE_OFFSET) & XATTR_NAMESPACE_MASK;
        int nid = v & XATTR_NAME_MASK;
        builder.setNameSpace(XATTR_NAMESPACE_VALUES[ns]);
        builder.setName(XAttrStorage.getName(nid));
        int vlen = ((0xff & attrs[i]) << 8) | (0xff & attrs[i + 1]);
        i += 2;
        if (vlen > 0) {
            byte[] value = new byte[vlen];
            System.arraycopy(attrs, i, value, 0, vlen);
            builder.setValue(value);
            i += vlen;
        }
        xAttrs.add(builder.build());
    }
    return xAttrs;
}

From source file:com.complexible.common.protobuf.io.Protobuf.java

private static int readBigEndianInt32(final CodedInputStream theCodeStream) throws IOException {
    byte one = theCodeStream.readRawByte();
    byte two = theCodeStream.readRawByte();
    byte three = theCodeStream.readRawByte();
    byte four = theCodeStream.readRawByte();

    return Ints.fromBytes(one, two, three, four);
}