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

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

Introduction

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

Prototype

public static long fromByteArray(byte[] bytes) 

Source Link

Document

Returns the long value whose big-endian representation is stored in the first 8 bytes of bytes ; equivalent to ByteBuffer.wrap(bytes).getLong() .

Usage

From source file:org.kmworks.util.misc.UUIDGenerator.java

public static UUID generate() {

    Calendar cal = Calendar.getInstance();
    int yy = cal.get(Calendar.YEAR) - 2000; // <= 99  < 128
    int ddd = cal.get(Calendar.DAY_OF_YEAR); // <= 366 < 512

    long msbLong = (yy << 9) | ddd;
    msbLong = msbLong << 48;/*from w w  w . ja  v a2  s .c om*/
    byte hiBytes[] = new byte[6];
    ThreadLocalRandom.current().nextBytes(hiBytes);
    byte loBytes[] = new byte[8];
    ThreadLocalRandom.current().nextBytes(loBytes);
    msbLong |= from6Bytes(hiBytes);

    long lsbLong = Longs.fromByteArray(loBytes);
    UUID uuid = new UUID(msbLong, lsbLong);

    return uuid;
}

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

/**
 * Converts a byte array to a long value. Reverses {@link #toBytes(long)}
 */// ww  w. j a v a2s. c o m
public static long toLong(byte[] bytes) {
    return Longs.fromByteArray(bytes);
}

From source file:co.paralleluniverse.galaxy.server.MainMemoryEntry.java

public MainMemoryEntry(byte[] buffer) {
    this.version = Longs.fromByteArray(buffer);
    this.data = new byte[buffer.length - (Longs.BYTES)];
    System.arraycopy(buffer, Longs.BYTES, data, 0, data.length);
}

From source file:com.yandex.yoctodb.util.UnsignedByteArrays.java

public static long toLong(@NotNull final UnsignedByteArray bytes) {
    if (bytes.length() != Longs.BYTES)
        throw new IllegalArgumentException("Wrong length");

    return Longs.fromByteArray(bytes.data) ^ Long.MIN_VALUE;
}

From source file:com.google.template.soy.msgs.SoyMsgIdConverter.java

/**
 * Converts received message ID strings, as rendered by the Soy <code>msgId</code> function, into
 * a list of long message IDs.//from  w ww.j av  a 2s.  c  o m
 *
 * <p>The message IDs are generated by Soy as Base64 web safe ("base64url")-encoded int64s.
 *
 * <p>For example, "URTo5tnzILU=" = [0x51 0x14 0xe8 0xe6 0xd9 0xf3 0x20 0xb5] = 0x5114e8e6d9f320b5
 * = 5842550694803087541.
 *
 * @throws IllegalArgumentException if any of the strings fails to decode into message IDs, is too
 *     long or too short.
 */
public static ImmutableList<Long> convertSoyMessageIdStrings(Iterable<String> encodedIds) {
    ImmutableList.Builder<Long> messageIds = ImmutableList.builder();
    for (String encodedId : encodedIds) {
        byte[] decodedBytes = BaseEncoding.base64Url().decode(encodedId);
        if (decodedBytes.length != Long.BYTES) {
            throw new IllegalArgumentException(
                    String.format("The message ID to decode ('%s') was of invalid size (%d != %d)", encodedId,
                            decodedBytes.length, Long.BYTES));
        }
        messageIds.add(Longs.fromByteArray(decodedBytes));
    }
    return messageIds.build();
}

From source file:org.apache.hadoop.mapred.nativetask.testutil.BytesFactory.java

public static void updateObject(Writable obj, byte[] seed) {
    if (obj instanceof IntWritable) {
        ((IntWritable) obj).set(Ints.fromByteArray(seed));
    } else if (obj instanceof FloatWritable) {
        ((FloatWritable) obj).set(r.nextFloat());
    } else if (obj instanceof DoubleWritable) {
        ((DoubleWritable) obj).set(r.nextDouble());
    } else if (obj instanceof LongWritable) {
        ((LongWritable) obj).set(Longs.fromByteArray(seed));
    } else if (obj instanceof VIntWritable) {
        ((VIntWritable) obj).set(Ints.fromByteArray(seed));
    } else if (obj instanceof VLongWritable) {
        ((VLongWritable) obj).set(Longs.fromByteArray(seed));
    } else if (obj instanceof BooleanWritable) {
        ((BooleanWritable) obj).set(seed[0] % 2 == 1 ? true : false);
    } else if (obj instanceof Text) {
        ((Text) obj).set(BytesUtil.toStringBinary(seed));
    } else if (obj instanceof ByteWritable) {
        ((ByteWritable) obj).set(seed.length > 0 ? seed[0] : 0);
    } else if (obj instanceof BytesWritable) {
        ((BytesWritable) obj).set(seed, 0, seed.length);
    } else if (obj instanceof UTF8) {
        ((UTF8) obj).set(BytesUtil.toStringBinary(seed));
    } else if (obj instanceof MockValueClass) {
        ((MockValueClass) obj).set(seed);
    } else {/*from  w w  w  .  ja va2s  .c o  m*/
        throw new IllegalArgumentException("unknown writable: " + obj.getClass().getName());
    }
}

From source file:org.onosproject.ipfix.packet.SamplingDataRecord.java

public static SamplingDataRecord parse(byte[] data) throws HeaderException {
    try {//w  w  w.  j a v  a2 s . com
        if (data.length < HEADER_LENGTH) {
            throw new HeaderException("Data array too short.");
        }
        SamplingDataRecord sdr = new SamplingDataRecord();
        // observationDomainId
        byte[] observationDomainId = new byte[4];
        System.arraycopy(data, 0, observationDomainId, 0, 4);
        sdr.setObservationDomainId(Longs.fromByteArray(observationDomainId));
        // selectorAlgorithm
        byte[] selectorAlgorithm = new byte[2];
        System.arraycopy(data, 4, selectorAlgorithm, 0, 2);
        sdr.setSelectorAlgorithm(Ints.fromByteArray(selectorAlgorithm));
        // samplingPacketInterval
        byte[] samplingPacketInterval = new byte[4];
        System.arraycopy(data, 6, samplingPacketInterval, 0, 4);
        sdr.setSamplingPacketInterval(Longs.fromByteArray(samplingPacketInterval));
        // samplingPacketSpace
        byte[] samplingPacketSpace = new byte[4];
        System.arraycopy(data, 10, samplingPacketSpace, 0, 4);
        sdr.setSamplingPacketSpace(Longs.fromByteArray(samplingPacketSpace));
        return sdr;
    } catch (Exception e) {
        throw new HeaderException("Parse error: " + e.getMessage());
    }
}

From source file:net.bither.adapter.TransactionListAdapter.java

@Override
public long getItemId(int position) {
    return Longs.fromByteArray(transactions.get(position).getTxHash());
}

From source file:io.warp10.script.functions.HHCODETO.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object hhcode = stack.pop();/*from  w  ww. j  a v a  2 s . co m*/

    double[] latlon = null;

    if (hhcode instanceof Long) {
        latlon = GeoXPLib.fromGeoXPPoint((long) hhcode);
    } else if (hhcode instanceof String) {
        long hh = new BigInteger(hhcode.toString(), 16).longValue();
        latlon = GeoXPLib.fromGeoXPPoint(hh);
    } else if (hhcode instanceof byte[]) {
        long hh = Longs.fromByteArray((byte[]) hhcode);
        latlon = GeoXPLib.fromGeoXPPoint(hh);
    } else {
        throw new WarpScriptException(getName() + " expects a long, a string or a byte array.");
    }

    stack.push(latlon[0]);
    stack.push(latlon[1]);

    return stack;
}

From source file:org.apache.flume.event.EventHelper.java

public static String dumpEvent(Event event, int maxBytes) {
    StringBuilder buffer = new StringBuilder();
    Map<String, String> headers = event.getHeaders();

    if (event.getBody() == null) {
        buffer.append("null");
    } else if (event.getBody().length == 0) {
        // do nothing... in this case, HexDump.dump() will throw an exception
    } else {//from   www. j  a v a2  s .  co m
        byte[] body = event.getBody();

        String bodyType = null;
        String bodyCharset = null;
        if (headers != null) {
            bodyType = headers.get(Event.bodyType);
            bodyCharset = headers.get(Event.bodyCharset);
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                MDC.put(entry.getKey(), entry.getValue());
            }
        }

        if (bodyCharset == null)
            bodyCharset = "UTF-8";
        if ("long".equals(bodyType)) {
            return "" + Longs.fromByteArray(body);
        }
        if ("string".equals(bodyType)) {
            return fromBytes(body, bodyCharset);
        }

        byte[] data = Arrays.copyOf(body, Math.min(body.length, maxBytes));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            HexDump.dump(data, 0, out, 0);
            String hexDump = new String(out.toByteArray());
            // remove offset since it's not relevant for such a small dataset
            if (hexDump.startsWith(HEXDUMP_OFFSET)) {
                hexDump = hexDump.substring(HEXDUMP_OFFSET.length());
            }
            buffer.append(hexDump);
        } catch (Exception e) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Exception while dumping event", e);
            }
            buffer.append("...Exception while dumping: ").append(e.getMessage());
        }
        String result = buffer.toString();
        if (result.endsWith(EOL) && buffer.length() > EOL.length()) {
            buffer.delete(buffer.length() - EOL.length(), buffer.length()).toString();
        }
    }
    return "{ headers:" + headers + " body:" + buffer + " }";
}