Class ByteBufUtil
- java.lang.Object
-
- io.netty.buffer.ByteBufUtil
-
public final class ByteBufUtil extends java.lang.Object
A collection of utility methods that is related with handlingByteBuf
, such as the generation of hex dump and swapping an integer's byte order.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
appendPrettyHexDump(java.lang.StringBuilder dump, ByteBuf buf)
Appends the prettified multi-line hexadecimal dump of the specifiedByteBuf
to the specifiedStringBuilder
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 specifiedByteBuf
to the specifiedStringBuilder
that is easy to read by humans, starting at the givenoffset
using the givenlength
.static int
compare(ByteBuf bufferA, ByteBuf bufferB)
Compares the two specified buffers as described inByteBuf.compareTo(ByteBuf)
.static void
copy(AsciiString src, int srcIdx, ByteBuf dst, int length)
static void
copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length)
static void
copy(AsciiString src, ByteBuf dst)
static byte
decodeHexByte(java.lang.CharSequence s, int pos)
Decode a 2-digit hex byte from within a string.static byte[]
decodeHexDump(java.lang.CharSequence hexDump)
Decodes a string generated byhexDump(byte[])
static byte[]
decodeHexDump(java.lang.CharSequence hexDump, int fromIndex, int length)
Decodes part of a string generated byhexDump(byte[])
static ByteBuf
encodeString(ByteBufAllocator alloc, java.nio.CharBuffer src, java.nio.charset.Charset charset)
Encode the givenCharBuffer
using the givenCharset
into a newByteBuf
which is allocated via theByteBufAllocator
.static ByteBuf
encodeString(ByteBufAllocator alloc, java.nio.CharBuffer src, java.nio.charset.Charset charset, int extraCapacity)
Encode the givenCharBuffer
using the givenCharset
into a newByteBuf
which is allocated via theByteBufAllocator
.static ByteBuf
ensureAccessible(ByteBuf buffer)
static boolean
ensureWritableSuccess(int ensureWritableResult)
Used to determine if the return value ofByteBuf.ensureWritable(int, boolean)
means that there is adequate space and a write operation will succeed.static boolean
equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length)
Returnstrue
if and only if the two specified buffers are identical to each other forlength
bytes starting ataStartIndex
index for thea
buffer andbStartIndex
index for theb
buffer.static boolean
equals(ByteBuf bufferA, ByteBuf bufferB)
Returnstrue
if and only if the two specified buffers are identical to each other as described inByteBuf.equals(Object)
.static byte[]
getBytes(ByteBuf buf)
Create a copy of the underlying storage frombuf
into a byte array.static byte[]
getBytes(ByteBuf buf, int start, int length)
Create a copy of the underlying storage frombuf
into a byte array.static byte[]
getBytes(ByteBuf buf, int start, int length, boolean copy)
Return an array of the underlying storage frombuf
into a byte array.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 ofByteBuf.indexOf(int, int, byte)
.static int
indexOf(ByteBuf needle, ByteBuf haystack)
Returns the reader index of needle in haystack, or -1 if needle is not in haystack.static boolean
isAccessible(ByteBuf buffer)
static boolean
isText(ByteBuf buf, int index, int length, java.nio.charset.Charset charset)
Returnstrue
if the specifiedByteBuf
starting atindex
withlength
is valid text using the givenCharset
, otherwise returnfalse
.static boolean
isText(ByteBuf buf, java.nio.charset.Charset charset)
static java.lang.String
prettyHexDump(ByteBuf buffer)
Returns a multi-line hexadecimal dump of the specifiedByteBuf
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 specifiedByteBuf
that is easy to read by humans, starting at the givenoffset
using the givenlength
.static ByteBuf
readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length)
Read the given amount of bytes into a newByteBuf
that is allocated from theByteBufAllocator
.static int
readIntBE(ByteBuf buf)
Reads a big-endian 32-bit integer from the buffer.static int
readUnsignedShortBE(ByteBuf buf)
Reads a big-endian unsigned 16-bit short integer from the buffer.static int
reserveAndWriteUtf8(ByteBuf buf, java.lang.CharSequence seq, int reserveBytes)
static int
reserveAndWriteUtf8(ByteBuf buf, java.lang.CharSequence seq, int start, int end, int reserveBytes)
Equivalent to
but avoids subsequence object allocation if possible.reserveAndWriteUtf8(buf, seq.subSequence(start, end), reserveBytes)
static void
setLeakListener(ResourceLeakDetector.LeakListener leakListener)
static ByteBuf
setShortBE(ByteBuf buf, int index, int shortValue)
Sets a big-endian 16-bit short integer to the buffer.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.static int
utf8Bytes(java.lang.CharSequence seq)
Returns the exact bytes length of UTF8 character sequence.static int
utf8Bytes(java.lang.CharSequence seq, int start, int end)
Equivalent to
but avoids subsequence object allocation.utf8Bytes(seq.subSequence(start, end))
static int
utf8MaxBytes(int seqLength)
Returns max bytes length of UTF8 character sequence of the given length.static int
utf8MaxBytes(java.lang.CharSequence seq)
Returns max bytes length of UTF8 character sequence.static ByteBuf
writeAscii(ByteBufAllocator alloc, java.lang.CharSequence seq)
static int
writeAscii(ByteBuf buf, java.lang.CharSequence seq)
static ByteBuf
writeMediumBE(ByteBuf buf, int mediumValue)
Writes a big-endian 24-bit medium integer to the buffer.static ByteBuf
writeShortBE(ByteBuf buf, int shortValue)
Writes a big-endian 16-bit short integer to the buffer.static ByteBuf
writeUtf8(ByteBufAllocator alloc, java.lang.CharSequence seq)
static int
writeUtf8(ByteBuf buf, java.lang.CharSequence seq)
static int
writeUtf8(ByteBuf buf, java.lang.CharSequence seq, int start, int end)
Equivalent to
but avoids subsequence object allocation.writeUtf8(buf, seq.subSequence(start, end))
-
-
-
Method Detail
-
isAccessible
public static boolean isAccessible(ByteBuf buffer)
- Returns:
- whether the specified buffer has a nonzero ref count
-
ensureAccessible
public static ByteBuf ensureAccessible(ByteBuf buffer)
- Returns:
- the passed in buffer
- Throws:
IllegalReferenceCountException
- if the buffer has a zero ref count
-
hexDump
public static java.lang.String hexDump(ByteBuf buffer)
Returns a hex dump of the specified buffer's readable bytes.
-
hexDump
public static java.lang.String hexDump(ByteBuf buffer, int fromIndex, int length)
Returns a hex dump of the specified buffer's sub-region.
-
hexDump
public static java.lang.String hexDump(byte[] array)
Returns a hex dump of the specified byte array.
-
hexDump
public static java.lang.String hexDump(byte[] array, int fromIndex, int length)
Returns a hex dump of the specified byte array's sub-region.
-
decodeHexByte
public static byte decodeHexByte(java.lang.CharSequence s, int pos)
Decode a 2-digit hex byte from within a string.
-
decodeHexDump
public static byte[] decodeHexDump(java.lang.CharSequence hexDump)
Decodes a string generated byhexDump(byte[])
-
decodeHexDump
public static byte[] decodeHexDump(java.lang.CharSequence hexDump, int fromIndex, int length)
Decodes part of a string generated byhexDump(byte[])
-
ensureWritableSuccess
public static boolean ensureWritableSuccess(int ensureWritableResult)
Used to determine if the return value ofByteBuf.ensureWritable(int, boolean)
means that there is adequate space and a write operation will succeed.- Parameters:
ensureWritableResult
- The return value fromByteBuf.ensureWritable(int, boolean)
.- Returns:
true
ifensureWritableResult
means that there is adequate space and a write operation will succeed.
-
hashCode
public static int hashCode(ByteBuf buffer)
Calculates the hash code of the specified buffer. This method is useful when implementing a new buffer type.
-
indexOf
public static int indexOf(ByteBuf needle, ByteBuf haystack)
Returns the reader index of needle in haystack, or -1 if needle is not in haystack. This method uses the Two-Way string matching algorithm, which yields O(1) space complexity and excellent performance.
-
equals
public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length)
Returnstrue
if and only if the two specified buffers are identical to each other forlength
bytes starting ataStartIndex
index for thea
buffer andbStartIndex
index for theb
buffer. A more compact way to express this is:a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]
-
equals
public static boolean equals(ByteBuf bufferA, ByteBuf bufferB)
Returnstrue
if and only if the two specified buffers are identical to each other as described inByteBuf.equals(Object)
. This method is useful when implementing a new buffer type.
-
compare
public static int compare(ByteBuf bufferA, ByteBuf bufferB)
Compares the two specified buffers as described inByteBuf.compareTo(ByteBuf)
. This method is useful when implementing a new buffer type.
-
indexOf
public static int indexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value)
The default implementation ofByteBuf.indexOf(int, int, byte)
. This method is useful when implementing a new buffer type.
-
swapShort
public static short swapShort(short value)
Toggles the endianness of the specified 16-bit short integer.
-
swapMedium
public static int swapMedium(int value)
Toggles the endianness of the specified 24-bit medium integer.
-
swapInt
public static int swapInt(int value)
Toggles the endianness of the specified 32-bit integer.
-
swapLong
public static long swapLong(long value)
Toggles the endianness of the specified 64-bit long integer.
-
writeShortBE
public static ByteBuf writeShortBE(ByteBuf buf, int shortValue)
Writes a big-endian 16-bit short integer to the buffer.
-
setShortBE
public static ByteBuf setShortBE(ByteBuf buf, int index, int shortValue)
Sets a big-endian 16-bit short integer to the buffer.
-
writeMediumBE
public static ByteBuf writeMediumBE(ByteBuf buf, int mediumValue)
Writes a big-endian 24-bit medium integer to the buffer.
-
readUnsignedShortBE
public static int readUnsignedShortBE(ByteBuf buf)
Reads a big-endian unsigned 16-bit short integer from the buffer.
-
readIntBE
public static int readIntBE(ByteBuf buf)
Reads a big-endian 32-bit integer from the buffer.
-
readBytes
public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length)
Read the given amount of bytes into a newByteBuf
that is allocated from theByteBufAllocator
.
-
writeUtf8
public static ByteBuf writeUtf8(ByteBufAllocator alloc, java.lang.CharSequence seq)
-
writeUtf8
public static int writeUtf8(ByteBuf buf, java.lang.CharSequence seq)
Encode aCharSequence
in UTF-8 and write it to aByteBuf
.It behaves like
reserveAndWriteUtf8(ByteBuf, CharSequence, int)
withreserveBytes
computed byutf8MaxBytes(CharSequence)
.
This method returns the actual number of bytes written.
-
writeUtf8
public static int writeUtf8(ByteBuf buf, java.lang.CharSequence seq, int start, int end)
Equivalent to
but avoids subsequence object allocation.writeUtf8(buf, seq.subSequence(start, end))
-
reserveAndWriteUtf8
public static int reserveAndWriteUtf8(ByteBuf buf, java.lang.CharSequence seq, int reserveBytes)
Encode aCharSequence
in UTF-8 and write it intoreserveBytes
of aByteBuf
.The
reserveBytes
must be computed (ie eagerly usingutf8MaxBytes(CharSequence)
or exactly withutf8Bytes(CharSequence)
) to ensure this method to not fail: for performance reasons the index checks will be performed using justreserveBytes
.
This method returns the actual number of bytes written.
-
reserveAndWriteUtf8
public static int reserveAndWriteUtf8(ByteBuf buf, java.lang.CharSequence seq, int start, int end, int reserveBytes)
Equivalent to
but avoids subsequence object allocation if possible.reserveAndWriteUtf8(buf, seq.subSequence(start, end), reserveBytes)
- Returns:
- actual number of bytes written
-
utf8MaxBytes
public static int utf8MaxBytes(int seqLength)
Returns max bytes length of UTF8 character sequence of the given length.
-
utf8MaxBytes
public static int utf8MaxBytes(java.lang.CharSequence seq)
Returns max bytes length of UTF8 character sequence.It behaves like
utf8MaxBytes(int)
applied toseq
CharSequence.length()
.
-
utf8Bytes
public static int utf8Bytes(java.lang.CharSequence seq)
Returns the exact bytes length of UTF8 character sequence.This method is producing the exact length according to
writeUtf8(ByteBuf, CharSequence)
.
-
utf8Bytes
public static int utf8Bytes(java.lang.CharSequence seq, int start, int end)
Equivalent to
but avoids subsequence object allocation.utf8Bytes(seq.subSequence(start, end))
This method is producing the exact length according to
writeUtf8(ByteBuf, CharSequence, int, int)
.
-
writeAscii
public static ByteBuf writeAscii(ByteBufAllocator alloc, java.lang.CharSequence seq)
-
writeAscii
public static int writeAscii(ByteBuf buf, java.lang.CharSequence seq)
-
encodeString
public static ByteBuf encodeString(ByteBufAllocator alloc, java.nio.CharBuffer src, java.nio.charset.Charset charset)
Encode the givenCharBuffer
using the givenCharset
into a newByteBuf
which is allocated via theByteBufAllocator
.
-
encodeString
public static ByteBuf encodeString(ByteBufAllocator alloc, java.nio.CharBuffer src, java.nio.charset.Charset charset, int extraCapacity)
Encode the givenCharBuffer
using the givenCharset
into a newByteBuf
which is allocated via theByteBufAllocator
.- Parameters:
alloc
- TheByteBufAllocator
to allocateByteBuf
.src
- TheCharBuffer
to encode.charset
- The specifiedCharset
.extraCapacity
- the extra capacity to alloc except the space for decoding.
-
threadLocalDirectBuffer
public static ByteBuf threadLocalDirectBuffer()
Returns a cached thread-local direct buffer, if available.- Returns:
- a cached thread-local direct buffer, if available.
null
otherwise.
-
getBytes
public static byte[] getBytes(ByteBuf buf)
Create a copy of the underlying storage frombuf
into a byte array. The copy will start atByteBuf.readerIndex()
and copyByteBuf.readableBytes()
bytes.
-
getBytes
public static byte[] getBytes(ByteBuf buf, int start, int length)
Create a copy of the underlying storage frombuf
into a byte array. The copy will start atstart
and copylength
bytes.
-
getBytes
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy)
Return an array of the underlying storage frombuf
into a byte array. The copy will start atstart
and copylength
bytes. Ifcopy
is true a copy will be made of the memory. Ifcopy
is false the underlying storage will be shared, if possible.
-
copy
public static void copy(AsciiString src, ByteBuf dst)
- Parameters:
src
- the source string to copydst
- the destination buffer
-
copy
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length)
Copies the content ofsrc
to aByteBuf
usingByteBuf.setBytes(int, byte[], int, int)
. Unlike thecopy(AsciiString, ByteBuf)
andcopy(AsciiString, int, ByteBuf, int)
methods, this method do not increase awriterIndex
ofdst
buffer.- Parameters:
src
- the source string to copysrcIdx
- the starting offset of characters to copydst
- the destination bufferdstIdx
- the starting offset in the destination bufferlength
- the number of characters to copy
-
copy
public static void copy(AsciiString src, int srcIdx, ByteBuf dst, int length)
- Parameters:
src
- the source string to copysrcIdx
- the starting offset of characters to copydst
- the destination bufferlength
- the number of characters to copy
-
prettyHexDump
public static java.lang.String prettyHexDump(ByteBuf buffer)
Returns a multi-line hexadecimal dump of the specifiedByteBuf
that is easy to read by humans.
-
prettyHexDump
public static java.lang.String prettyHexDump(ByteBuf buffer, int offset, int length)
Returns a multi-line hexadecimal dump of the specifiedByteBuf
that is easy to read by humans, starting at the givenoffset
using the givenlength
.
-
appendPrettyHexDump
public static void appendPrettyHexDump(java.lang.StringBuilder dump, ByteBuf buf)
Appends the prettified multi-line hexadecimal dump of the specifiedByteBuf
to the specifiedStringBuilder
that is easy to read by humans.
-
appendPrettyHexDump
public static void appendPrettyHexDump(java.lang.StringBuilder dump, ByteBuf buf, int offset, int length)
Appends the prettified multi-line hexadecimal dump of the specifiedByteBuf
to the specifiedStringBuilder
that is easy to read by humans, starting at the givenoffset
using the givenlength
.
-
isText
public static boolean isText(ByteBuf buf, java.nio.charset.Charset charset)
- Parameters:
buf
- The givenByteBuf
.charset
- The specifiedCharset
.
-
isText
public static boolean isText(ByteBuf buf, int index, int length, java.nio.charset.Charset charset)
Returnstrue
if the specifiedByteBuf
starting atindex
withlength
is valid text using the givenCharset
, otherwise returnfalse
.- Parameters:
buf
- The givenByteBuf
.index
- The start index of the specified buffer.length
- The length of the specified buffer.charset
- The specifiedCharset
.- Throws:
java.lang.IndexOutOfBoundsException
- ifindex
+length
is greater thanbuf.readableBytes
-
setLeakListener
public static void setLeakListener(ResourceLeakDetector.LeakListener leakListener)
- Parameters:
leakListener
- If leakListener is not null, it will be notified once a ByteBuf leak is detected.
-
-