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:com.yandex.yoctodb.v1.mutable.segment.AbstractV1FullIndex.java

@NotNull
@Override/*from   ww w.j  a v a  2s .  co  m*/
public OutputStreamWritable buildWritable() {
    checkNotFrozen();

    freeze();

    // Building index

    final IndexToIndexMultiMap valueToDocumentsIndex = new IntIndexToIndexMultiMap(
            valueToDocuments.asMap().values());

    final ByteArraySortedSet values;
    if (fixedLength) {
        values = new FixedLengthByteArraySortedSet(valueToDocuments.keySet());
    } else {
        values = new VariableLengthByteArraySortedSet(valueToDocuments.keySet());
    }

    final IndexToIndexMap documentToValueIndex = new IntIndexToIndexMap();
    for (Map.Entry<Integer, UnsignedByteArray> entry : documentToValue.entrySet()) {
        documentToValueIndex.put(entry.getKey(), values.indexOf(entry.getValue()));
    }

    // Free memory
    valueToDocuments = null;
    documentToValue = null;

    return new OutputStreamWritable() {
        @Override
        public long getSizeInBytes() {
            return 4L + // Field name
            fieldName.length + 8 + // Values
            values.getSizeInBytes() + 8 + // Value to documents
            valueToDocumentsIndex.getSizeInBytes() + 8 + // Document to value
            documentToValueIndex.getSizeInBytes();
        }

        @Override
        public void writeTo(@NotNull final OutputStream os) throws IOException {
            os.write(Longs.toByteArray(getSizeInBytes()));

            // Payload segment type
            os.write(Ints.toByteArray(segmentType.getCode()));

            // Field name
            os.write(Ints.toByteArray(fieldName.length));
            os.write(fieldName);

            // Values
            os.write(Longs.toByteArray(values.getSizeInBytes()));
            values.writeTo(os);

            // Value to documents
            os.write(Longs.toByteArray(valueToDocumentsIndex.getSizeInBytes()));
            valueToDocumentsIndex.writeTo(os);

            // Document to value
            os.write(Longs.toByteArray(documentToValueIndex.getSizeInBytes()));
            documentToValueIndex.writeTo(os);
        }
    };
}

From source file:com.continuuity.loom.common.zookeeper.IdService.java

private long generateId(Type type) {
    idLock.get().acquire();//  w  ww .j  a  v  a 2s.  c o  m
    try {
        NodeData nodeData = Futures.getUnchecked(zkClient.getData(type.path));
        long counterVal = Longs.fromByteArray(nodeData.getData());
        Futures.getUnchecked(zkClient.setData(type.path, Longs.toByteArray(counterVal + incrementBy)));
        return counterVal;
    } finally {
        idLock.get().release();
    }
}

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

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

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

@Override
public byte[] getBytes() throws HeaderException {
    try {/*from  ww  w  . j  a va 2s.c  o  m*/
        byte[] data = new byte[LENGTH];

        System.arraycopy(exporterIPv4Address.toOctets(), 0, data, 0, 4);
        System.arraycopy(exporterIPv6Address.toOctets(), 0, data, 4, 16);
        System.arraycopy(Longs.toByteArray(flowStartMilliseconds), 0, data, 20, 8);
        System.arraycopy(Longs.toByteArray(flowEndMilliseconds), 0, data, 28, 8);
        System.arraycopy(Longs.toByteArray(octetDeltaCount), 0, data, 36, 8);
        System.arraycopy(Longs.toByteArray(packetDeltaCount), 0, data, 44, 8);
        System.arraycopy(Ints.toByteArray(ingressInterface), 0, data, 52, 4);
        System.arraycopy(Ints.toByteArray(egressInterface), 0, data, 56, 4);
        System.arraycopy(sourceMacAddress.toBytes(), 0, data, 60, 6);
        System.arraycopy(destinationMacAddress.toBytes(), 0, data, 66, 6);
        System.arraycopy(Shorts.toByteArray(ethernetType), 0, data, 72, 2);
        System.arraycopy(Shorts.toByteArray(vlanId), 0, data, 74, 2);
        System.arraycopy(sourceIPv4Address.toOctets(), 0, data, 76, 4);
        System.arraycopy(destinationIPv4Address.toOctets(), 0, data, 80, 4);
        data[84] = protocolIdentifier;
        data[85] = ipClassOfService;
        System.arraycopy(Shorts.toByteArray(sourceTransportPort), 0, data, 86, 2);
        System.arraycopy(Shorts.toByteArray(destinationTransportPort), 0, data, 88, 2);

        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}

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

@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values,
        long fileLength) {
    Jedis jedis = openJedis();/*from   ww w  .  jav  a 2  s  . co m*/
    Pipeline pipelined = jedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = jedis.pipelined();
        }
    }
    pipelined.sync();
    jedis.close();
    values.clear();
}

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

@Override
public byte[] getBytes() throws HeaderException {
    try {//from  w w  w.ja v a2 s  .  co  m
        byte[] data = new byte[LENGTH];

        System.arraycopy(exporterIPv4Address.toOctets(), 0, data, 0, 4);
        System.arraycopy(exporterIPv6Address.toOctets(), 0, data, 4, 16);
        System.arraycopy(Longs.toByteArray(flowStartMilliseconds), 0, data, 20, 8);
        System.arraycopy(Longs.toByteArray(flowEndMilliseconds), 0, data, 28, 8);
        System.arraycopy(Longs.toByteArray(octetDeltaCount), 0, data, 36, 8);
        System.arraycopy(Longs.toByteArray(packetDeltaCount), 0, data, 44, 8);
        System.arraycopy(Ints.toByteArray(ingressInterface), 0, data, 52, 4);
        System.arraycopy(Ints.toByteArray(egressInterface), 0, data, 56, 4);
        System.arraycopy(sourceMacAddress.toBytes(), 0, data, 60, 6);
        System.arraycopy(destinationMacAddress.toBytes(), 0, data, 66, 6);
        System.arraycopy(Shorts.toByteArray(ethernetType), 0, data, 72, 2);
        System.arraycopy(Shorts.toByteArray(vlanId), 0, data, 74, 2);
        System.arraycopy(sourceIPv6Address.toOctets(), 0, data, 76, 16);
        System.arraycopy(destinationIPv6Address.toOctets(), 0, data, 92, 16);
        System.arraycopy(Ints.toByteArray(flowLabelIpv6), 0, data, 108, 4);
        data[112] = protocolIdentifier;
        data[113] = ipClassOfService;
        System.arraycopy(Shorts.toByteArray(sourceTransportPort), 0, data, 114, 2);
        System.arraycopy(Shorts.toByteArray(destinationTransportPort), 0, data, 116, 2);

        return data;
    } catch (Exception e) {
        throw new HeaderException("Error while generating the bytes: " + e.getMessage());
    }
}

From source file:com.continuuity.loom.common.zookeeper.IdService.java

private void initializeCounter(Type type) {
    Stat stat = Futures.getUnchecked(zkClient.exists(type.path));
    if (stat == null) {
        Futures.getUnchecked(//from   w w w . j  ava  2 s.c  o  m
                zkClient.create(type.path, Longs.toByteArray(startId), CreateMode.PERSISTENT, true));
    }
}

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

/**
 * Use transactions to add index file and then delete the old one
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param oldField      the old hash field
 * @param newField      the new hash field
 * @param values        the data values of the old hash field
 * @param fileLength    the data length of the old hash field
 *///from w w  w. j  av a  2s.  co m
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField,
        List<byte[]> values, long fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.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();
    shardedJedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}

From source file:com.linkedin.pinot.tools.admin.command.StreamAvroIntoKafkaCommand.java

@Override
public boolean execute() throws IOException {
    int messageDelayMillis = Integer.parseInt(_millisBetweenMessages);
    final boolean sleepRequired = 0 < messageDelayMillis;

    if (sleepRequired) {
        LOGGER.info("Streaming Avro file into Kafka topic {} with {} ms between messages", _kafkaTopic,
                _millisBetweenMessages);
    } else {/*from ww w .j  ava  2  s .c o  m*/
        LOGGER.info("Streaming Avro file into Kafka topic {} with no delay between messages", _kafkaTopic);
    }

    // Create Kafka producer
    Properties properties = new Properties();
    properties.put("metadata.broker.list", _kafkaBrokerList);
    properties.put("serializer.class", "kafka.serializer.DefaultEncoder");
    properties.put("request.required.acks", "1");

    ProducerConfig producerConfig = new ProducerConfig(properties);
    Producer<byte[], byte[]> producer = new Producer<byte[], byte[]>(producerConfig);
    try {
        // Open the Avro file
        DataFileStream<GenericRecord> reader = AvroUtils.getAvroReader(new File(_avroFile));

        // Iterate over every record
        for (GenericRecord genericRecord : reader) {
            // Write the message to Kafka
            String recordJson = genericRecord.toString();
            byte[] bytes = recordJson.getBytes("utf-8");
            KeyedMessage<byte[], byte[]> data = new KeyedMessage<byte[], byte[]>(_kafkaTopic,
                    Longs.toByteArray(HashUtil.hash64(bytes, bytes.length)), bytes);

            producer.send(data);

            // Sleep between messages
            if (sleepRequired) {
                Uninterruptibles.sleepUninterruptibly(messageDelayMillis, TimeUnit.MILLISECONDS);
            }
        }

        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    savePID(System.getProperty("java.io.tmpdir") + File.separator + ".streamAvro.pid");
    return true;
}

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

@Override
public String completeMultipartUpload(final MultipartUpload mpu, final List<MultipartPart> parts) {
    long length = 0;
    for (MultipartPart part : parts) {
        length += part.partSize();//w  w  w  . j  a  v  a2 s. co  m
    }

    byte[] array = Longs.toByteArray(length);
    ByteSourcePayload payload = new ByteSourcePayload(ByteSource.wrap(array));

    super.abortMultipartUpload(mpu);

    MultipartPart part = delegate().uploadMultipartPart(mpu, 1, payload);

    return delegate().completeMultipartUpload(mpu, ImmutableList.of(part));
}