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

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

Introduction

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

Prototype

public static byte[] toByteArray(long value) 

Source Link

Document

Returns a big-endian representation of value in an 8-element byte array; equivalent to ByteBuffer.allocate(8).putLong(value).array() .

Usage

From source file:net.bither.bitherj.utils.Utils.java

public static InetAddress parseAddressFromLong(long value) throws UnknownHostException {
    byte[] bytes;
    if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
        bytes = Ints.toByteArray((int) value);
    } else {//from ww  w.  j a  va2  s  .c  o  m
        bytes = Longs.toByteArray(value);
    }
    return InetAddress.getByAddress(bytes);
}

From source file:io.warp10.continuum.egress.EgressFetchHandler.java

private static void wrapperDump(HttpServletResponse resp, GTSDecoderIterator iter, boolean dedup,
        boolean signed, byte[] fetchPSK) throws IOException {

    if (!signed) {
        throw new IOException("Unsigned request.");
    }/*w  ww  . j av  a 2  s . co m*/

    // Labels for Sensision
    Map<String, String> labels = new HashMap<String, String>();

    StringBuilder sb = new StringBuilder();

    OutputStream out = resp.getOutputStream();

    while (iter.hasNext()) {
        GTSDecoder decoder = iter.next();

        if (dedup) {
            decoder = decoder.dedup();
        }

        if (!decoder.next()) {
            continue;
        }

        GTSEncoder encoder = decoder.getEncoder(true);

        //
        // Build a GTSWrapper
        //

        GTSWrapper wrapper = GTSWrapperHelper.fromGTSEncoderToGTSWrapper(encoder, true);

        //      GTSWrapper wrapper = new GTSWrapper();
        //      wrapper.setBase(encoder.getBaseTimestamp());
        //      wrapper.setMetadata(encoder.getMetadata());
        //      wrapper.setCount(encoder.getCount());
        //      wrapper.setEncoded(encoder.getBytes());

        //
        // Serialize the wrapper
        //

        TSerializer serializer = new TSerializer(new TCompactProtocol.Factory());
        byte[] data = null;

        try {
            data = serializer.serialize(wrapper);
        } catch (TException te) {
            throw new IOException(te);
        }

        //
        // Output is GTSWrapperId <WSP> HASH <WSP> GTSWrapper
        //

        out.write(Hex.encode(GTSWrapperHelper.getId(wrapper)));

        out.write(' ');

        if (null != fetchPSK) {
            //
            // Compute HMac for the wrapper
            //

            long hash = SipHashInline.hash24(fetchPSK, data);

            //
            // Output the MAC before the data, as hex digits
            //

            out.write(Hex.encode(Longs.toByteArray(hash)));
        } else {
            out.write('-');
        }

        out.write(' ');

        //
        // Base64 encode the wrapper
        //

        OrderPreservingBase64.encodeToStream(data, out);
        out.write('\r');
        out.write('\n');
        out.flush();

        //
        // Sensision metrics
        //

        labels.clear();
        labels.put(SensisionConstants.SENSISION_LABEL_APPLICATION,
                wrapper.getMetadata().getLabels().get(Constants.APPLICATION_LABEL));

        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS, Sensision.EMPTY_LABELS,
                1);
        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS_PERAPP, labels, 1);

        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS_SIZE,
                Sensision.EMPTY_LABELS, data.length);
        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS_SIZE_PERAPP, labels,
                data.length);

        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS_DATAPOINTS,
                Sensision.EMPTY_LABELS, wrapper.getCount());
        Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_SFETCH_WRAPPERS_DATAPOINTS_PERAPP, labels,
                wrapper.getCount());

    }
}

From source file:com.indeed.imhotep.web.QueryServlet.java

/**
 * Produces a Base64 encoded SHA-1 hash of the query and the list of shard names/versions which has to be sorted.
 *///from w ww  . ja  va  2  s .  co  m
private String getQueryHash(String query, Collection<ShardIdWithVersion> shards, boolean csv) {
    final MessageDigest sha1;
    try {
        sha1 = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        log.error("Failed to init SHA1", e);
        throw Throwables.propagate(e);
    }
    final String standardizedQuery = query.trim().replace('"', '\'').replaceAll("\\s+", " ");
    sha1.update(standardizedQuery.getBytes(UTF8_CHARSET));
    if (shards != null) {
        for (ShardIdWithVersion shard : shards) {
            sha1.update(shard.getShardId().getBytes(UTF8_CHARSET));
            sha1.update(Longs.toByteArray(shard.getVersion()));
            sha1.update(csv ? (byte) 1 : 0);
        }
    }
    sha1.update(VERSION_FOR_HASHING);
    return Base64.encodeBase64URLSafeString(sha1.digest());
}