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.
This commit is contained in:
Scott Mitchell 2016-02-26 09:27:49 -08:00 committed by Norman Maurer
parent 57cebc7f5b
commit 770e74a01c

View File

@ -57,6 +57,8 @@ public final class ByteBufUtil {
private static final byte WRITE_UTF_UNKNOWN = (byte) '?'; private static final byte WRITE_UTF_UNKNOWN = (byte) '?';
private static final int MAX_CHAR_BUFFER_SIZE; private static final int MAX_CHAR_BUFFER_SIZE;
private static final int THREAD_LOCAL_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; static final ByteBufAllocator DEFAULT_ALLOCATOR;
@ -335,6 +337,21 @@ public final class ByteBufUtil {
return buffer.forEachByteDesc(toIndex, fromIndex - toIndex, new IndexOfProcessor(value)); return buffer.forEachByteDesc(toIndex, fromIndex - toIndex, new IndexOfProcessor(value));
} }
/**
* 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(seq.length() * MAX_BYTES_PER_CHAR_UTF8);
writeUtf8(buf, seq);
return buf;
}
/** /**
* Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write
* it to a {@link ByteBuf}. * it to a {@link ByteBuf}.
@ -342,16 +359,8 @@ public final class ByteBufUtil {
* This method returns the actual number of bytes written. * This method returns the actual number of bytes written.
*/ */
public static int writeUtf8(ByteBuf buf, CharSequence seq) { 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 len = seq.length();
final int maxSize = len * 3; buf.ensureWritable(len * MAX_BYTES_PER_CHAR_UTF8);
buf.ensureWritable(maxSize);
for (;;) { for (;;) {
if (buf instanceof AbstractByteBuf) { if (buf instanceof AbstractByteBuf) {
@ -418,6 +427,21 @@ public final class ByteBufUtil {
return writerIndex - oldWriterIndex; return writerIndex - oldWriterIndex;
} }
/**
* Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</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/ASCII">ASCII</a> 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 <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> and write it * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> and write it
* to a {@link ByteBuf}. * to a {@link ByteBuf}.
@ -425,12 +449,6 @@ public final class ByteBufUtil {
* This method returns the actual number of bytes written. * This method returns the actual number of bytes written.
*/ */
public static int writeAscii(ByteBuf buf, CharSequence seq) { 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 // ASCII uses 1 byte per char
final int len = seq.length(); final int len = seq.length();
buf.ensureWritable(len); buf.ensureWritable(len);