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); }
/** * 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); }
/** * Returns {@code CR ('\r')} and {@code LF ('\n')} delimiters, which could * be used for text-based line protocols. */ public static ByteBuf[] lineDelimiter() { return new ByteBuf[] { Unpooled.wrappedBuffer(new byte[] { '\r', '\n' }), Unpooled.wrappedBuffer(new byte[] { '\n' }), }; }
/** * 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 big-endian composite buffer which wraps the readable bytes of the * specified buffers without copying them. A modification on the content * of the specified buffers will be visible to the returned buffer. * @param buffers The buffers to wrap. Reference count ownership of all variables is transfered to this method. * @return The readable portion of the {@code buffers}. The caller is responsible for releasing this buffer. */ public static ByteBuf wrappedBuffer(ByteBuf... buffers) { return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, buffers); }
/** * Creates a new big-endian composite buffer which wraps the slices of the specified * NIO buffers without copying them. A modification on the content of the * specified buffers will be visible to the returned buffer. */ public static ByteBuf wrappedBuffer(ByteBuffer... buffers) { return wrappedBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS, buffers); }
@Override protected void encode(ChannelHandlerContext ctx, byte[] msg, List<Object> out) throws Exception { out.add(Unpooled.wrappedBuffer(msg)); } }
/** * Creates a {@link PemX509Certificate} from raw {@code byte[]}. * * ATTENTION: It's assumed that the given argument is a PEM/PKCS#8 encoded value. * No input validation is performed to validate it. */ public static PemX509Certificate valueOf(byte[] key) { return valueOf(Unpooled.wrappedBuffer(key)); }
/** * Creates a {@link PemPrivateKey} from raw {@code byte[]}. * * ATTENTION: It's assumed that the given argument is a PEM/PKCS#8 encoded value. * No input validation is performed to validate it. */ public static PemPrivateKey valueOf(byte[] key) { return valueOf(Unpooled.wrappedBuffer(key)); }
@Override public ByteBuf getByteBuf() throws IOException { if (file == null) { return EMPTY_BUFFER; } byte[] array = readFrom(file); return wrappedBuffer(array); }
/** * Creates a new big-endian buffer whose content is a copy of the * specified {@code array}. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0} and {@code array.length} respectively. */ public static ByteBuf copiedBuffer(byte[] array) { if (array.length == 0) { return EMPTY_BUFFER; } return wrappedBuffer(array.clone()); }
@Override protected void encode( ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception { if (msg instanceof MessageLite) { out.add(wrappedBuffer(((MessageLite) msg).toByteArray())); return; } if (msg instanceof MessageLite.Builder) { out.add(wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray())); } } }
@Override public void setValue(String value) throws IOException { if (value == null) { throw new NullPointerException("value"); } byte [] bytes = value.getBytes(charset.name()); ByteBuf buffer = wrappedBuffer(bytes); if (definedSize > 0) { definedSize = buffer.readableBytes(); } setContent(buffer); }
@Override protected void encode( ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception { if (msg instanceof MessageLite) { out.add(wrappedBuffer(((MessageLite) msg).toByteArray())); return; } if (msg instanceof MessageLite.Builder) { out.add(wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray())); } } }
@Override public void setValue(String value) throws IOException { if (value == null) { throw new NullPointerException("value"); } byte [] bytes = value.getBytes(charset.name()); ByteBuf buffer = wrappedBuffer(bytes); if (definedSize > 0) { definedSize = buffer.readableBytes(); } setContent(buffer); }
@Override public void setValue(String value) throws IOException { if (value == null) { throw new NullPointerException("value"); } byte [] bytes = value.getBytes(charset.name()); ByteBuf buffer = wrappedBuffer(bytes); if (definedSize > 0) { definedSize = buffer.readableBytes(); } setContent(buffer); }
@Override public void setValue(String value) throws IOException { if (value == null) { throw new NullPointerException("value"); } byte [] bytes = value.getBytes(charset.name()); ByteBuf buffer = wrappedBuffer(bytes); if (definedSize > 0) { definedSize = buffer.readableBytes(); } setContent(buffer); }
@Override protected <T> ByteBuf doEncodeSingle(T value, String transcodingErrorMessage) throws TranscodingException { try { return Unpooled.wrappedBuffer(writeValueAsBytes(value)); } catch (Exception e) { throw new TranscodingException(transcodingErrorMessage, e); } }
@Override protected Tuple2<ByteBuf, Integer> doEncode(ByteArrayDocument document) throws Exception { return Tuple.create( Unpooled.wrappedBuffer(document.content()), TranscoderUtils.BINARY_COMPAT_FLAGS ); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes())); ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.fireExceptionCaught(cause); ctx.close(); } }