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(); }
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); }
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!"); } }
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"); } }
byteBuf.getBytes(byteBuf.readerIndex(), out, byteBuf.readableBytes()); byteBuf.release(); };
@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); }
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")));
/** * 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); }
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; }
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(); }
@Override public int readableBytes() { if (terminated) { return buffer.readableBytes(); } else { return Integer.MAX_VALUE - buffer.readerIndex(); } }
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)); }
@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; }
@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); }
private void decodeNonJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) { try { in.skipBytes(unwrap(ctx, in, in.readerIndex(), in.readableBytes())); } catch (Throwable cause) { handleUnwrapThrowable(ctx, cause); } }
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()); }
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()); }
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()); }
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; }
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; }