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.amazonaws.services.kinesis.model.transform.PutRecordRequestMarshallerTest.java

@Ignore
@Test/*from   ww  w  . j  av  a  2s.c o  m*/
public void test() throws Exception {
    PutRecordRequest putRecordRequest = new PutRecordRequest();
    putRecordRequest.setStreamName("stream name");
    putRecordRequest.setSequenceNumberForOrdering("sequence number for ordering");
    putRecordRequest.setPartitionKey("partition key");
    String randomStr = RandomStringUtils.random(128 * 1024);
    putRecordRequest.setData(ByteBuffer.wrap(randomStr.getBytes(StringUtils.UTF8)));
    PutRecordRequestMarshaller marshaller = new PutRecordRequestMarshaller();
    Request<PutRecordRequest> request = marshaller.marshall(putRecordRequest);

    assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
    byte[] content = IOUtils.toByteArray(request.getContent());
    assertEquals("content length", request.getHeaders().get("Content-Length"), String.valueOf(content.length));
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
    String str = IOUtils.toString(gis);
    Map<String, String> map = JsonUtils.jsonToMap(str);
    assertEquals("StreamName", "stream name", map.get("StreamName"));
    String data = new String(BinaryUtils.fromBase64(map.get("Data")), StringUtils.UTF8);
    assertEquals("same data", randomStr, data);
}

From source file:com.cloudera.science.avrostarter.Main.java

public Call makeCall(String center, String queue, String date, String fileName, byte[] data) {
    return Call.newBuilder().setCallCenter(center).setQueue(queue).setDateString(date).setFilename(fileName)
            .setData(ByteBuffer.wrap(data)).build();
}

From source file:it.geosolutions.geostore.core.security.password.SecurityUtils.java

/**
 * Converts byte array to char array.//from   w  w  w.  j a  v  a  2s .co m
 */
public static char[] toChars(byte[] b, Charset charset) {
    CharBuffer buff = charset.decode(ByteBuffer.wrap(b));
    char[] tmp = new char[buff.limit()];
    buff.get(tmp);
    return tmp;
}

From source file:rxweb.RxJavaServerTests.java

@Test
public void writeBuffer() throws IOException {
    server.get("/test", (request, response) -> response.status(Status.OK)
            .content(Observable.just(ByteBuffer.wrap("This is a test!".getBytes(StandardCharsets.UTF_8)))));
    String content = Request.Get("http://localhost:8080/test").execute().returnContent().asString();
    Assert.assertEquals("This is a test!", content);
}

From source file:com.amazon.kinesis.streaming.agent.processing.processors.SingleLineDataConverter.java

@Override
public ByteBuffer convert(ByteBuffer data) throws DataConversionException {
    String dataStr = ByteBuffers.toString(data, StandardCharsets.UTF_8);
    String[] lines = dataStr.split(NEW_LINE);
    for (int i = 0; i < lines.length; i++) {
        // FIXME: Shall we trim each line?
        lines[i] = lines[i].trim();//w w w. j  av a  2 s  .co m
    }

    String dataRes = StringUtils.join(lines) + NEW_LINE;
    return ByteBuffer.wrap(dataRes.getBytes(StandardCharsets.UTF_8));
}

From source file:org.ulyssis.ipp.config.Config.java

/**
 * Create a configuration from the given configuration file.
 * //from w  w w  . j  ava2s. c  o  m
 * Expects the configuration file to be UTF-8 formatted.
 */
public static Optional<Config> fromConfigurationFile(Path configFile) {
    try {
        byte[] config = Files.readAllBytes(configFile);
        Charset charset = Charset.forName("utf-8");
        String configString = charset.decode(ByteBuffer.wrap(config)).toString();
        return fromConfigurationString(configString);
    } catch (IOException e) {
        LOG.error("Error reading configuration file: {}", configFile, e);
        return Optional.empty();
    }
}

From source file:com.conwet.xjsp.features.MessageChannel.java

synchronized public void sendFragment(String fragment) throws IOException {
    ByteBuffer buffer = ByteBuffer.wrap(fragment.getBytes(StandardCharsets.UTF_8));

    try {/*w ww.j a  v a  2  s .  c  o  m*/
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }
    } catch (IOException ex) {
        logger.error("Closing connection on exception", ex);
        throw ex;
    }
}

From source file:com.inmobi.messaging.util.AuditUtil.java

private static boolean isValidHeaders(byte[] data) {
    if (data.length < HEADER_LENGTH) {
        LOG.debug("Total size of data in message is less than length of headers");
        return false;
    }/* w w  w.j a  v a2  s  . c om*/
    ByteBuffer buffer = ByteBuffer.wrap(data);
    return isValidVersion(buffer) && isValidMagicBytes(buffer) && isValidTimestamp(buffer)
            && isValidSize(buffer);
}

From source file:ec.edu.chyc.manejopersonal.util.ServerUtils.java

/***
 * Devuelve un UUID generado y codificado en Base64
 *
 * @return Uuid codificado en Base64/*w  w  w. j a va2 s  .  co  m*/
 */
public static String generateB64Uuid() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer uuidBytes = ByteBuffer.wrap(new byte[16]);
    uuidBytes.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    String asB64 = Base64.getEncoder().encodeToString(uuidBytes.array());
    return asB64;
}

From source file:io.wcm.caravan.io.http.response.ByteArrayBody.java

private static String decodeOrDefault(byte[] data, Charset charset, String defaultValue) {
    if (data == null) {
        return defaultValue;
    }//from   w w  w . ja  va  2 s .  c  om
    checkNotNull(charset, "charset");
    try {
        return charset.newDecoder().decode(ByteBuffer.wrap(data)).toString();
    } catch (CharacterCodingException ex) {
        return defaultValue;
    }
}