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:
parent
4a5e484c5a
commit
94f27be59b
@ -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 <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
|
||||
* 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 <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
|
||||
* 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);
|
||||
|
Loading…
Reference in New Issue
Block a user