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

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

Introduction

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

Prototype

public static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) 

Source Link

Document

Returns the long value whose byte representation is the given 8 bytes, in big-endian order; equivalent to Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})}.

Usage

From source file:com.palantir.atlasdb.encoding.PtBytes.java

/**
 * Converts a byte array to a long value. Assumes there will be {@link #SIZEOF_LONG} bytes
 * available.//from   w  w w  .  j av a 2 s  . com
 */
public static long toLong(byte[] bytes, int offset) {
    return Longs.fromBytes(bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3],
            bytes[offset + 4], bytes[offset + 5], bytes[offset + 6], bytes[offset + 7]);
}

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
 *///from  w  w w . ja  va 2s.c  o m
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: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
 *///from w  ww  .  j  a v a  2  s.com
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
 *///  www .  java  2s  . c  om
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:com.palantir.atlasdb.keyvalue.rocksdb.impl.RocksComparator.java

@Override
public int compare(Slice a, Slice b) {
    byte[] adata = a.data();
    byte[] bdata = b.data();
    byte[] rowSizeBytes = new byte[2];
    rowSizeBytes[0] = adata[adata.length - 1];
    rowSizeBytes[1] = adata[adata.length - 2];
    int aRowSize = (int) EncodingUtils.decodeVarLong(rowSizeBytes);
    rowSizeBytes[0] = bdata[bdata.length - 1];
    rowSizeBytes[1] = bdata[bdata.length - 2];
    int bRowSize = (int) EncodingUtils.decodeVarLong(rowSizeBytes);

    int comp = UnsignedBytes.lexicographicalComparator().compare(Arrays.copyOf(adata, aRowSize),
            Arrays.copyOf(bdata, bRowSize));
    if (comp != 0) {
        return comp;
    }/*ww  w.j  a v a  2s .  com*/

    int aColEnd = adata.length - 8 - EncodingUtils.sizeOfVarLong(aRowSize);
    int bColEnd = bdata.length - 8 - EncodingUtils.sizeOfVarLong(bRowSize);
    comp = UnsignedBytes.lexicographicalComparator().compare(Arrays.copyOfRange(adata, aRowSize, aColEnd),
            Arrays.copyOfRange(bdata, bRowSize, bColEnd));
    if (comp != 0) {
        return comp;
    }

    long aTs = Longs.fromBytes(adata[aColEnd + 0], adata[aColEnd + 1], adata[aColEnd + 2], adata[aColEnd + 3],
            adata[aColEnd + 4], adata[aColEnd + 5], adata[aColEnd + 6], adata[aColEnd + 7]);
    long bTs = Longs.fromBytes(bdata[bColEnd + 0], bdata[bColEnd + 1], bdata[bColEnd + 2], bdata[bColEnd + 3],
            bdata[bColEnd + 4], bdata[bColEnd + 5], bdata[bColEnd + 6], bdata[bColEnd + 7]);

    // Note: Ordering is reversed, later timestamps come before eariler ones.
    return Long.compare(bTs, aTs);
}

From source file:com.palantir.atlasdb.keyvalue.rocksdb.impl.RocksOldComparator.java

@Override
public int compare(Slice a, Slice b) {
    byte[] adata = a.data();
    byte[] bdata = b.data();
    byte[] rowSizeBytes = new byte[2];
    rowSizeBytes[0] = adata[adata.length - 1];
    rowSizeBytes[1] = adata[adata.length - 2];
    int aRowSize = (int) EncodingUtils.decodeVarLong(rowSizeBytes);
    rowSizeBytes[0] = bdata[bdata.length - 1];
    rowSizeBytes[1] = bdata[bdata.length - 2];
    int bRowSize = (int) EncodingUtils.decodeVarLong(rowSizeBytes);

    int comp = UnsignedBytes.lexicographicalComparator().compare(Arrays.copyOf(adata, aRowSize),
            Arrays.copyOf(bdata, bRowSize));
    if (comp != 0) {
        return comp;
    }//  ww w  .j av  a2  s . c  o m

    int aColEnd = adata.length - 8 - EncodingUtils.sizeOfVarLong(aRowSize);
    int bColEnd = bdata.length - 8 - EncodingUtils.sizeOfVarLong(bRowSize);
    comp = UnsignedBytes.lexicographicalComparator().compare(Arrays.copyOfRange(adata, aRowSize, aColEnd),
            Arrays.copyOfRange(bdata, bRowSize, bColEnd));
    if (comp != 0) {
        return comp;
    }

    long aTs = Longs.fromBytes(adata[aColEnd + 0], adata[aColEnd + 1], adata[aColEnd + 2], adata[aColEnd + 3],
            adata[aColEnd + 4], adata[aColEnd + 5], adata[aColEnd + 6], adata[aColEnd + 7]);
    long bTs = Longs.fromBytes(bdata[bColEnd + 0], bdata[bColEnd + 1], bdata[bColEnd + 2], bdata[bColEnd + 3],
            bdata[bColEnd + 4], bdata[bColEnd + 5], bdata[bColEnd + 6], bdata[bColEnd + 7]);

    return Long.compare(aTs, bTs);
}

From source file:strat.mining.stratum.proxy.utils.mining.SHA256HashingUtils.java

/**
 * Compute the SHA256 midstate of the given 64 bytes of data and return the
 * 32 bytes midstate. This algorithm is the Java implementation of the
 * Python implementation got from the Slush0 stratum proxy.
 * //from   w  w  w . j av a  2 s  . c o  m
 * @param data
 * @return
 */
public static final byte[] midstateSHA256(byte[] data) {
    if (data.length != 64) {
        throw new IndexOutOfBoundsException("Data must be 64 bytes long");
    }

    LinkedList<Long> w = new LinkedList<>();
    for (int i = 0; i < 16; i++) {
        int dataIndex = i * 4;
        w.add(Longs.fromBytes((byte) 0, (byte) 0, (byte) 0, (byte) 0, data[dataIndex + 3], data[dataIndex + 2],
                data[dataIndex + 1], data[dataIndex]));
    }

    long a = A0;
    long b = B0;
    long c = C0;
    long d = D0;
    long e = E0;
    long f = F0;
    long g = G0;
    long h = H0;

    for (long k : K) {
        long s0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
        long s1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
        long ma = (a & b) ^ (a & c) ^ (b & c);
        long ch = (e & f) ^ ((~e) & g);

        h = addu32(h, w.get(0), k, ch, s1);
        d = addu32(d, h);
        h = addu32(h, ma, s0);

        long tempa = a;
        a = h;
        h = g;
        g = f;
        f = e;
        e = d;
        d = c;
        c = b;
        b = tempa;

        long w1 = w.get(1);
        long w14 = w.get(14);
        s0 = rotateRight(w1, 7) ^ rotateRight(w1, 18) ^ (w1 >> 3);
        s1 = rotateRight(w14, 17) ^ rotateRight(w14, 19) ^ (w14 >> 10);
        w.add(addu32(w.get(0), s0, w.get(9), s1));
        w.remove(0);
    }

    a = addu32(a, A0);
    b = addu32(b, B0);
    c = addu32(c, C0);
    d = addu32(d, D0);
    e = addu32(e, E0);
    f = addu32(f, F0);
    g = addu32(g, G0);
    h = addu32(h, H0);

    byte[] result = new byte[32];
    byte[] bytes = Longs.toByteArray(a);
    result[0] = bytes[7];
    result[1] = bytes[6];
    result[2] = bytes[5];
    result[3] = bytes[4];

    bytes = Longs.toByteArray(b);
    result[4] = bytes[7];
    result[5] = bytes[6];
    result[6] = bytes[5];
    result[7] = bytes[4];

    bytes = Longs.toByteArray(c);
    result[8] = bytes[7];
    result[9] = bytes[6];
    result[10] = bytes[5];
    result[11] = bytes[4];

    bytes = Longs.toByteArray(d);
    result[12] = bytes[7];
    result[13] = bytes[6];
    result[14] = bytes[5];
    result[15] = bytes[4];

    bytes = Longs.toByteArray(e);
    result[16] = bytes[7];
    result[17] = bytes[6];
    result[18] = bytes[5];
    result[19] = bytes[4];

    bytes = Longs.toByteArray(f);
    result[20] = bytes[7];
    result[21] = bytes[6];
    result[22] = bytes[5];
    result[23] = bytes[4];

    bytes = Longs.toByteArray(g);
    result[24] = bytes[7];
    result[25] = bytes[6];
    result[26] = bytes[5];
    result[27] = bytes[4];

    bytes = Longs.toByteArray(h);
    result[28] = bytes[7];
    result[29] = bytes[6];
    result[30] = bytes[5];
    result[31] = bytes[4];

    return result;
}

From source file:Model.Leader.java

/**
 * Listens for messages from group requesting an election.  The packets being
 * received will contain the amount of free memory in the sender.  The local 
 * client will compare against its own memory at startup and determine if it
 * should rebroadcast using <code> startElection</code>
 * claiming it has more free memory or accept the new leader
 * because they have more available resources.  
 * //from ww  w  . j  a  v  a  2 s .c  o m
 * This may result in a flood of broadcasts temporarily as the leader is 
 * sorted in theory.  E.g. if this is the lowest id peer, then all other 
 * peers might respond and end up with a Big O(n2) transmissions.  
 * 
 * Each peer with greater id will respond, until only one peer responds and 
 * other peers set it as the leader.
 */
public void receiveMessage() {
    byte[] buffer;
    long id;
    while (run) {
        try {
            buffer = new byte[256];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            while (serverSocket != null) {
                serverSocket.receive(packet);
                buffer = packet.getData();
                id = Longs.fromByteArray(buffer);
                if (id < ownID) {
                    startElection();
                } else {
                    localPeerNode.setLeader(packet.getAddress());
                    long timestamp = Longs.fromBytes(buffer[9], buffer[10], buffer[11], buffer[12], buffer[13],
                            buffer[14], buffer[15], buffer[16]);
                    System.out.println("received update for leader " + timestamp);
                    localPeerNode.updateVectorForPeer(packet.getAddress().getHostAddress(), timestamp);
                }
            }
            buffer = null;
        } catch (IOException ex) {
            Logger.getLogger(PeerNode.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        serverSocket.leaveGroup(group);
    } catch (IOException ex) {
        Logger.getLogger(Leader.class.getName()).log(Level.SEVERE, null, ex);
    }
    serverSocket.close();
}

From source file:com.palantir.atlasdb.keyvalue.rocksdb.impl.RocksDbKeyValueServices.java

static Pair<Cell, Long> parseCellAndTs(byte[] key) {
    byte[] rowSizeBytes = new byte[2];
    rowSizeBytes[0] = key[key.length - 1];
    rowSizeBytes[1] = key[key.length - 2];

    int rowSize = (int) EncodingUtils.decodeVarLong(rowSizeBytes);
    int colEnd = key.length - 8 - EncodingUtils.sizeOfVarLong(rowSize);

    byte[] rowName = Arrays.copyOf(key, rowSize);
    byte[] colName = Arrays.copyOfRange(key, rowSize, colEnd);
    long ts = Longs.fromBytes(key[colEnd + 0], key[colEnd + 1], key[colEnd + 2], key[colEnd + 3],
            key[colEnd + 4], key[colEnd + 5], key[colEnd + 6], key[colEnd + 7]);

    return Pair.create(Cell.create(rowName, colName), ts);
}

From source file:co.cask.cdap.common.app.RunIds.java

@VisibleForTesting
static UUID generateUUIDForTime(long timeInMillis) {
    // Use system time in milliseconds to generate time in 100ns.
    // Use COUNTER to ensure unique time gets generated for the same millisecond (up to HUNDRED_NANO_MULTIPLIER)
    // Hence the time is valid only for millisecond precision, event though it represents 100ns precision.
    long ts = timeInMillis * HUNDRED_NANO_MULTIPLIER + COUNTER.incrementAndGet() % HUNDRED_NANO_MULTIPLIER;
    long time = ts + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH;

    long timeLow = time & 0xffffffffL;
    long timeMid = time & 0xffff00000000L;
    long timeHi = time & 0xfff000000000000L;
    long upperLong = (timeLow << 32) | (timeMid >> 16) | (1 << 12) | (timeHi >> 48);

    // Random clock ID
    int clockId = RANDOM.nextInt() & 0x3FFF;
    long nodeId;/*from  w  w w. j  a v  a 2 s.com*/

    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        NetworkInterface networkInterface = null;
        while (interfaces.hasMoreElements()) {
            networkInterface = interfaces.nextElement();
            if (!networkInterface.isLoopback()) {
                break;
            }
        }
        byte[] mac = networkInterface == null ? null : networkInterface.getHardwareAddress();
        if (mac == null) {
            nodeId = (RANDOM.nextLong() & 0xFFFFFFL) | 0x100000L;
        } else {
            nodeId = Longs.fromBytes((byte) 0, (byte) 0, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        }

    } catch (SocketException e) {
        // Generate random node ID
        nodeId = RANDOM.nextLong() & 0xFFFFFFL | 0x100000L;
    }

    long lowerLong = ((long) clockId | 0x8000) << 48 | nodeId;

    return new java.util.UUID(upperLong, lowerLong);
}