/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it to a {@link ByteBuf} allocated with {@code alloc}. * @param alloc The allocator used to allocate a new {@link ByteBuf}. * @param seq The characters to write into a buffer. * @return The {@link ByteBuf} which contains the <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoded * result. */ public static ByteBuf writeUtf8(ByteBufAllocator alloc, CharSequence seq) { // UTF-8 uses max. 3 bytes per char, so calculate the worst case. ByteBuf buf = alloc.buffer(utf8MaxBytes(seq)); writeUtf8(buf, seq); return buf; }
/** * Creates a buffer containing the error message from the given exception. If the cause is * {@code null} returns an empty buffer. */ public static ByteBuf toByteBuf(ChannelHandlerContext ctx, Throwable cause) { if (cause == null || cause.getMessage() == null) { return Unpooled.EMPTY_BUFFER; } return ByteBufUtil.writeUtf8(ctx.alloc(), cause.getMessage()); }
@Override public DataBuffer write(CharSequence charSequence, Charset charset) { Assert.notNull(charSequence, "CharSequence must not be null"); Assert.notNull(charset, "Charset must not be null"); if (StandardCharsets.UTF_8.equals(charset)) { ByteBufUtil.writeUtf8(this.byteBuf, charSequence); } else if (StandardCharsets.US_ASCII.equals(charset)) { ByteBufUtil.writeAscii(this.byteBuf, charSequence); } else { return PooledDataBuffer.super.write(charSequence, charset); } return this; }
/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it to a {@link ByteBuf} allocated with {@code alloc}. * @param alloc The allocator used to allocate a new {@link ByteBuf}. * @param seq The characters to write into a buffer. * @return The {@link ByteBuf} which contains the <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoded * result. */ public static ByteBuf writeUtf8(ByteBufAllocator alloc, CharSequence seq) { // UTF-8 uses max. 3 bytes per char, so calculate the worst case. ByteBuf buf = alloc.buffer(utf8MaxBytes(seq)); writeUtf8(buf, seq); return buf; }
private ByteBuf encode(Object in) { if (in instanceof byte[]) { return Unpooled.wrappedBuffer((byte[])in); } if (in instanceof ByteBuf) { return (ByteBuf) in; } if (in instanceof ChannelName) { return Unpooled.wrappedBuffer(((ChannelName)in).getName()); } String payload = in.toString(); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(ByteBufUtil.utf8MaxBytes(payload)); ByteBufUtil.writeUtf8(buf, payload); return buf; }
private ByteBuf encode(Object in) { if (in instanceof byte[]) { return Unpooled.wrappedBuffer((byte[])in); } if (in instanceof ByteBuf) { return (ByteBuf) in; } if (in instanceof ChannelName) { return Unpooled.wrappedBuffer(((ChannelName)in).getName()); } String payload = in.toString(); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(ByteBufUtil.utf8MaxBytes(payload)); ByteBufUtil.writeUtf8(buf, payload); return buf; }
/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it to a {@link ByteBuf} allocated with {@code alloc}. * @param alloc The allocator used to allocate a new {@link ByteBuf}. * @param seq The characters to write into a buffer. * @return The {@link ByteBuf} which contains the <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoded * result. */ public static ByteBuf writeUtf8(ByteBufAllocator alloc, CharSequence seq) { // UTF-8 uses max. 3 bytes per char, so calculate the worst case. ByteBuf buf = alloc.buffer(utf8MaxBytes(seq)); writeUtf8(buf, seq); return buf; }
@Override public DataBuffer write(CharSequence charSequence, Charset charset) { Assert.notNull(charSequence, "CharSequence must not be null"); Assert.notNull(charset, "Charset must not be null"); if (StandardCharsets.UTF_8.equals(charset)) { ByteBufUtil.writeUtf8(this.byteBuf, charSequence); } else if (StandardCharsets.US_ASCII.equals(charset)) { ByteBufUtil.writeAscii(this.byteBuf, charSequence); } else { return PooledDataBuffer.super.write(charSequence, charset); } return this; }
static void writeString(ByteBuf buffer, String value) { final ByteBuf buf = buffer.alloc().buffer(ByteBufUtil.utf8MaxBytes(value)); try { final int length = ByteBufUtil.writeUtf8(buf, value); buffer.writeShort(length); buffer.writeBytes(buf); } finally { buf.release(); } }
/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it into {@code reserveBytes} of a {@link ByteBuf}. * <p> * The {@code reserveBytes} must be computed (ie eagerly using {@link #utf8MaxBytes(CharSequence)} * or exactly with {@link #utf8Bytes(CharSequence)}) to ensure this method to not fail: for performance reasons * the index checks will be performed using just {@code reserveBytes}.<br> * This method returns the actual number of bytes written. */ public static int reserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int reserveBytes) { for (;;) { if (buf instanceof WrappedCompositeByteBuf) { // WrappedCompositeByteBuf is a sub-class of AbstractByteBuf so it needs special handling. buf = buf.unwrap(); } else if (buf instanceof AbstractByteBuf) { AbstractByteBuf byteBuf = (AbstractByteBuf) buf; byteBuf.ensureWritable0(reserveBytes); int written = writeUtf8(byteBuf, byteBuf.writerIndex, seq, seq.length()); byteBuf.writerIndex += written; return written; } else if (buf instanceof WrappedByteBuf) { // Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path. buf = buf.unwrap(); } else { byte[] bytes = seq.toString().getBytes(CharsetUtil.UTF_8); buf.writeBytes(bytes); return bytes.length; } } }
/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it into {@code reserveBytes} of a {@link ByteBuf}. * <p> * The {@code reserveBytes} must be computed (ie eagerly using {@link #utf8MaxBytes(CharSequence)} * or exactly with {@link #utf8Bytes(CharSequence)}) to ensure this method to not fail: for performance reasons * the index checks will be performed using just {@code reserveBytes}.<br> * This method returns the actual number of bytes written. */ public static int reserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int reserveBytes) { for (;;) { if (buf instanceof WrappedCompositeByteBuf) { // WrappedCompositeByteBuf is a sub-class of AbstractByteBuf so it needs special handling. buf = buf.unwrap(); } else if (buf instanceof AbstractByteBuf) { AbstractByteBuf byteBuf = (AbstractByteBuf) buf; byteBuf.ensureWritable0(reserveBytes); int written = writeUtf8(byteBuf, byteBuf.writerIndex, seq, seq.length()); byteBuf.writerIndex += written; return written; } else if (buf instanceof WrappedByteBuf) { // Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path. buf = buf.unwrap(); } else { byte[] bytes = seq.toString().getBytes(CharsetUtil.UTF_8); buf.writeBytes(bytes); return bytes.length; } } }
ByteBufUtil.writeUtf8(target, str); return;
private static void writeString(ByteBufAllocator allocator, RedisMessageType type, String content, List<Object> out) { ByteBuf buf = allocator.ioBuffer(type.length() + ByteBufUtil.utf8MaxBytes(content) + RedisConstants.EOL_LENGTH); type.writeTo(buf); ByteBufUtil.writeUtf8(buf, content); buf.writeShort(RedisConstants.EOL_SHORT); out.add(buf); }
private int setCharSequence0(int index, CharSequence sequence, Charset charset, boolean expand) { if (charset.equals(CharsetUtil.UTF_8)) { int length = ByteBufUtil.utf8MaxBytes(sequence); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeUtf8(this, index, sequence, sequence.length()); } if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) { int length = sequence.length(); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeAscii(this, index, sequence, length); } byte[] bytes = sequence.toString().getBytes(charset); if (expand) { ensureWritable0(bytes.length); // setBytes(...) will take care of checking the indices. } setBytes(index, bytes); return bytes.length; }
/** * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * it into {@code reserveBytes} of a {@link ByteBuf}. * <p> * The {@code reserveBytes} must be computed (ie eagerly using {@link #utf8MaxBytes(CharSequence)} * or exactly with {@link #utf8Bytes(CharSequence)}) to ensure this method to not fail: for performance reasons * the index checks will be performed using just {@code reserveBytes}.<br> * This method returns the actual number of bytes written. */ public static int reserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int reserveBytes) { for (;;) { if (buf instanceof WrappedCompositeByteBuf) { // WrappedCompositeByteBuf is a sub-class of AbstractByteBuf so it needs special handling. buf = buf.unwrap(); } else if (buf instanceof AbstractByteBuf) { AbstractByteBuf byteBuf = (AbstractByteBuf) buf; byteBuf.ensureWritable0(reserveBytes); int written = writeUtf8(byteBuf, byteBuf.writerIndex, seq, seq.length()); byteBuf.writerIndex += written; return written; } else if (buf instanceof WrappedByteBuf) { // Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path. buf = buf.unwrap(); } else { byte[] bytes = seq.toString().getBytes(CharsetUtil.UTF_8); buf.writeBytes(bytes); return bytes.length; } } }
private int setCharSequence0(int index, CharSequence sequence, Charset charset, boolean expand) { if (charset.equals(CharsetUtil.UTF_8)) { int length = ByteBufUtil.utf8MaxBytes(sequence); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeUtf8(this, index, sequence, sequence.length()); } if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) { int length = sequence.length(); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeAscii(this, index, sequence, length); } byte[] bytes = sequence.toString().getBytes(charset); if (expand) { ensureWritable0(bytes.length); // setBytes(...) will take care of checking the indices. } setBytes(index, bytes); return bytes.length; }
private int setCharSequence0(int index, CharSequence sequence, Charset charset, boolean expand) { if (charset.equals(CharsetUtil.UTF_8)) { int length = ByteBufUtil.utf8MaxBytes(sequence); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeUtf8(this, index, sequence, sequence.length()); } if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) { int length = sequence.length(); if (expand) { ensureWritable0(length); checkIndex0(index, length); } else { checkIndex(index, length); } return ByteBufUtil.writeAscii(this, index, sequence, length); } byte[] bytes = sequence.toString().getBytes(charset); if (expand) { ensureWritable0(bytes.length); // setBytes(...) will take care of checking the indices. } setBytes(index, bytes); return bytes.length; }
@Test public void shouldNotLogExceptionsWhenEvenLoopIsShuttingDown() throws Exception { AssertableLogProvider logProvider = new AssertableLogProvider(); BoltConnection connection = mock( BoltConnection.class ); HouseKeeper houseKeeper = new HouseKeeper( connection, logProvider.getLog( HouseKeeper.class ) ); Bootstrap bootstrap = newBootstrap( houseKeeper ); try ( ServerSocket serverSocket = new ServerSocket( 0 ) ) { ChannelFuture future = bootstrap.connect( "localhost", serverSocket.getLocalPort() ).sync(); Channel channel = future.channel(); // write some messages without flushing for ( int i = 0; i < 100; i++ ) { // use void promise which should redirect all write errors back to the pipeline and the HouseKeeper channel.write( writeUtf8( channel.alloc(), "Hello" ), channel.voidPromise() ); } // stop the even loop to make all pending writes fail bootstrap.config().group().shutdownGracefully(); // await for the channel to be closed by the HouseKeeper channel.closeFuture().sync(); } finally { // make sure event loop group is always terminated bootstrap.config().group().shutdownGracefully().sync(); } logProvider.assertNoLoggingOccurred(); }
/** * Static factory method for a text payload. Mainly looks better than "new ByteBufPayload(data, * metadata)" * * @param data the data of the payload. * @param metadata the metadata for the payload. * @return a payload. */ public static Payload create(String data, @Nullable String metadata) { return create( ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, data), metadata == null ? null : ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, metadata)); }
/** * Static factory method for a text payload. Mainly looks better than "new ByteBufPayload(data)" * * @param data the data of the payload. * @return a payload. */ public static Payload create(String data) { return create(ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, data), null); }