diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index d5a51f4b04..0bcde1330c 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -60,6 +60,8 @@ public final class ByteBufUtil { private static final byte WRITE_UTF_UNKNOWN = (byte) '?'; private static final int MAX_CHAR_BUFFER_SIZE; private static final int THREAD_LOCAL_BUFFER_SIZE; + private static final int MAX_BYTES_PER_CHAR_UTF8 = + (int) CharsetUtil.getEncoder(CharsetUtil.UTF_8).maxBytesPerChar(); static final ByteBufAllocator DEFAULT_ALLOCATOR; @@ -353,6 +355,21 @@ public final class ByteBufUtil { return buffer.forEachByteDesc(toIndex, fromIndex - toIndex, new ByteProcessor.IndexOfProcessor(value)); } + /** + * Encode a {@link CharSequence} in UTF-8 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 UTF-8 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(seq.length() * MAX_BYTES_PER_CHAR_UTF8); + writeUtf8(buf, seq); + return buf; + } + /** * Encode a {@link CharSequence} in UTF-8 and write * it to a {@link ByteBuf}. @@ -360,16 +377,8 @@ public final class ByteBufUtil { * This method returns the actual number of bytes written. */ public static int writeUtf8(ByteBuf buf, CharSequence seq) { - if (buf == null) { - throw new NullPointerException("buf"); - } - if (seq == null) { - throw new NullPointerException("seq"); - } - // UTF-8 uses max. 3 bytes per char, so calculate the worst case. final int len = seq.length(); - final int maxSize = len * 3; - buf.ensureWritable(maxSize); + buf.ensureWritable(len * MAX_BYTES_PER_CHAR_UTF8); for (;;) { if (buf instanceof AbstractByteBuf) { @@ -436,6 +445,21 @@ public final class ByteBufUtil { return writerIndex - oldWriterIndex; } + /** + * Encode a {@link CharSequence} in ASCII 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 ASCII encoded + * result. + */ + public static ByteBuf writeAscii(ByteBufAllocator alloc, CharSequence seq) { + // ASCII uses 1 byte per char + ByteBuf buf = alloc.buffer(seq.length()); + writeAscii(buf, seq); + return buf; + } + /** * Encode a {@link CharSequence} in ASCII and write it * to a {@link ByteBuf}. @@ -443,12 +467,6 @@ public final class ByteBufUtil { * This method returns the actual number of bytes written. */ public static int writeAscii(ByteBuf buf, CharSequence seq) { - if (buf == null) { - throw new NullPointerException("buf"); - } - if (seq == null) { - throw new NullPointerException("seq"); - } // ASCII uses 1 byte per char final int len = seq.length(); buf.ensureWritable(len);