Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

In this page you can find the example usage for java.nio ByteBuffer wrap.

Prototype

public static ByteBuffer wrap(byte[] array) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:com.jordanwilliams.heftydb.test.generator.KeyValueGenerator.java

private ByteBuffer randomKey(int size) {
    return ByteBuffer.wrap(RandomStringUtils.randomAlphanumeric(size).getBytes());
}

From source file:com.hazelcast.simulator.probes.xml.HistogramConverter.java

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
    String encodedHistogram = reader.getValue();

    try {/*w  w  w .  j  av  a  2 s.  c o m*/
        byte[] bytes = decodeBase64(encodedHistogram);
        return decodeFromCompressedByteBuffer(ByteBuffer.wrap(bytes), 0);
    } catch (Exception e) {
        throw new IllegalArgumentException("Could not parse histogram", e);
    }
}

From source file:net.phoenix.thrift.hello.SpringHelloService.java

@Override
public ByteBuffer hello(ByteBuffer hello_request) throws TException {
    HelloRequest request;//from w  w  w. j a  v a2  s. c  o  m
    try {
        request = HelloRequest.parseFrom(
                ArrayUtils.subarray(hello_request.array(), hello_request.position(), hello_request.limit()));
    } catch (InvalidProtocolBufferException e) {
        throw new TException(e);
    }
    HelloResponse.Builder response = HelloResponse.newBuilder();
    response.setMessage("Hello " + request.getName());
    return ByteBuffer.wrap(response.build().toByteArray());
}

From source file:edu.hawaii.soest.hioos.storx.StorXParserTest.java

@Before
public void setUp() {

    // read the sample data from a binary StorX file
    try {//from ww  w. j  a v a2  s  . c  o m

        // Set up a simple logger that logs to the console
        BasicConfigurator.configure();

        // create a byte buffer from the binary file data
        storXData = this.getClass().getResourceAsStream("/edu/hawaii/soest/kilonalu/ctd/16364020.RAW");
        dataAsByteArray = IOUtils.toByteArray(storXData);
        buffer = ByteBuffer.wrap(dataAsByteArray);

        // create a parser instance and test that it succeeds
        this.parser = new StorXParser();

    } catch (IOException ioe) {
        fail("There was a problem reading the" + " data file.  The error was: " + ioe.getMessage());

    } catch (NullPointerException npe) {
        fail("There was a problem reading the" + " data file.  The error was: " + npe.getMessage());

    }

}

From source file:com.mh2c.LogGenerator.java

/**
 * Generates log lines and sends them to Kinesis.
 *
 * @param streamName Kinesis stream name
 * @param recsPerSecond number of records to send each second
 * @param numRecords total number of records to send
 *///from  ww  w  .j a v  a2 s  . co  m
public void generate(String streamName, int recsPerSecond, int numRecords) throws InterruptedException {

    AmazonKinesisClient client = new AmazonKinesisClient();

    int numPasses = (numRecords + recsPerSecond - 1) / recsPerSecond;
    int recordsLeft = numRecords;
    for (int i = 0; i < numPasses; i++) {
        int numToGenerate = Math.min(recordsLeft, recsPerSecond);
        for (int j = 0; j < numToGenerate; j++) {
            String logLine = generateLogLine();

            PutRecordRequest request = new PutRecordRequest().withStreamName(streamName)
                    .withPartitionKey(PARTITION_KEY)
                    .withData(ByteBuffer.wrap(logLine.getBytes(StandardCharsets.UTF_8)));
            PutRecordResult result = client.putRecord(request);
            System.out.println(
                    String.format("Wrote to shard %s as %s", result.getShardId(), result.getSequenceNumber()));
        }

        recordsLeft -= numToGenerate;
        if (recordsLeft > 0) {
            Thread.sleep(1000L);
        }
    }
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static ByteBuffer bytes(String s) {
    try {/* w w  w . j a  v  a  2  s  .co  m*/
        return ByteBuffer.wrap(s.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.kotcrab.vis.editor.module.editor.AnalyticsModule.java

public static int truncateUtf8(String input, byte[] output) {
    ByteBuffer outBuf = ByteBuffer.wrap(output);
    CharBuffer inBuf = CharBuffer.wrap(input.toCharArray());

    Charset utf8 = Charset.forName("UTF-8");
    utf8.newEncoder().encode(inBuf, outBuf, true);
    return outBuf.position();
}

From source file:org.cloudfoundry.metron.MetronMetricWriterTest.java

private static void verify(Async async, Message<?, ?> message) {
    Mockito.verify(async).sendBinary(ByteBuffer.wrap(message.encode()));
}

From source file:com.datatorrent.contrib.hdht.HDHTDynamicPartitioningTest.java

public long getLong(byte[] value) throws IOException {
    ByteBuffer bb = ByteBuffer.wrap(value);
    return bb.getLong();
}

From source file:de.zalando.spring.cloud.config.aws.kms.KmsTextEncryptor.java

@Override
public String encrypt(final String text) {
    Assert.hasText(kmsKeyId, "kmsKeyId must not be blank");
    if (text == null || text.isEmpty()) {
        return EMPTY_STRING;
    } else {/* w w w  .  j  a v a  2 s  .  c o m*/
        final EncryptRequest encryptRequest = new EncryptRequest().withKeyId(kmsKeyId) //
                .withPlaintext(ByteBuffer.wrap(text.getBytes()));

        final ByteBuffer encryptedBytes = kms.encrypt(encryptRequest).getCiphertextBlob();

        return extractString(ByteBuffer.wrap(Base64.encode(encryptedBytes.array())));
    }
}