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:org.roqmessaging.core.PublisherClient.java

/**
 * @return the encoded time stamp//from w ww  .  j a v  a2s .  c om
 */
private byte[] getTimestamp() {
    //      return (Long.toString(System.currentTimeMillis()) + " ").getBytes();
    return Longs.toByteArray(System.currentTimeMillis());
}

From source file:org.apache.druid.common.utils.SerializerUtils.java

private void writeLong(OutputStream out, long longValue) throws IOException {
    out.write(Longs.toByteArray(longValue));
}

From source file:cn.codepub.redis.directory.io.JedisStream.java

@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField,
        List<byte[]> values, long fileLength) {
    Jedis jedis = openJedis();/*from   ww  w  .  j  a  v  a2  s .  c  o m*/
    Pipeline pipelined = jedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    jedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}

From source file:com.subitarius.domain.Team.java

@Transient
@Override//from w w w  . j a  v  a 2s  .  c om
public byte[] getBytes() {
    byte[] idBytes = getIdBytes();
    byte[] nameBytes = name.getBytes(Charsets.UTF_8);
    byte[] expiryBytes = Longs.toByteArray(expiry.getTime());
    byte[] licenseCapBytes = Ints.toByteArray(licenseCap);
    return DistributedEntity.merge(idBytes, nameBytes, expiryBytes, licenseCapBytes);
}

From source file:pl.coffeepower.blog.messagebus.MessageBusTestHelper.java

Publisher executePublisher(final Engine engine) throws InterruptedException {
    Publisher publisher = Guice.createInjector(Stage.PRODUCTION, new TestConfigurationModule(),
            new BytesEventModule(), engine.getModule()).getInstance(Publisher.class);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.execute(() -> {//from  www  . j  a v a 2s  .  com
        Fixtures fixtures = new Fixtures();
        Stopwatch stopwatch = Stopwatch.createStarted();
        LongStream.rangeClosed(fixtures.getFirstMessageId(), fixtures.getNumberOfMessages()).forEach(value -> {
            IdleStrategy idleStrategy = new SleepingIdleStrategy(TimeUnit.MICROSECONDS.toNanos(1L));
            while (!publisher.send(Bytes.concat(Longs.toByteArray(value), fixtures.getAdditionalData()))) {
                idleStrategy.idle();
            }
        });
        System.out.println("Sent all messages in " + stopwatch.stop());
    });
    executorService.shutdown();
    executorService.awaitTermination(1L, TimeUnit.MINUTES);
    return publisher;
}

From source file:org.onosproject.ipfix.FlowRemovedListener.java

/**
 * Handle ONOS Reactive forwarding application flow removal.
 * When flow is removed, generate and send IPFIX record.
 *
 * @param entry flow entry removed from ONOS
 *//* w w w .jav a2  s.co m*/
private void flowRemovedRfwd(FlowEntry entry) {

    //Log
    ipfixManager.log.trace("Flow Removed from Reactive Forwarding, id={}, device={}, selector={}, treatment={}",
            entry.id(), entry.deviceId(), entry.selector(), entry.treatment());

    // Exporters
    IpAddress exporterIpv4 = IpAddress.valueOf(
            ipfixManager.deviceService.getDevice(entry.deviceId()).annotations().toString().split("=")[2]
                    .split(":")[0]);

    long dpid = Dpid.dpid(entry.deviceId().uri()).value();
    byte[] byteExporterIpv6 = new byte[16];
    System.arraycopy(Longs.toByteArray(0), 0, byteExporterIpv6, 0, 8);
    System.arraycopy(Longs.toByteArray(dpid), 0, byteExporterIpv6, 8, 8);
    Ip6Address exporterIpv6 = Ip6Address.valueOf(byteExporterIpv6);

    // Timestamps, octets, packets
    long start = System.currentTimeMillis() - (1000 * entry.life());
    long end = System.currentTimeMillis();
    long octets = entry.bytes();
    long packets = entry.packets();

    // Input and Output ports
    PortCriterion portCrit = (PortCriterion) entry.selector().getCriterion(Type.IN_PORT);
    int intfIn = (portCrit == null) ? 0 : (int) portCrit.port().toLong();
    List<Instruction> instructions = entry.treatment().allInstructions();
    int intfOut = 0;
    for (Instruction instruction : instructions) {
        if (instruction.type() == Instruction.Type.OUTPUT) {
            OutputInstruction outputInstruction = (OutputInstruction) instruction;
            intfOut = (outputInstruction == null) ? 0 : (int) outputInstruction.port().toLong();
        }
    }

    // Ethernet MACs, Ethertype and VLAN
    EthCriterion ethCrit;
    ethCrit = (EthCriterion) entry.selector().getCriterion(Type.ETH_SRC);
    MacAddress srcMac = (ethCrit == null) ? MacAddress.valueOf("00:00:00:00:00:00") : ethCrit.mac();
    ethCrit = (EthCriterion) entry.selector().getCriterion(Type.ETH_DST);
    MacAddress dstMac = (ethCrit == null) ? MacAddress.valueOf("00:00:00:00:00:00") : ethCrit.mac();

    EthTypeCriterion ethTypeCrit = (EthTypeCriterion) entry.selector().getCriterion(Type.ETH_TYPE);
    Short ethType = (ethTypeCrit == null) ? 0x0000 : ethTypeCrit.ethType().toShort();

    VlanIdCriterion vlanCrit = (VlanIdCriterion) entry.selector().getCriterion(Type.VLAN_VID);
    Short vlan = (vlanCrit == null) ? 0x0000 : vlanCrit.vlanId().toShort();

    // IP Criterion check
    IPCriterion srcIpCrit = (IPCriterion) entry.selector().getCriterion(Type.IPV4_SRC);
    IPCriterion dstIpCrit = (IPCriterion) entry.selector().getCriterion(Type.IPV4_DST);
    IPCriterion srcIp6Crit = (IPCriterion) entry.selector().getCriterion(Type.IPV6_SRC);
    IPCriterion dstIp6Crit = (IPCriterion) entry.selector().getCriterion(Type.IPV6_DST);

    // If IP criterions are null send MAC Data Record, else send IPv4 or IPv6 Data Record
    if (srcIpCrit == null && dstIpCrit == null && srcIp6Crit == null && dstIp6Crit == null) {
        DataRecordRfwdMac record = new DataRecordRfwdMac(exporterIpv4, exporterIpv6, start, end, octets,
                packets, intfIn, intfOut, srcMac, dstMac, ethType, vlan);
        List<DataRecord> recordList = new ArrayList<DataRecord>();
        recordList.add(record);
        ipfixManager.ipfixSender.sendRecords(DataRecordRfwdMac.getTemplateRecord(), recordList, dpid,
                IpfixManager.collectorIp, IpfixManager.collectorPort);
    } else {
        // Checking IPv4 and IPv6 criterions
        IPProtocolCriterion protocolCrit = (IPProtocolCriterion) entry.selector().getCriterion(Type.IP_PROTO);
        byte ipProtocol = (protocolCrit == null) ? (byte) 0xff : (byte) protocolCrit.protocol();

        IPDscpCriterion dscpCrit = (IPDscpCriterion) entry.selector().getCriterion(Type.IP_DSCP);
        byte dscp = (dscpCrit == null) ? 0x00 : dscpCrit.ipDscp();
        IPEcnCriterion ecnCrit = (IPEcnCriterion) entry.selector().getCriterion(Type.IP_ECN);
        byte ecn = (ecnCrit == null) ? 0x00 : ecnCrit.ipEcn();
        byte tos = (byte) ((byte) (dscp << 2) | ecn);

        IPv6FlowLabelCriterion flowLabelCrit = (IPv6FlowLabelCriterion) entry.selector()
                .getCriterion(Type.IPV6_FLABEL);
        int flowLabelIpv6 = (flowLabelCrit == null) ? 0 : flowLabelCrit.flowLabel();

        int srcPort = 0;
        int dstPort = 0;
        if (ipProtocol == IPv4.PROTOCOL_TCP) {
            TcpPortCriterion tcpCrit;
            tcpCrit = (TcpPortCriterion) entry.selector().getCriterion(Type.TCP_SRC);
            srcPort = (tcpCrit == null) ? 0 : tcpCrit.tcpPort().toInt();
            tcpCrit = (TcpPortCriterion) entry.selector().getCriterion(Type.TCP_DST);
            dstPort = (tcpCrit == null) ? 0 : tcpCrit.tcpPort().toInt();
        } else if (ipProtocol == IPv4.PROTOCOL_UDP) {
            UdpPortCriterion udpCrit;
            udpCrit = (UdpPortCriterion) entry.selector().getCriterion(Type.UDP_SRC);
            srcPort = (udpCrit == null) ? 0 : udpCrit.udpPort().toInt();
            udpCrit = (UdpPortCriterion) entry.selector().getCriterion(Type.UDP_DST);
            dstPort = (udpCrit == null) ? 0 : udpCrit.udpPort().toInt();
        } else if (ipProtocol == IPv4.PROTOCOL_ICMP) {
            IcmpTypeCriterion icmpTypeCrit = (IcmpTypeCriterion) entry.selector()
                    .getCriterion(Type.ICMPV4_TYPE);
            Short icmpType = (icmpTypeCrit == null) ? 0 : icmpTypeCrit.icmpType();
            IcmpCodeCriterion icmpCodeCrit = (IcmpCodeCriterion) entry.selector()
                    .getCriterion(Type.ICMPV4_CODE);
            Short icmpCode = (icmpCodeCrit == null) ? 0 : icmpCodeCrit.icmpCode();
            dstPort = 256 * icmpType + icmpCode;
        } else if (ipProtocol == IPv6.PROTOCOL_ICMP6) {
            Icmpv6TypeCriterion icmpv6TypeCrit = (Icmpv6TypeCriterion) entry.selector()
                    .getCriterion(Type.ICMPV6_TYPE);
            Short icmpType = (icmpv6TypeCrit == null) ? 0 : icmpv6TypeCrit.icmpv6Type();
            Icmpv6CodeCriterion icmpv6CodeCrit = (Icmpv6CodeCriterion) entry.selector()
                    .getCriterion(Type.ICMPV6_CODE);
            Short icmpCode = (icmpv6CodeCrit == null) ? 0 : icmpv6CodeCrit.icmpv6Code();
            dstPort = 256 * icmpType + icmpCode;
        }
        // If IPv4 than send IPv4 Data record
        if ((srcIpCrit != null || dstIpCrit != null) && ethType == Ethernet.TYPE_IPV4) {
            IpAddress srcIp = (srcIpCrit == null) ? IpAddress.valueOf(0) : srcIpCrit.ip().address();
            IpAddress dstIp = (dstIpCrit == null) ? IpAddress.valueOf(0) : dstIpCrit.ip().address();
            DataRecordRfwdIpv4 record = new DataRecordRfwdIpv4(exporterIpv4, exporterIpv6, start, end, octets,
                    packets, intfIn, intfOut, srcMac, dstMac, ethType, vlan, srcIp, dstIp, ipProtocol, tos,
                    (short) srcPort, (short) dstPort);
            List<DataRecord> recordList = new ArrayList<DataRecord>();
            recordList.add(record);
            ipfixManager.ipfixSender.sendRecords(DataRecordRfwdIpv4.getTemplateRecord(), recordList, dpid,
                    IpfixManager.collectorIp, IpfixManager.collectorPort);
        }
        // If IPv6 than send IPv6 Data record
        if ((srcIp6Crit != null || dstIp6Crit != null) && ethType == Ethernet.TYPE_IPV6) {
            Ip6Address srcIp6 = (srcIp6Crit == null) ? Ip6Address.valueOf("0:0:0:0:0:0:0:0")
                    : srcIp6Crit.ip().address().getIp6Address();
            Ip6Address dstIp6 = (dstIp6Crit == null) ? Ip6Address.valueOf("0:0:0:0:0:0:0:0")
                    : dstIp6Crit.ip().address().getIp6Address();
            DataRecordRfwdIpv6 record = new DataRecordRfwdIpv6(exporterIpv4, exporterIpv6, start, end, octets,
                    packets, intfIn, intfOut, srcMac, dstMac, ethType, vlan, srcIp6, dstIp6, flowLabelIpv6,
                    ipProtocol, tos, (short) srcPort, (short) dstPort);
            List<DataRecord> recordList = new ArrayList<DataRecord>();
            recordList.add(record);
            ipfixManager.ipfixSender.sendRecords(DataRecordRfwdIpv6.getTemplateRecord(), recordList, dpid,
                    IpfixManager.collectorIp, IpfixManager.collectorPort);
        }
    }
}

From source file:org.gaul.s3proxy.NullBlobStore.java

@Override
public String putBlob(String containerName, Blob blob, PutOptions options) {
    long length;/* w  w  w. j  a  va 2s  . c om*/
    try (InputStream is = blob.getPayload().openStream()) {
        length = ByteStreams.copy(is, ByteStreams.nullOutputStream());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    byte[] array = Longs.toByteArray(length);
    ByteSourcePayload payload = new ByteSourcePayload(ByteSource.wrap(array));
    payload.setContentMetadata(blob.getPayload().getContentMetadata());
    payload.getContentMetadata().setContentLength((long) array.length);
    payload.getContentMetadata().setContentMD5((HashCode) null);
    blob.setPayload(payload);

    return super.putBlob(containerName, blob, options);
}

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

/**
 * Serialize a double as the IEEE 754 double format output. The resultant
 * array will be 8 bytes long.//from w w  w  . j av a  2s .c  om
 *
 * @param d value
 * @return the double represented as byte []
 */
public static byte[] toBytes(final double d) {
    // Encode it as a long
    return Longs.toByteArray(Double.doubleToRawLongBits(d));
}

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.
 * /*ww  w.  jav  a  2  s  .  com*/
 * @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

/**
 * Called when this thread receives an id less than its own local id value. 
 * transmits its id to other peers in group.
 *///from  www  .j ava 2s.  co  m
public void startElection() {
    participant = true;
    byte[] msg = new byte[256];
    byte[] longByte = Longs.toByteArray(ownID);
    for (int i = 0; i < longByte.length; i++) {
        msg[i] = longByte[i];
    }
    System.out.println("free memory msg length = " + msg.length);
    byte[] timestamp = Longs.toByteArray(localPeerNode.getVectorTimeStamp());
    for (int i = 0; i < timestamp.length; i++) {
        msg[i + 9] = timestamp[i];
    }
    localPeerNode.setVectorTimeStamp();
    sendPacket(msg);
}