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

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

Introduction

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

Prototype

@GwtIncompatible("doesn't work")
public static byte[] toByteArray(int value) 

Source Link

Document

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

Usage

From source file:com.datatorrent.flume.storage.HDFSStorageMatching.java

public static void main(String[] args) {
    HDFSStorage storage = new HDFSStorage();
    storage.setBaseDir(args[0]);//from  w  w  w.  ja  v  a2  s. com
    storage.setId(args[1]);
    storage.setRestore(true);
    storage.setup(null);
    int count = 100000000;

    logger.debug(" start time {}", System.currentTimeMillis());
    int index = 10000;
    byte[] b = Ints.toByteArray(index);
    for (int i = 0; i < count; i++) {
        storage.store(new Slice(b, 0, b.length));
        index++;
        b = Ints.toByteArray(index);
    }
    storage.flush();
    for (int i = 0; i < count; i++) {
        storage.store(new Slice(b, 0, b.length));
        index++;
        b = Ints.toByteArray(index);
    }
    storage.flush();
    for (int i = 0; i < count; i++) {
        storage.store(new Slice(b, 0, b.length));
        index++;
        b = Ints.toByteArray(index);
    }
    storage.flush();
    for (int i = 0; i < count; i++) {
        storage.store(new Slice(b, 0, b.length));
        index++;
        b = Ints.toByteArray(index);
    }
    storage.flush();
    for (int i = 0; i < count; i++) {
        storage.store(new Slice(b, 0, b.length));
        index++;
        b = Ints.toByteArray(index);
    }
    storage.flush();
    logger.debug(" end time {}", System.currentTimeMillis());
    logger.debug(" start time for retrieve {}", System.currentTimeMillis());
    b = storage.retrieve(new byte[8]);
    int org_index = index;
    index = 10000;
    match(b, index);
    while (true) {
        index++;
        b = storage.retrieveNext();
        if (b == null) {
            logger.debug(" end time for retrieve {}/{}/{}", System.currentTimeMillis(), index, org_index);
            return;
        } else {
            if (!match(b, index)) {
                throw new RuntimeException("failed : " + index);
            }
        }
    }

}

From source file:com.google.api.services.samples.storage.cmdline.StorageSample.java

public static void main(String[] args) {
    try {/*from  w  ww  .  ja  v a  2s. c om*/
        // initialize network, sample settings, credentials, and the storage client.
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        SampleSettings settings = SampleSettings.load(jsonFactory);
        Credential credential = CredentialsProvider.authorize(httpTransport, jsonFactory);
        Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential)
                .setApplicationName(APPLICATION_NAME).build();

        //
        // run commands
        //
        View.header1("Trying to create a new bucket " + settings.getBucket());
        BucketsInsertExample.createInProject(storage, settings.getProject(),
                new Bucket().setName(settings.getBucket()).setLocation("US"));

        View.header1("Getting bucket " + settings.getBucket() + " metadata");
        Bucket bucket = BucketsGetExample.get(storage, settings.getBucket());
        View.show(bucket);

        View.header1("Listing objects in bucket " + settings.getBucket());
        for (StorageObject object : ObjectsListExample.list(storage, settings.getBucket())) {
            View.show(object);
        }

        View.header1("Getting object metadata from gs://pub/SomeOfTheTeam.jpg");
        StorageObject object = ObjectsGetMetadataExample.get(storage, "pub", "SomeOfTheTeam.jpg");
        View.show(object);

        View.header1("Uploading object.");
        final long objectSize = 100 * 1024 * 1024 /* 100 MB */;
        InputStream data = new Helpers.RandomDataBlockInputStream(objectSize, 1024);
        object = new StorageObject().setBucket(settings.getBucket()).setName(settings.getPrefix() + "myobject")
                .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
                .setCacheControl("max-age=3600, must-revalidate").setContentDisposition("attachment");
        object = ObjectsUploadExample.uploadWithMetadata(storage, object, data);
        View.show(object);
        System.out.println("md5Hash: " + object.getMd5Hash());
        System.out.println("crc32c: " + object.getCrc32c() + ", decoded to "
                + ByteBuffer.wrap(BaseEncoding.base64().decode(object.getCrc32c())).getInt());

        View.header1("Getting object data of uploaded object, calculate hashes/crcs.");
        OutputStream nullOutputStream = new OutputStream() {
            // Throws away the bytes.
            @Override
            public void write(int b) throws IOException {
            }

            @Override
            public void write(byte b[], int off, int len) {
            }
        };
        DigestOutputStream md5DigestOutputStream = new DigestOutputStream(nullOutputStream,
                MessageDigest.getInstance("MD5"));
        HashingOutputStream crc32cHashingOutputStream = new HashingOutputStream(Hashing.crc32c(),
                md5DigestOutputStream);
        ObjectsDownloadExample.downloadToOutputStream(storage, settings.getBucket(),
                settings.getPrefix() + "myobject", crc32cHashingOutputStream);
        String calculatedMD5 = BaseEncoding.base64().encode(md5DigestOutputStream.getMessageDigest().digest());
        System.out.println(
                "md5Hash: " + calculatedMD5 + " " + (object.getMd5Hash().equals(calculatedMD5) ? "(MATCHES)"
                        : "(MISMATCHES; data altered in transit)"));
        int calculatedCrc32c = crc32cHashingOutputStream.hash().asInt();
        String calculatedEncodedCrc32c = BaseEncoding.base64().encode(Ints.toByteArray(calculatedCrc32c));
        // NOTE: Don't compare HashCode.asBytes() directly, as that encodes the crc32c in
        // little-endien. One would have to reverse the bytes first.
        System.out.println("crc32c: " + calculatedEncodedCrc32c + ", decoded to "
                + crc32cHashingOutputStream.hash().asInt() + " "
                + (object.getCrc32c().equals(calculatedEncodedCrc32c) ? "(MATCHES)"
                        : "(MISMATCHES; data altered in transit)"));

        // success!
        return;
    } catch (GoogleJsonResponseException e) {
        // An error came back from the API.
        GoogleJsonError error = e.getDetails();
        System.err.println(error.getMessage());
        // More error information can be retrieved with error.getErrors().
    } catch (HttpResponseException e) {
        // No JSON body was returned by the API.
        System.err.println(e.getHeaders());
        System.err.println(e.getMessage());
    } catch (IOException e) {
        // Error formulating a HTTP request or reaching the HTTP service.
        System.err.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}

From source file:com.axiomine.largecollections.utils.SerDeUtils.java

public static byte[] integerToByteArray(int o) {
    return Ints.toByteArray(o);
}

From source file:com.github.zhongl.util.FileAsserter.java

public static byte[] length(int value) {
    return Ints.toByteArray(value);
}

From source file:org.apache.mahout.math.MurmurHash.java

/**
 * Hashes an int./*w ww .j  a va 2  s  . com*/
 * @param data The int to hash.
 * @param seed The seed for the hash.
 * @return The 32 bit hash of the bytes in question.
 */
public static int hash(int data, int seed) {
    return hash(ByteBuffer.wrap(Ints.toByteArray(data)), seed);
}

From source file:se.sics.ktoolbox.cc.heartbeat.util.CCKeyFactory.java

public static Key getHeartbeatKey(byte[] schemaPrefix, Identifier overlayId, int slot) {
    Key.KeyBuilder key = new Key.KeyBuilder(schemaPrefix);
    ByteBuf buf = Unpooled.buffer();//from w  w w. ja  v a2  s  .  c  o m
    Serializers.toBinary(overlayId, buf);
    byte[] oId = Arrays.copyOfRange(buf.array(), 0, buf.readableBytes());
    key.append(new byte[] { (byte) oId.length });
    key.append(oId);
    key.append(Ints.toByteArray(slot));
    return key.get();
}

From source file:org.b1.pack.standard.common.PbPlainBlock.java

private static Writable createPlainBlock(Writable content, int checksum) {
    return new CompositeWritable(new PbBinary(content), new ByteArrayWritable(Ints.toByteArray(checksum)));
}

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

@NotNull
public static UnsignedByteArray from(final int i) {
    return from(Ints.toByteArray(i ^ Integer.MIN_VALUE));
}

From source file:net.bither.bitherj.core.OutPoint.java

public OutPoint(byte[] txHash, int outSn) {
    this.txHash = txHash;
    this.outSn = outSn;
    this.bytes = new byte[In.OUTPOINT_MESSAGE_LENGTH];
    System.arraycopy(this.txHash, 0, this.bytes, 0, this.txHash.length);
    System.arraycopy(Ints.toByteArray(this.outSn), 0, this.bytes, this.txHash.length,
            In.OUTPOINT_MESSAGE_LENGTH - this.txHash.length);
}

From source file:com.metamx.druid.kv.VSizeIndexedInts.java

public static VSizeIndexedInts fromList(List<Integer> list, int maxValue) {
    int numBytes = getNumBytesForMax(maxValue);

    final ByteBuffer buffer = ByteBuffer.allocate((list.size() * numBytes) + (4 - numBytes));
    int i = 0;//from   w w w .j  a va  2s .  co m
    for (Integer val : list) {
        if (val < 0) {
            throw new IAE("integer values must be positive, got[%d], i[%d]", val, i);
        }
        if (val > maxValue) {
            throw new IAE("val[%d] > maxValue[%d], please don't lie about maxValue.  i[%d]", val, maxValue, i);
        }

        byte[] intAsBytes = Ints.toByteArray(val);
        buffer.put(intAsBytes, intAsBytes.length - numBytes, numBytes);
        ++i;
    }
    buffer.position(0);

    return new VSizeIndexedInts(buffer.asReadOnlyBuffer(), numBytes);
}