本文整理了Java中com.couchbase.client.deps.io.netty.buffer.ByteBuf.readableBytes()
方法的一些代码示例,展示了ByteBuf.readableBytes()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.readableBytes()
方法的具体详情如下:
包路径:com.couchbase.client.deps.io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:readableBytes
[英]Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex).
[中]返回等于(this.writerIndex-this.readerIndex)的可读字节数。
代码示例来源:origin: apache/nifi
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();
}
代码示例来源:origin: apache/nifi
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);
}
代码示例来源:origin: apache/incubator-gobblin
void verify(Bucket bucket)
throws UnsupportedEncodingException {
// verify
System.out.println("Starting verification procedure");
for (Map.Entry<String, byte[]> cacheEntry : verificationCache.entrySet()) {
Object doc = bucket.get(cacheEntry.getKey(), recordClass);
if (doc instanceof TupleDocument) {
ByteBuf returnedBuf = (((TupleDocument) doc).content()).value1();
byte[] returnedBytes = new byte[returnedBuf.readableBytes()];
returnedBuf.getBytes(0, returnedBytes);
Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for TupleDoc should be equal");
} else if (doc instanceof RawJsonDocument) {
byte[] returnedBytes = ((RawJsonDocument) doc).content().getBytes("UTF-8");
Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for JsonDoc should be equal");
} else {
Assert.fail("Returned type was neither TupleDocument nor RawJsonDocument");
}
}
System.out.println("Verification success!");
}
}
代码示例来源:origin: apache/incubator-gobblin
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");
}
}
代码示例来源:origin: apache/nifi
byteBuf.getBytes(byteBuf.readerIndex(), out, byteBuf.readableBytes());
byteBuf.release();
};
代码示例来源:origin: apache/incubator-gobblin
@Test
public void testBasicConvert() throws Exception {
Schema dataRecordSchema = SchemaBuilder.record("Data")
.fields()
.name("data").type().bytesType().noDefault()
.name("flags").type().intType().noDefault()
.endRecord();
Schema schema = SchemaBuilder.record("TestRecord")
.fields()
.name("key").type().stringType().noDefault()
.name("data").type(dataRecordSchema).noDefault()
.endRecord();
GenericData.Record testRecord = new GenericData.Record(schema);
String testContent = "hello world";
GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema);
dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8"))));
dataRecord.put("flags", 0);
testRecord.put("key", "hello");
testRecord.put("data", dataRecord);
Converter<Schema, String, GenericRecord, TupleDocument> recordConverter = new AvroToCouchbaseTupleConverter();
TupleDocument returnDoc = recordConverter.convertRecord("", testRecord, null).iterator().next();
byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()];
returnDoc.content().value1().readBytes(returnedBytes);
Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));
int returnedFlags = returnDoc.content().value2();
Assert.assertEquals(returnedFlags, 0);
}
代码示例来源:origin: apache/incubator-gobblin
TupleDocument returnDoc = writer.getBucket().get("hello", TupleDocument.class);
byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()];
returnDoc.content().value1().readBytes(returnedBytes);
Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));
代码示例来源:origin: com.couchbase.client/core-io
/**
* Creates a new stream which reads data from the specified {@code buffer}
* starting at the current {@code readerIndex} and ending at the current
* {@code writerIndex}.
* @param buffer The buffer which provides the content for this {@link InputStream}.
* @param releaseOnClose {@code true} means that when {@link #close()} is called then {@link ByteBuf#release()} will
* be called on {@code buffer}.
*/
public ByteBufInputStream(ByteBuf buffer, boolean releaseOnClose) {
this(buffer, buffer.readableBytes(), releaseOnClose);
}
代码示例来源:origin: com.couchbase.client/core-io
private static long total(Object msg) {
if (msg instanceof ByteBuf) {
return ((ByteBuf) msg).readableBytes();
}
if (msg instanceof FileRegion) {
return ((FileRegion) msg).count();
}
if (msg instanceof ByteBufHolder) {
return ((ByteBufHolder) msg).content().readableBytes();
}
return -1;
}
代码示例来源:origin: com.couchbase.client/core-io
public void setValue(String value, int rank) {
if (value == null) {
throw new NullPointerException("value");
}
ByteBuf buf = Unpooled.copiedBuffer(value, charset);
ByteBuf old = this.value.set(rank, buf);
if (old != null) {
size -= old.readableBytes();
old.release();
}
size += buf.readableBytes();
}
代码示例来源:origin: com.couchbase.client/core-io
@Override
public int readableBytes() {
if (terminated) {
return buffer.readableBytes();
} else {
return Integer.MAX_VALUE - buffer.readerIndex();
}
}
代码示例来源:origin: com.couchbase.client/core-io
private static long contentLength(Object msg) {
if (msg instanceof HttpContent) {
return ((HttpContent) msg).content().readableBytes();
}
if (msg instanceof ByteBuf) {
return ((ByteBuf) msg).readableBytes();
}
if (msg instanceof FileRegion) {
return ((FileRegion) msg).count();
}
throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
}
代码示例来源:origin: com.couchbase.client/core-io
@Override
public long skip(long bytes) throws IOException {
int readable = buffer.readableBytes();
if (readable < bytes) {
bytes = readable;
}
buffer.readerIndex((int) (buffer.readerIndex() + bytes));
return bytes;
}
代码示例来源:origin: com.couchbase.client/java-client
@Override
protected ByteArrayDocument doDecode(String id, ByteBuf content, long cas, int expiry, int flags,
ResponseStatus status) throws Exception {
if (!TranscoderUtils.hasBinaryFlags(flags)) {
throw new TranscodingException("Flags (0x" + Integer.toHexString(flags) + ") indicate non-binary " +
"document for id " + id + ", could not decode.");
}
byte[] data = new byte[content.readableBytes()];
content.readBytes(data);
return newDocument(id, expiry, data, cas);
}
代码示例来源:origin: com.couchbase.client/core-io
private void decodeNonJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) {
try {
in.skipBytes(unwrap(ctx, in, in.readerIndex(), in.readableBytes()));
} catch (Throwable cause) {
handleUnwrapThrowable(ctx, cause);
}
}
代码示例来源:origin: com.couchbase.client/core-io
private RawQueryResponse handleRawQueryResponse(boolean lastChunk, ChannelHandlerContext ctx) {
if (!lastChunk) {
return null;
}
ResponseStatus status = ResponseStatusConverter.fromHttp(responseHeader.getStatus().code());
ByteBuf responseCopy = ctx.alloc().buffer(responseContent.readableBytes(), responseContent.readableBytes());
responseCopy.writeBytes(responseContent);
return new RawQueryResponse(status, currentRequest(), responseCopy,
responseHeader.getStatus().code(),
responseHeader.getStatus().reasonPhrase());
}
代码示例来源:origin: com.couchbase.client/core-io
private RawAnalyticsResponse handleRawAnalyticsResponse(boolean lastChunk, ChannelHandlerContext ctx) {
if (!lastChunk) {
return null;
}
ResponseStatus status = ResponseStatusConverter.fromHttp(responseHeader.getStatus().code());
ByteBuf responseCopy = ctx.alloc().buffer(responseContent.readableBytes(), responseContent.readableBytes());
responseCopy.writeBytes(responseContent);
return new RawAnalyticsResponse(status, currentRequest(), responseCopy,
responseHeader.getStatus().code(),
responseHeader.getStatus().reasonPhrase());
}
代码示例来源:origin: com.couchbase.client/core-io
private RawQueryResponse handleRawQueryResponse(boolean lastChunk, ChannelHandlerContext ctx) {
if (!lastChunk) {
return null;
}
ResponseStatus status = ResponseStatusConverter.fromHttp(responseHeader.getStatus().code());
ByteBuf responseCopy = ctx.alloc().buffer(responseContent.readableBytes(), responseContent.readableBytes());
responseCopy.writeBytes(responseContent);
cleanupQueryStates();
return new RawQueryResponse(status, currentRequest(), responseCopy,
responseHeader.getStatus().code(),
responseHeader.getStatus().reasonPhrase());
}
代码示例来源:origin: com.couchbase.client/core-io
private static BinaryMemcacheRequest handleObserveSeqnoRequest(final ChannelHandlerContext ctx,
final ObserveSeqnoRequest msg) {
ByteBuf content = ctx.alloc().buffer();
content.writeLong(msg.vbucketUUID());
BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER, content);
request.setOpcode(OP_OBSERVE_SEQ);
request.setTotalBodyLength(content.readableBytes());
return request;
}
代码示例来源:origin: com.couchbase.client/core-io
private static BinaryMemcacheRequest handlePrependRequest(final PrependRequest msg) {
byte[] key = msg.keyBytes();
short keyLength = (short) key.length;
BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER, msg.content());
request.setOpcode(OP_PREPEND);
request.setKeyLength(keyLength);
request.setCAS(msg.cas());
request.setTotalBodyLength(keyLength + msg.content().readableBytes());
return request;
}
内容来源于网络,如有侵权,请联系作者删除!