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

The following examples show how to use com.couchbase.client.deps.io.netty.buffer.ByteBuf#readableBytes() . 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: CouchbaseWriterTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
void onWrite(AbstractDocument doc)
    throws UnsupportedEncodingException {
  recordClass = doc.getClass();
  if (doc instanceof TupleDocument) {
    ByteBuf outgoingBuf = (((TupleDocument) doc).content()).value1();
    byte[] outgoingBytes = new byte[outgoingBuf.readableBytes()];
    outgoingBuf.getBytes(0, outgoingBytes);
    verificationCache.put(doc.id(), outgoingBytes);
  } else if (doc instanceof RawJsonDocument) {
    verificationCache.put(doc.id(), ((RawJsonDocument) doc).content().getBytes("UTF-8"));
  } else {
    throw new UnsupportedOperationException("Can only support TupleDocument or RawJsonDocument at this time");
  }
}
 
Example 3
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 4
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 5
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;
}