private <V> Document toDocument(String docId, V value, Serializer<V> valueSerializer, long revision) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); valueSerializer.serialize(value, bos); final ByteBuf byteBuf = Unpooled.wrappedBuffer(bos.toByteArray()); return BinaryDocument.create(docId, byteBuf, revision); }
@Override public Iterable<TupleDocument> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { String key = inputRecord.get(keyField).toString(); GenericRecord data = (GenericRecord) inputRecord.get(dataRecordField); ByteBuffer dataBytes = (ByteBuffer) data.get(valueField); Integer flags = (Integer) data.get(flagsField); ByteBuf buffer = Unpooled.copiedBuffer(dataBytes); return new SingleRecordIterable<>(new TupleDocument(key, Tuple.create(buffer, flags))); } }
/** * Create a new {@link DefaultBinaryMemcacheRequest} with the header, key and extras. * * @param key the key to use. * @param extras the extras to use. */ public DefaultFullBinaryMemcacheRequest(byte[] key, ByteBuf extras) { this(key, extras, Unpooled.buffer(0)); }
/** * Helper method which performs decompression for snappy compressed values. */ private void handleSnappyDecompression(final ChannelHandlerContext ctx, final FullBinaryMemcacheResponse response) { ByteBuf decompressed; if (response.content().readableBytes() > 0) { // we need to copy it on-heap... byte[] compressed = Unpooled.copiedBuffer(response.content()).array(); try { decompressed = Unpooled.wrappedBuffer(Snappy.uncompress(compressed, 0, compressed.length)); } catch (Exception ex) { throw new RuntimeException("Could not decode snappy-compressed value.", ex); } } else { decompressed = Unpooled.buffer(0); } response.content().release(); response.setContent(decompressed); response.setTotalBodyLength( response.getExtrasLength() + response.getKeyLength() + decompressed.readableBytes() ); response.setDataType((byte) (response.getDataType() & ~DATATYPE_SNAPPY)); }
private static ByteBuf encode(List<LookupCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator? for (LookupCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator? commandBuf.writeByte(command.opCode()); //flags if (command.xattr()) { commandBuf.writeByte(SUBDOC_FLAG_XATTR_PATH); } else { commandBuf.writeByte(0); } commandBuf.writeShort(pathLength); //no value length commandBuf.writeBytes(pathBytes); compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
/** * Returns a new big-endian composite buffer with no components. */ public static CompositeByteBuf compositeBuffer() { return compositeBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS); }
@Override public ByteBuf slice(int index, int length) { return Unpooled.unmodifiableBuffer(unwrap().slice(index, length)); }
buffer = ByteBufUtil.threadLocalDirectBuffer(); if (buffer == null) { buffer = Unpooled.directBuffer(readableBytes);
/** * Creates a new close frame with no losing getStatus code and no reason text * * @param finalFragment * flag indicating if this frame is the final fragment * @param rsv * reserved bits used for protocol extensions */ public CloseWebSocketFrame(boolean finalFragment, int rsv) { this(finalFragment, rsv, Unpooled.buffer(0)); }
private static ByteBuf encode(List<LookupCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator? for (LookupCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator? commandBuf.writeByte(command.opCode()); //flags if (command.xattr()) { commandBuf.writeByte(SUBDOC_FLAG_XATTR_PATH); } else { commandBuf.writeByte(0); } commandBuf.writeShort(pathLength); //no value length commandBuf.writeBytes(pathBytes); compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
/** * Returns a new big-endian composite buffer with no components. */ public static CompositeByteBuf compositeBuffer() { return compositeBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS); }
@Override public ByteBuf slice(int index, int length) { return Unpooled.unmodifiableBuffer(unwrap().slice(index, length)); }
buffer = ByteBufUtil.threadLocalDirectBuffer(); if (buffer == null) { buffer = Unpooled.directBuffer(readableBytes);
/** * Creates a new big-endian composite buffer which wraps the specified * arrays without copying them. A modification on the specified arrays' * content will be visible to the returned buffer. */ public static ByteBuf wrappedBuffer(byte[]... arrays) { return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, arrays); }
/** * Creates a new empty pong frame. */ public PongWebSocketFrame() { super(Unpooled.buffer(0)); }
private static ByteBuf deltaToFragment(long delta) { String sDelta = "" + delta; return Unpooled.copiedBuffer(sDelta, CharsetUtil.UTF_8); }
private static ByteBuf encode(List<MutationCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); for (MutationCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes()); commandBuf.writeByte(command.opCode()); byte subdocFlags = 0;
@Override public void addContent(ByteBuf buffer, boolean last) throws IOException { if (buffer != null) { long localsize = buffer.readableBytes(); if (definedSize > 0 && definedSize < size + localsize) { throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize); } size += localsize; if (byteBuf == null) { byteBuf = buffer; } else if (byteBuf instanceof CompositeByteBuf) { CompositeByteBuf cbb = (CompositeByteBuf) byteBuf; cbb.addComponent(true, buffer); } else { CompositeByteBuf cbb = compositeBuffer(Integer.MAX_VALUE); cbb.addComponents(true, byteBuf, buffer); byteBuf = cbb; } } if (last) { completed = true; } else { if (buffer == null) { throw new NullPointerException("buffer"); } } }
/** * Returns a {@code NUL (0x00)} delimiter, which could be used for * Flash XML socket or any similar protocols. */ public static ByteBuf[] nulDelimiter() { return new ByteBuf[] { Unpooled.wrappedBuffer(new byte[] { 0 }) }; }
/** * Creates a new instance. * * @param streamId the Stream-ID of this frame */ public DefaultSpdyDataFrame(int streamId) { this(streamId, Unpooled.buffer(0)); }