Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:org.redisson.codec.JsonJacksonCodec.java

private Object decode(ByteBuffer bytes) {
    try {// www  .  j a v a2s. c  om
        return objectMapper.readValue(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(),
                Object.class);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.cloudera.seismic.crunch.SUPipeline.java

public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("cwproot", true, "The path to CWPROOT on the cluster machines");
    options.addOption("input", true, "SU files in Hadoop");
    options.addOption("output", true, "The path of the SU files to write out to Hadoop");
    options.addOption("command", true, "A pipeline of SU commands to run on the data");

    // Parse the commandline and check for required arguments.
    CommandLine cmdLine = new PosixParser().parse(options, args, false);
    if (!cmdLine.hasOption("input") || !cmdLine.hasOption("command")) {
        System.out.println("Mising required input/command arguments");
        new HelpFormatter().printHelp("SUPipeline", options);
        System.exit(1);/*from   w  ww.j  a  v  a  2  s.c  o  m*/
    }

    String clusterCwproot = null;
    if (cmdLine.hasOption("cwproot")) {
        clusterCwproot = cmdLine.getOptionValue("cwproot");
    }
    if (clusterCwproot == null || clusterCwproot.isEmpty()) {
        System.out.println("Could not determine cluster's CWPROOT value");
        new HelpFormatter().printHelp("SUPipeline", options);
        System.exit(1);
    }

    Pipeline pipeline = new MRPipeline(SUPipeline.class);
    PCollection<ByteBuffer> traces = pipeline
            .read(From.sequenceFile(cmdLine.getOptionValue("input"), Writables.bytes()));
    Pair<List<String>, String> cmd = parse(cmdLine.getOptionValue("command"));
    PCollection<ByteBuffer> result = constructPipeline(traces, clusterCwproot, cmd.first());

    if (cmdLine.hasOption("output")) {
        result.write(To.sequenceFile(cmdLine.getOptionValue("output")));
    }

    if (cmd.second() != null) {
        String localCwproot = System.getenv("CWPROOT");
        if (localCwproot == null) {
            System.out.println("To use local SU commands, the CWPROOT environment variable must be set");
            System.exit(1);
        }
        String[] pieces = cmd.second().split("\\s+");
        SUProcess x = new SUProcess(localCwproot, pieces[0]);
        for (int i = 1; i < pieces.length; i++) {
            x.addArg(pieces[i]);
        }
        x.addEnvironment(ImmutableMap.of("DISPLAY", System.getenv("DISPLAY")));
        Iterator<ByteBuffer> iter = result.materialize().iterator();
        x.start();
        while (iter.hasNext()) {
            ByteBuffer bb = iter.next();
            x.write(bb.array(), bb.arrayOffset(), bb.limit());
        }
        x.closeAndWait();
    }

    if (!cmdLine.hasOption("output") && cmd.second() == null) {
        System.out.println("No output destination specified");
        System.exit(1);
    }

    pipeline.done();
    return 0;
}

From source file:com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer.java

protected SchemaIdVersion retrieveSchemaIdVersion(InputStream payloadInputStream) throws SerDesException {
    // it can be enhanced to have respective protocol handlers for different versions
    // first byte is protocol version/id.
    // protocol format:
    // 1 byte  : protocol version
    // 8 bytes : schema metadata Id
    // 4 bytes : schema version
    ByteBuffer byteBuffer = ByteBuffer.allocate(13);
    try {//from w  w  w .  j a  v  a 2 s. c  o m
        payloadInputStream.read(byteBuffer.array());
    } catch (IOException e) {
        throw new SerDesException(e);
    }

    byte protocolId = byteBuffer.get();
    if (protocolId != AvroSchemaProvider.CURRENT_PROTOCOL_VERSION) {
        throw new SerDesException(
                "Unknown protocol id [" + protocolId + "] received while deserializing the payload");
    }
    long schemaMetadataId = byteBuffer.getLong();
    int schemaVersion = byteBuffer.getInt();

    return new SchemaIdVersion(schemaMetadataId, schemaVersion);
}

From source file:org.redisson.codec.JsonJacksonCodec.java

@Override
public Object decodeMapValue(ByteBuffer bytes) {
    try {//from  w ww  .  ja v a 2 s  .  co  m
        return mapObjectMapper.readValue(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(),
                Object.class);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

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 {//from w  ww  .ja  v  a  2s .  c  om
        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())));
    }
}

From source file:com.crushpaper.UuidlIdGenerator.java

@Override
public String getAnotherId() {
    final UUID uuid = UUID.randomUUID();
    final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:MainClass.java

public void run() {

    ByteBuffer sizeb = ByteBuffer.allocate(4);
    try {//from   w w w.  j a v a2  s . co m
        while (sizeb.hasRemaining())
            in.read(sizeb);
        sizeb.flip();
        int howMany = sizeb.getInt();
        sizeb.clear();

        for (int i = 0; i < howMany; i++) {
            while (sizeb.hasRemaining())
                in.read(sizeb);
            sizeb.flip();
            int length = sizeb.getInt();
            sizeb.clear();

            ByteBuffer data = ByteBuffer.allocate(length);
            while (data.hasRemaining())
                in.read(data);

            BigInteger result = new BigInteger(data.array());
            System.out.println(result);
        }
    } catch (IOException ex) {
        System.err.println(ex);
    } finally {
        try {
            in.close();
        } catch (Exception ex) {
            // We tried
        }
    }
}

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

@Test
public void testBinary() throws Exception {
    LOG.info("Client starting....");
    String name = "Hello ";
    // TTransport transport = new TNonblockingSocket("localhost", 9804);
    TTransport transport = new TSocket("localhost", 9804);
    transport.open();//from w  ww . j  a  v  a  2 s .c o m
    // TTransport transport = new TTransport(socket);
    TProtocol protocol = new TBinaryProtocol(transport);
    HelloService.Client client = new HelloService.Client(protocol);
    try {

        ByteBuffer buffer = client.testBinary(ByteBuffer.wrap(name.getBytes("UTF-8")));

        assertEquals(new String(buffer.array(), buffer.position(), buffer.limit() - buffer.position(), "UTF-8"),
                "Hello " + name);
        // System.out.println(message);
    } finally {
        transport.close();
    }
}

From source file:org.alfresco.cacheserver.TestEdgeServer.java

private void compare(ByteBuffer expected, ByteBuffer actual) {
    Arrays.equals(expected.array(), actual.array());
}

From source file:ch.cyberduck.core.sds.triplecrypt.CryptoInputStream.java

private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if (lastread == 0) {
        return IOUtils.EOF;
    }//from  ww  w  .ja  va 2 s . co  m
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        final PlainDataContainer pDataContainer;
        if (read == 0) {
            final PlainDataContainer c1 = cipher
                    .processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
            final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
            pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
        } else {
            pDataContainer = cipher
                    .processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
        }
        final byte[] content = pDataContainer.getContent();
        buffer = ByteBuffer.allocate(content.length);
        buffer.put(content);
        buffer.flip();
        lastread = read;
        return content.length;
    } catch (CryptoException e) {
        throw new IOException(e);
    }
}