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:com.cloudera.flume.handlers.thrift.ThriftEventAdaptor.java

@Override
public Map<String, byte[]> getAttrs() {
    if (evt.fields == null) {
        return Collections.<String, byte[]>emptyMap();
    }//from  w  w  w  .  j  a v  a 2 s  .co  m
    Map<String, ByteBuffer> tempMap = Collections.unmodifiableMap(evt.fields);
    Map<String, byte[]> returnMap = new HashMap<String, byte[]>();
    for (String key : tempMap.keySet()) {
        ByteBuffer buf = tempMap.get(key);
        returnMap.put(key, buf.array());
    }
    return Collections.unmodifiableMap(returnMap);
}

From source file:gobblin.writer.SimpleDataWriter.java

/**
 * Write a source record to the staging file
 *
 * @param record data record to write//from  ww w.  j ava 2s .  c o m
 * @throws java.io.IOException if there is anything wrong writing the record
 */
@Override
public void write(byte[] record) throws IOException {
    Preconditions.checkNotNull(record);

    byte[] toWrite = record;
    if (this.recordDelimiter.isPresent()) {
        toWrite = Arrays.copyOf(record, record.length + 1);
        toWrite[toWrite.length - 1] = this.recordDelimiter.get();
    }
    if (this.prependSize) {
        long recordSize = toWrite.length;
        ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES);
        buf.putLong(recordSize);
        toWrite = ArrayUtils.addAll(buf.array(), toWrite);
    }
    this.stagingFileOutputStream.write(toWrite);
    this.bytesWritten += toWrite.length;
    this.recordsWritten++;
}

From source file:ch.cyberduck.core.cryptomator.features.CryptoReadFeature.java

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback)
        throws BackgroundException {
    try {//from ww w  . j a v  a2  s.  c o m
        final Path encrypted = vault.encrypt(session, file);
        // Header
        final Cryptor cryptor = vault.getCryptor();
        final TransferStatus headerStatus = new TransferStatus(status);
        headerStatus.setOffset(0);
        final InputStream in = proxy.read(encrypted,
                headerStatus.length(status.isAppend() ? cryptor.fileHeaderCryptor().headerSize()
                        : vault.toCiphertextSize(status.getLength())),
                callback);
        final ByteBuffer headerBuffer = ByteBuffer.allocate(cryptor.fileHeaderCryptor().headerSize());
        final int read = IOUtils.read(in, headerBuffer.array());
        final FileHeader header = cryptor.fileHeaderCryptor().decryptHeader(headerBuffer);
        if (status.isAppend()) {
            IOUtils.closeQuietly(in);
            final TransferStatus s = new TransferStatus(status).length(-1L);
            s.setOffset(this.align(status.getOffset()));
            final CryptoInputStream crypto = new CryptoInputStream(proxy.read(encrypted, s, callback), cryptor,
                    header, this.chunk(status.getOffset()));
            crypto.skip(this.position(status.getOffset()));
            return crypto;
        } else {
            return new CryptoInputStream(in, cryptor, header, vault.numberOfChunks(status.getOffset()));
        }
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}

From source file:com.amazonaws.services.dynamodbv2.streamsadapter.model.RecordAdapterTest.java

@Test
public void testGetData() {
    java.nio.ByteBuffer data = adapter.getData();
    Record actual = null;/*from   w  w  w . j  ava2 s  . c  om*/
    try {
        actual = MAPPER.readValue(data.array(), Record.class);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertEquals(testRecord, actual);
}

From source file:com.homeadvisor.kafdrop.service.MessageInspector.java

private byte[] readBytes(ByteBuffer buffer, int offset, int size) {
    byte[] dest = new byte[size];
    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size);
    } else {/* w  w  w. j av a2  s .c  o m*/
        buffer.mark();
        buffer.get(dest);
        buffer.reset();
    }
    return dest;
}

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

@Test
public void testBinary() throws Exception {
    LOG.info("Client starting....");
    String name = "Hello";
    // TTransport transport = new TNonblockingSocket("localhost", 9804);
    TTransport transport = new TFramedTransport(new TSocket("localhost", 9804));
    transport.open();/*from   ww  w .ja  va  2  s  .c  om*/
    // 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:com.jivesoftware.os.rcvs.api.keys.SymetricalHashableKeyTest.java

License:asdf

public byte[] longBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(x);/*  w  w  w . j ava 2s.com*/
    return buffer.array();
}

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

@Override
public ByteBuffer hello(ByteBuffer hello_request) throws TException {
    HelloRequest request;/* ww w.  j a  va 2s.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:com.unister.semweb.drums.file.RepairIndexTest.java

/**
 * Converts the given <code>value</code> to a byte array representation. The final array has
 * <code>numberOfBytes</code>.
 *///from   w  ww.j  av  a2  s.c  o  m
private byte[] transform(int value, int numberOfBytes) {
    ByteBuffer buffer = ByteBuffer.allocate(numberOfBytes);
    buffer.putInt(numberOfBytes - 4, value);
    return buffer.array();
}

From source file:Main.java

public final static int substringBetweenLast(final byte[] text, final int offset, final int limit,
        final byte[] start, final byte[] end, final boolean trim, ByteBuffer bb) {
    int nEnd = lastIndexOf(text, offset, limit, end);
    int nStart = -1;
    if (nEnd > start.length) {
        nStart = lastIndexOf(text, offset, nEnd - 1, start);
        if (nStart < nEnd && nStart != -1 && nEnd != -1) {
            nStart += start.length;/* w w w .  j a va2 s. c o  m*/
            if (trim) {
                byte c;
                int i;
                for (i = nStart; i < nEnd; i++) {
                    c = text[i];
                    if (c != ' ' && c != '\t' && c != '\r') {
                        break;
                    }
                }
                nStart = i;
                for (i = nEnd; i >= nStart; i--) {
                    c = text[i];
                    if (c != ' ' && c != '\t' && c != '\r') {
                        break;
                    }
                }
                nEnd = i;
            }
            if (nEnd > nStart) {
                int len = nEnd - nStart;
                System.arraycopy(text, nStart, bb.array(), 0, len);
                bb.limit(len);
            } else {
                bb.limit(0);
            }
        } else {
            bb.limit(0);
        }
    } else {
        bb.limit(0);
    }
    return bb.limit();
}