Top Description Inners Fields Constructors Methods
io.netty.buffer

public final Class ByteBufUtil

extends Object
Class Inheritance
Imports
io.netty.util.AsciiString, .ByteProcessor, .CharsetUtil, .IllegalReferenceCountException, .Recycler.EnhancedHandle, .ResourceLeakDetector, io.netty.util.concurrent.FastThreadLocal, io.netty.util.internal.MathUtil, .ObjectPool, .ObjectPool.Handle, .ObjectPool.ObjectCreator, .PlatformDependent, .StringUtil, .SystemPropertyUtil, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.io.IOException, .OutputStream, java.nio.ByteBuffer, .ByteOrder, .CharBuffer, java.nio.charset.CharacterCodingException, .Charset, .CharsetDecoder, .CharsetEncoder, .CoderResult, .CodingErrorAction, java.util.Arrays, .Locale

A collection of utility methods that is related with handling ByteBuf, such as the generation of hex dump and swapping an integer's byte order.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final FastThreadLocal<byte[]>
pack-priv static final ByteBufAllocator
private static final ByteProcessor
FIND_NON_ASCII

Aborts on a byte which is not a valid ASCII character.

private static final InternalLogger
private static final int
private static final int
pack-priv static final int
private static final int
pack-priv static final int
private static final byte

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
appendPrettyHexDump(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.

public static void
appendPrettyHexDump(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.

private static CharSequence
checkCharSequenceBounds(CharSequence seq, int start, int end)

public 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)

public static void
copy(AsciiString
the source string to copy
src
,
ByteBuf
the destination buffer
dst
)

Copies the all content of src to a ByteBuf using ByteBuf#writeBytes(byte[], int, int).

public static void
copy(AsciiString
the source string to copy
src
,
int
the starting offset of characters to copy
srcIdx
,
ByteBuf
the destination buffer
dst
,
int
the starting offset in the destination buffer
dstIdx
,
int
the number of characters to copy
length
)

Copies the content of src to a ByteBuf using ByteBuf#setBytes(int, byte[], int, int).

public static void
copy(AsciiString
the source string to copy
src
,
int
the starting offset of characters to copy
srcIdx
,
ByteBuf
the destination buffer
dst
,
int
the number of characters to copy
length
)

Copies the content of src to a ByteBuf using ByteBuf#writeBytes(byte[], int, int).

public static byte
decodeHexByte(CharSequence s, int pos)

Decode a 2-digit hex byte from within a string.

public static byte[]
decodeHexDump(CharSequence hexDump)

Decodes a string generated by hexDump(byte[])

public static byte[]
decodeHexDump(CharSequence hexDump, int fromIndex, int length)

Decodes part of a string generated by hexDump(byte[])

pack-priv static String
decodeString(ByteBuf src, int readerIndex, int len, Charset charset)

public static ByteBuf
encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset)

Encode the given CharBuffer using the given Charset into a new ByteBuf which is allocated via the ByteBufAllocator.

public static ByteBuf
encodeString(ByteBufAllocator
The ByteBufAllocator to allocate ByteBuf.
alloc
,
CharBuffer
The CharBuffer to encode.
src
,
Charset
The specified Charset.
charset
,
int
the extra capacity to alloc except the space for decoding.
extraCapacity
)

Encode the given CharBuffer using the given Charset into a new ByteBuf which is allocated via the ByteBufAllocator.

pack-priv static ByteBuf
encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset, int extraCapacity)

public static ByteBuf

Returns:

the passed in buffer
ensureAccessible
(ByteBuf buffer)

public static boolean

Returns:

true if ensureWritableResult means that there is adequate space and a write operation will succeed.
ensureWritableSuccess
(int ensureWritableResult)

Used to determine if the return value of ByteBuf#ensureWritable(int, boolean) means that there is adequate space and a write operation will succeed.

public static boolean
equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length)

Returns true if and only if the two specified buffers are identical to each other for length bytes starting at aStartIndex index for the a buffer and bStartIndex index for the b buffer.

public 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 ByteBuf#equals(Object).

pack-priv static int
firstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value)

This is using a SWAR (SIMD Within A Register) batch read technique to minimize bound-checks and improve memory usage while searching for value.

public static byte[]
getBytes(ByteBuf buf)

Create a copy of the underlying storage from buf into a byte array.

public static byte[]
getBytes(ByteBuf buf, int start, int length)

Create a copy of the underlying storage from buf into a byte array.

public static byte[]
getBytes(ByteBuf buf, int start, int length, boolean copy)

Return an array of the underlying storage from buf into a byte array.

private static void
getBytes(ByteBuffer inBuffer, byte[] in, int inOffset, int inLen, OutputStream out, int outLen)

public static int
hashCode(ByteBuf buffer)

Calculates the hash code of the specified buffer.

public static String
hexDump(ByteBuf buffer)

Returns a hex dump of the specified buffer's readable bytes.

public static String
hexDump(ByteBuf buffer, int fromIndex, int length)

Returns a hex dump of the specified buffer's sub-region.

public static String
hexDump(byte[] array)

Returns a hex dump of the specified byte array.

public static String
hexDump(byte[] array, int fromIndex, int length)

Returns a hex dump of the specified byte array's sub-region.

public static int
indexOf(ByteBuf needle, ByteBuf haystack)

Returns the reader index of needle in haystack, or -1 if needle is not in haystack.

public static int
indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)

The default implementation of ByteBuf#indexOf(int, int, byte).

public static boolean

Returns:

whether the specified buffer has a nonzero ref count
isAccessible
(ByteBuf buffer)

private static boolean
isAscii(ByteBuf
The given ByteBuf.
buf
,
int
The start index of the specified buffer.
index
,
int
The length of the specified buffer.
length
)

Returns true if the specified ByteBuf starting at index with length is valid ASCII text, otherwise return false.

public static boolean
isText(ByteBuf
The given ByteBuf.
buf
,
Charset
The specified Charset.
charset
)

Returns true if the given ByteBuf is valid text using the given Charset, otherwise return false.

public static boolean
isText(ByteBuf
The given ByteBuf.
buf
,
int
The start index of the specified buffer.
index
,
int
The length of the specified buffer.
length
,
Charset
The specified 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
The given ByteBuf.
buf
,
int
The start index of the specified buffer.
index
,
int
The length of the specified buffer.
length
)

Returns true if the specified ByteBuf starting at index with length is valid UTF8 text, otherwise return false.

pack-priv static int
lastIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value)

private static int
linearFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value)

private static long
maxSuf(ByteBuf x, int m, int start, boolean isSuffix)

public static String
prettyHexDump(ByteBuf buffer)

Returns a multi-line hexadecimal dump of the specified ByteBuf that is easy to read by humans.

public static 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.

public 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.

pack-priv static void
readBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out)

Read bytes from the given ByteBuffer into the given OutputStream using the position and length.

public static int
readIntBE(ByteBuf buf)

Reads a big-endian 32-bit integer from the buffer.

public static int
readUnsignedShortBE(ByteBuf buf)

Reads a big-endian unsigned 16-bit short integer from the buffer.

public static int
reserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int reserveBytes)

Encode a CharSequence in UTF-8 and write it into reserveBytes of a ByteBuf.

public static int

Returns:

actual number of bytes written
reserveAndWriteUtf8
(ByteBuf buf, CharSequence seq, int start, int end, int reserveBytes)

Equivalent to reserveAndWriteUtf8(buf, seq.subSequence(start, end), reserveBytes) but avoids subsequence object allocation if possible.

private static int
reserveAndWriteUtf8Seq(ByteBuf buf, CharSequence seq, int start, int end, int reserveBytes)

private static int
safeArrayWriteUtf8(byte[] buffer, int writerIndex, CharSequence seq, int start, int end)

private static int
safeDirectWriteUtf8(ByteBuffer buffer, int writerIndex, CharSequence seq, int start, int end)

private static int
safeWriteUtf8(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int start, int end)

public static void
setLeakListener(ResourceLeakDetector.LeakListener
If leakListener is not null, it will be notified once a ByteBuf leak is detected.
leakListener
)

Set AbstractByteBuf#leakDetector's ResourceLeakDetector.LeakListener.

public static ByteBuf
setShortBE(ByteBuf buf, int index, int shortValue)

Sets a big-endian 16-bit short integer to the buffer.

public static int
swapInt(int value)

Toggles the endianness of the specified 32-bit integer.

public static long
swapLong(long value)

Toggles the endianness of the specified 64-bit long integer.

public static int
swapMedium(int value)

Toggles the endianness of the specified 24-bit medium integer.

public static short
swapShort(short value)

Toggles the endianness of the specified 16-bit short integer.

public static ByteBuf

Returns:

a cached thread-local direct buffer, if available. null otherwise.
threadLocalDirectBuffer
()

Returns a cached thread-local direct buffer, if available.

pack-priv static byte[]
threadLocalTempArray(int minLength)

Allocates a new array if minLength > ByteBufUtil#MAX_TL_ARRAY_LEN

private static long
uintFromLE(long value)

private static int
unrolledFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int byteCount, byte value)

private static int
unsafeWriteUtf8(byte[] buffer, long memoryOffset, int writerIndex, CharSequence seq, int start, int end)

private static int
utf8ByteCount(final CharSequence seq, int start, int end)

public static int
utf8Bytes(final CharSequence seq)

Returns the exact bytes length of UTF8 character sequence.

public static int
utf8Bytes(final CharSequence seq, int start, int end)

Equivalent to utf8Bytes(seq.subSequence(start, end)) but avoids subsequence object allocation.

private static int
utf8BytesNonAscii(final CharSequence seq, final int start, final int end)

public static int
utf8MaxBytes(final int seqLength)

Returns max bytes length of UTF8 character sequence of the given length.

public static int
utf8MaxBytes(CharSequence seq)

Returns max bytes length of UTF8 character sequence.

public static ByteBuf

Returns:

The ByteBuf which contains the ASCII encoded result.
writeAscii
(ByteBufAllocator
The allocator used to allocate a new ByteBuf.
alloc
,
CharSequence
The characters to write into a buffer.
seq
)

Encode a CharSequence in ASCII and write it to a ByteBuf allocated with alloc.

public static int
writeAscii(ByteBuf buf, CharSequence seq)

Encode a CharSequence in ASCII and write it to a ByteBuf.

pack-priv static int
writeAscii(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int len)

private static int
writeAsciiCharSequence(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int len)

pack-priv static void
writeAsciiString(AbstractByteBuf buffer, int writerIndex, AsciiString seq, int start, int end)

public static ByteBuf
writeMediumBE(ByteBuf buf, int mediumValue)

Writes a big-endian 24-bit medium integer to the buffer.

public static ByteBuf
writeShortBE(ByteBuf buf, int shortValue)

Writes a big-endian 16-bit short integer to the buffer.

public static ByteBuf

Returns:

The ByteBuf which contains the UTF-8 encoded result.
writeUtf8
(ByteBufAllocator
The allocator used to allocate a new ByteBuf.
alloc
,
CharSequence
The characters to write into a buffer.
seq
)

Encode a CharSequence in UTF-8 and write it to a ByteBuf allocated with alloc.

public static int
writeUtf8(ByteBuf buf, CharSequence seq)

Encode a CharSequence in UTF-8 and write it to a ByteBuf.

public static int
writeUtf8(ByteBuf buf, CharSequence seq, int start, int end)

Equivalent to writeUtf8(buf, seq.subSequence(start, end)) but avoids subsequence object allocation.

pack-priv static int
writeUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int len)

pack-priv static int
writeUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int start, int end)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait