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); }
@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); }
returnDoc.content().value1().readBytes(returnedBytes); Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));
@Override public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { buf.readBytes(dst, dstIndex, length); return this; }
@Override public ByteBuf readBytes(byte[] dst) { buf.readBytes(dst); return this; }
@Override public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { buf.readBytes(dst, dstIndex, length); return this; }
@Override public ByteBuf readBytes(ByteBuf dst) { buf.readBytes(dst); return this; }
@Override public ByteBuf readBytes(ByteBuf dst, int length) { buf.readBytes(dst, length); return this; }
@Override public ByteBuf readBytes(OutputStream out, int length) throws IOException { buf.readBytes(out, length); return this; }
@Override public ByteBuf readBytes(ByteBuf dst) { buf.readBytes(dst); return this; }
@Override public ByteBuf readBytes(ByteBuf dst, int length) { buf.readBytes(dst, length); return this; }
@Override public ByteBuf readBytes(OutputStream out, int length) throws IOException { buf.readBytes(out, length); return this; }
@Override public ByteBuf readBytes(byte[] dst) { checkReadableBytes(dst.length); buffer.readBytes(dst); return this; }
@Override public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { checkReadableBytes(length); buffer.readBytes(dst, dstIndex, length); return this; }
@Override public int read(byte[] dst, int dstIndex, int length) throws IOException { int available = available(); if (available == 0) { return -1; } length = Math.min(available, length); buffer.readBytes(dst, dstIndex, length); return length; }
@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); return buffer.readBytes(length); }
@Override public void readFully(byte[] b, int off, int len) throws IOException { checkAvailable(len); buffer.readBytes(b, off, len); }
@Override protected void doWriteBytes(ByteBuf buf) throws Exception { OutputStream os = this.os; if (os == null) { throw new NotYetConnectedException(); } buf.readBytes(os, buf.readableBytes()); }
@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); }