public final class ByteBufUtil
extends java.lang.Object
ByteBuf
,
such as the generation of hex dump and swapping an integer's byte order.Modifier and Type | Class and Description |
---|---|
private static class |
ByteBufUtil.HexUtil |
private static class |
ByteBufUtil.IndexOfProcessor |
(package private) static class |
ByteBufUtil.ThreadLocalDirectByteBuf |
(package private) static class |
ByteBufUtil.ThreadLocalUnsafeDirectByteBuf |
Modifier and Type | Field and Description |
---|---|
private static FastThreadLocal<java.nio.CharBuffer> |
CHAR_BUFFERS |
(package private) static ByteBufAllocator |
DEFAULT_ALLOCATOR |
private static ByteBufProcessor |
FIND_NON_ASCII
Aborts on a byte which is not a valid ASCII character.
|
private static InternalLogger |
logger |
private static int |
MAX_BYTES_PER_CHAR_UTF8 |
private static int |
MAX_CHAR_BUFFER_SIZE |
private static int |
THREAD_LOCAL_BUFFER_SIZE |
private static byte |
WRITE_UTF_UNKNOWN |
Modifier | Constructor and Description |
---|---|
private |
ByteBufUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
appendPrettyHexDump(java.lang.StringBuilder dump,
ByteBuf buf)
Appends the prettified multi-line hexadecimal dump of the specified
ByteBuf to the specified
StringBuilder that is easy to read by humans. |
static void |
appendPrettyHexDump(java.lang.StringBuilder dump,
ByteBuf buf,
int offset,
int length)
Appends the prettified multi-line hexadecimal dump of the specified
ByteBuf to the specified
StringBuilder that is easy to read by humans, starting at the given offset using
the given length . |
static int |
compare(ByteBuf bufferA,
ByteBuf bufferB)
Compares the two specified buffers as described in
ByteBuf.compareTo(ByteBuf) . |
private static long |
compareUintBigEndian(ByteBuf bufferA,
ByteBuf bufferB,
int aIndex,
int bIndex,
int uintCountIncrement) |
private static long |
compareUintBigEndianA(ByteBuf bufferA,
ByteBuf bufferB,
int aIndex,
int bIndex,
int uintCountIncrement) |
private static long |
compareUintBigEndianB(ByteBuf bufferA,
ByteBuf bufferB,
int aIndex,
int bIndex,
int uintCountIncrement) |
private static long |
compareUintLittleEndian(ByteBuf bufferA,
ByteBuf bufferB,
int aIndex,
int bIndex,
int uintCountIncrement) |
(package private) static java.lang.String |
decodeString(ByteBuf src,
int readerIndex,
int len,
java.nio.charset.Charset charset) |
private static void |
decodeString(java.nio.charset.CharsetDecoder decoder,
java.nio.ByteBuffer src,
java.nio.CharBuffer dst) |
static ByteBuf |
encodeString(ByteBufAllocator alloc,
java.nio.CharBuffer src,
java.nio.charset.Charset charset)
Encode the given
CharBuffer using the given Charset into a new ByteBuf which
is allocated via the ByteBufAllocator . |
(package private) static ByteBuf |
encodeString0(ByteBufAllocator alloc,
boolean enforceHeap,
java.nio.CharBuffer src,
java.nio.charset.Charset charset) |
static boolean |
equals(ByteBuf bufferA,
ByteBuf bufferB)
Returns
true if and only if the two specified buffers are
identical to each other as described in ChannelBuffer#equals(Object) . |
private static int |
firstIndexOf(ByteBuf buffer,
int fromIndex,
int toIndex,
byte value) |
static int |
hashCode(ByteBuf buffer)
Calculates the hash code of the specified buffer.
|
static java.lang.String |
hexDump(byte[] array)
Returns a hex dump
of the specified byte array.
|
static java.lang.String |
hexDump(byte[] array,
int fromIndex,
int length)
Returns a hex dump
of the specified byte array's sub-region.
|
static java.lang.String |
hexDump(ByteBuf buffer)
Returns a hex dump
of the specified buffer's readable bytes.
|
static java.lang.String |
hexDump(ByteBuf buffer,
int fromIndex,
int length)
Returns a hex dump
of the specified buffer's sub-region.
|
static int |
indexOf(ByteBuf buffer,
int fromIndex,
int toIndex,
byte value)
The default implementation of
ByteBuf.indexOf(int, int, byte) . |
private static boolean |
isAscii(ByteBuf buf,
int index,
int length)
Returns
true if the specified ByteBuf starting at index with length is valid
ASCII text, otherwise return false . |
static boolean |
isText(ByteBuf buf,
java.nio.charset.Charset charset)
|
static boolean |
isText(ByteBuf buf,
int index,
int length,
java.nio.charset.Charset charset)
Returns
true if the specified ByteBuf starting at index with length is valid
text using the given Charset , otherwise return false . |
private static boolean |
isUtf8(ByteBuf buf,
int index,
int length)
Returns
true if the specified ByteBuf starting at index with length is valid
UTF8 text, otherwise return false . |
private static int |
lastIndexOf(ByteBuf buffer,
int fromIndex,
int toIndex,
byte value) |
static java.lang.String |
prettyHexDump(ByteBuf buffer)
Returns a multi-line hexadecimal dump of the specified
ByteBuf that is easy to read by humans. |
static java.lang.String |
prettyHexDump(ByteBuf buffer,
int offset,
int length)
Returns a multi-line hexadecimal dump of the specified
ByteBuf that is easy to read by humans,
starting at the given offset using the given length . |
static ByteBuf |
readBytes(ByteBufAllocator alloc,
ByteBuf buffer,
int length)
Read the given amount of bytes into a new
ByteBuf that is allocated from the ByteBufAllocator . |
static int |
swapInt(int value)
Toggles the endianness of the specified 32-bit integer.
|
static long |
swapLong(long value)
Toggles the endianness of the specified 64-bit long integer.
|
static int |
swapMedium(int value)
Toggles the endianness of the specified 24-bit medium integer.
|
static short |
swapShort(short value)
Toggles the endianness of the specified 16-bit short integer.
|
static ByteBuf |
threadLocalDirectBuffer()
Returns a cached thread-local direct buffer, if available.
|
private static void |
writeAscii(AbstractByteBuf buffer,
java.lang.CharSequence seq,
int len) |
static ByteBuf |
writeAscii(ByteBufAllocator alloc,
java.lang.CharSequence seq)
|
static int |
writeAscii(ByteBuf buf,
java.lang.CharSequence seq)
|
private static int |
writeUtf8(AbstractByteBuf buffer,
java.lang.CharSequence seq,
int len) |
static ByteBuf |
writeUtf8(ByteBufAllocator alloc,
java.lang.CharSequence seq)
|
static int |
writeUtf8(ByteBuf buf,
java.lang.CharSequence seq)
|
private static final InternalLogger logger
private static final FastThreadLocal<java.nio.CharBuffer> CHAR_BUFFERS
private static final byte WRITE_UTF_UNKNOWN
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
static final ByteBufAllocator DEFAULT_ALLOCATOR
private static final ByteBufProcessor FIND_NON_ASCII
public static java.lang.String hexDump(ByteBuf buffer)
public static java.lang.String hexDump(ByteBuf buffer, int fromIndex, int length)
public static java.lang.String hexDump(byte[] array)
public static java.lang.String hexDump(byte[] array, int fromIndex, int length)
public static int hashCode(ByteBuf buffer)
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB)
true
if and only if the two specified buffers are
identical to each other as described in ChannelBuffer#equals(Object)
.
This method is useful when implementing a new buffer type.public static int compare(ByteBuf bufferA, ByteBuf bufferB)
ByteBuf.compareTo(ByteBuf)
.
This method is useful when implementing a new buffer type.private static long compareUintBigEndian(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement)
private static long compareUintLittleEndian(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement)
private static long compareUintBigEndianA(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement)
private static long compareUintBigEndianB(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement)
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)
ByteBuf.indexOf(int, int, byte)
.
This method is useful when implementing a new buffer type.public static short swapShort(short value)
public static int swapMedium(int value)
public static int swapInt(int value)
public static long swapLong(long value)
public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length)
ByteBuf
that is allocated from the ByteBufAllocator
.private static int firstIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)
private static int lastIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)
public static ByteBuf writeUtf8(ByteBufAllocator alloc, java.lang.CharSequence seq)
public static int writeUtf8(ByteBuf buf, java.lang.CharSequence seq)
private static int writeUtf8(AbstractByteBuf buffer, java.lang.CharSequence seq, int len)
public static ByteBuf writeAscii(ByteBufAllocator alloc, java.lang.CharSequence seq)
public static int writeAscii(ByteBuf buf, java.lang.CharSequence seq)
private static void writeAscii(AbstractByteBuf buffer, java.lang.CharSequence seq, int len)
public static ByteBuf encodeString(ByteBufAllocator alloc, java.nio.CharBuffer src, java.nio.charset.Charset charset)
CharBuffer
using the given Charset
into a new ByteBuf
which
is allocated via the ByteBufAllocator
.static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, java.nio.CharBuffer src, java.nio.charset.Charset charset)
static java.lang.String decodeString(ByteBuf src, int readerIndex, int len, java.nio.charset.Charset charset)
private static void decodeString(java.nio.charset.CharsetDecoder decoder, java.nio.ByteBuffer src, java.nio.CharBuffer dst)
public static java.lang.String prettyHexDump(ByteBuf buffer)
ByteBuf
that is easy to read by humans.public static java.lang.String prettyHexDump(ByteBuf buffer, int offset, int length)
ByteBuf
that is easy to read by humans,
starting at the given offset
using the given length
.public static void appendPrettyHexDump(java.lang.StringBuilder dump, ByteBuf buf)
ByteBuf
to the specified
StringBuilder
that is easy to read by humans.public static void appendPrettyHexDump(java.lang.StringBuilder dump, ByteBuf buf, int offset, int length)
ByteBuf
to the specified
StringBuilder
that is easy to read by humans, starting at the given offset
using
the given length
.public static ByteBuf threadLocalDirectBuffer()
null
otherwise.public static boolean isText(ByteBuf buf, java.nio.charset.Charset charset)
buf
- The given ByteBuf
.charset
- The specified Charset
.public static boolean isText(ByteBuf buf, int index, int length, java.nio.charset.Charset charset)
true
if the specified ByteBuf
starting at index
with length
is valid
text using the given Charset
, otherwise return false
.buf
- The given ByteBuf
.index
- The start index of the specified buffer.length
- The length of the specified buffer.charset
- The specified Charset
.java.lang.IndexOutOfBoundsException
- if index
+ length
is greater than buf.readableBytes
private static boolean isAscii(ByteBuf buf, int index, int length)
true
if the specified ByteBuf
starting at index
with length
is valid
ASCII text, otherwise return false
.buf
- The given ByteBuf
.index
- The start index of the specified buffer.length
- The length of the specified buffer.private static boolean isUtf8(ByteBuf buf, int index, int length)
true
if the specified ByteBuf
starting at index
with length
is valid
UTF8 text, otherwise return false
.buf
- The given ByteBuf
.index
- The start index of the specified buffer.length
- The length of the specified buffer.1. Bytes format of UTF-8 The table below summarizes the format of these different octet types. The letter x indicates bits available for encoding bits of the character number. Char. number range | UTF-8 octet sequence (hexadecimal) | (binary) --------------------+--------------------------------------------- 0000 0000-0000 007F | 0xxxxxxx 0000 0080-0000 07FF | 110xxxxx 10xxxxxx 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
2. Syntax of UTF-8 Byte Sequences UTF8-octets = *( UTF8-char ) UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4 UTF8-1 = %x00-7F UTF8-2 = %xC2-DF UTF8-tail UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) / %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail ) UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) / %xF4 %x80-8F 2( UTF8-tail ) UTF8-tail = %x80-BF