From 94f27be59b637c74763cc3b9fd27cf628b59f7bd Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 26 Feb 2016 09:27:49 -0800 Subject: [PATCH] ByteBufUtil writeUtf8 and writeAscii helper methods Motivation: Utility methods in ByteBufUtil to writeUtf8 and writeAscii expect a buffer to already be allocated. If the user does not have a buffer allocated they have to know details of the encoding in order to know the size of the buffer to allocate. Modifications: - Add writeUtf8 and writeAscii which take a ByteBufAllocator and allocate a ByteBuf of the correct size for the user Result: ByteBufUtil methods which are easier to use if the user doesn't already have a ByteBuf. --- .../java/io/netty/buffer/ByteBufUtil.java | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) 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);