Java Code Examples for com.couchbase.client.deps.io.netty.buffer.ByteBuf#readBytes()

The following examples show how to use com.couchbase.client.deps.io.netty.buffer.ByteBuf#readBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CouchbaseTableReadFunction.java    From samza with Apache License 2.0 6 votes vote down vote up
protected void handleGetAsyncBinaryDocument(BinaryDocument binaryDocument, CompletableFuture<V> future, String key) {
  ByteBuf buffer = binaryDocument.content();
  try {
    byte[] bytes;
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.readableBytes() == buffer.array().length) {
      bytes = buffer.array();
    } else {
      bytes = new byte[buffer.readableBytes()];
      buffer.readBytes(bytes);
    }
    future.complete(valueSerde.fromBytes(bytes));
  } catch (Exception e) {
    future.completeExceptionally(
        new SamzaException(String.format("Failed to deserialize value of key %s with given serde", key), e));
  } finally {
    ReferenceCountUtil.release(buffer);
  }
}
 
Example 2
Source File: CouchbaseUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static String getStringContent(Object content) {
    if (content instanceof String) {
        return (String) content;
    } else if (content instanceof byte[]) {
        return new String((byte[]) content, StandardCharsets.UTF_8);
    } else if (content instanceof ByteBuf) {
        final ByteBuf byteBuf = (ByteBuf) content;
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        byteBuf.release();
        return new String(bytes, StandardCharsets.UTF_8);
    }
    return content.toString();
}
 
Example 3
Source File: CouchbaseMapCacheClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
private <V> V deserialize(BinaryDocument doc, Deserializer<V> valueDeserializer) throws IOException {
    if (doc == null) {
        return null;
    }
    final ByteBuf byteBuf = doc.content();
    final byte[] bytes = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(bytes);
    byteBuf.release();
    return valueDeserializer.deserialize(bytes);
}
 
Example 4
Source File: CouchbaseEventGenericRecordConverter.java    From components with Apache License 2.0 4 votes vote down vote up
private static byte[] bufToBytes(ByteBuf buf) {
    byte[] bytes;
    bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);
    return bytes;
}