diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
index de5819c69a..6993ff5beb 100644
--- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
+++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
@@ -57,6 +57,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;
@@ -335,6 +337,21 @@ public final class ByteBufUtil {
return buffer.forEachByteDesc(toIndex, fromIndex - toIndex, new 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}.
@@ -342,16 +359,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) {
@@ -418,6 +427,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}.
@@ -425,12 +449,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);