Class ByteBufUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class(package private) static final class(package private) static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final FastThreadLocal<byte[]> (package private) static final ByteBufAllocatorprivate static final ByteProcessorAborts on a byte which is not a valid ASCII character.private static final InternalLoggerprivate static final intprivate static final int(package private) static final intprivate static final int(package private) static final intprivate static final byte -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendPrettyHexDump(StringBuilder dump, ByteBuf buf) Appends the prettified multi-line hexadecimal dump of the specifiedByteBufto the specifiedStringBuilderthat is easy to read by humans.static voidappendPrettyHexDump(StringBuilder dump, ByteBuf buf, int offset, int length) Appends the prettified multi-line hexadecimal dump of the specifiedByteBufto the specifiedStringBuilderthat is easy to read by humans, starting at the givenoffsetusing the givenlength.private static CharSequencecheckCharSequenceBounds(CharSequence seq, int start, int end) static intCompares the two specified buffers as described inByteBuf.compareTo(ByteBuf).private static longcompareUintBigEndian(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement) private static longcompareUintBigEndianA(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement) private static longcompareUintBigEndianB(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement) private static longcompareUintLittleEndian(ByteBuf bufferA, ByteBuf bufferB, int aIndex, int bIndex, int uintCountIncrement) static voidcopy(AsciiString src, int srcIdx, ByteBuf dst, int length) static voidcopy(AsciiString src, int srcIdx, ByteBuf dst, int dstIdx, int length) static voidcopy(AsciiString src, ByteBuf dst) static bytedecodeHexByte(CharSequence s, int pos) Decode a 2-digit hex byte from within a string.static byte[]decodeHexDump(CharSequence hexDump) Decodes a string generated byhexDump(byte[])static byte[]decodeHexDump(CharSequence hexDump, int fromIndex, int length) Decodes part of a string generated byhexDump(byte[])(package private) static StringdecodeString(ByteBuf src, int readerIndex, int len, Charset charset) static ByteBufencodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset) Encode the givenCharBufferusing the givenCharsetinto a newByteBufwhich is allocated via theByteBufAllocator.static ByteBufencodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset, int extraCapacity) Encode the givenCharBufferusing the givenCharsetinto a newByteBufwhich is allocated via theByteBufAllocator.(package private) static ByteBufencodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset, int extraCapacity) static ByteBufensureAccessible(ByteBuf buffer) static booleanensureWritableSuccess(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 booleanReturnstrueif and only if the two specified buffers are identical to each other forlengthbytes starting ataStartIndexindex for theabuffer andbStartIndexindex for thebbuffer.static booleanReturnstrueif and only if the two specified buffers are identical to each other as described inByteBuf.equals(Object).(package private) static intfirstIndexOf(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 forvalue.static byte[]Create a copy of the underlying storage frombufinto a byte array.static byte[]Create a copy of the underlying storage frombufinto a byte array.static byte[]Return an array of the underlying storage frombufinto a byte array.private static voidgetBytes(ByteBuffer inBuffer, byte[] in, int inOffset, int inLen, OutputStream out, int outLen) static intCalculates the hash code of the specified buffer.static StringhexDump(byte[] array) Returns a hex dump of the specified byte array.static StringhexDump(byte[] array, int fromIndex, int length) Returns a hex dump of the specified byte array's sub-region.static StringReturns a hex dump of the specified buffer's readable bytes.static StringReturns a hex dump of the specified buffer's sub-region.static intThe default implementation ofByteBuf.indexOf(int, int, byte).static intReturns the reader index of needle in haystack, or -1 if needle is not in haystack.static booleanisAccessible(ByteBuf buffer) private static booleanReturnstrueif the specifiedByteBufstarting atindexwithlengthis valid ASCII text, otherwise returnfalse.static booleanstatic booleanprivate static booleanReturnstrueif the specifiedByteBufstarting atindexwithlengthis valid UTF8 text, otherwise returnfalse.(package private) static intlastIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value) private static intlinearFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value) private static intlinearLastIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value) private static longstatic StringprettyHexDump(ByteBuf buffer) Returns a multi-line hexadecimal dump of the specifiedByteBufthat is easy to read by humans.static StringprettyHexDump(ByteBuf buffer, int offset, int length) Returns a multi-line hexadecimal dump of the specifiedByteBufthat is easy to read by humans, starting at the givenoffsetusing the givenlength.static ByteBufreadBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) Read the given amount of bytes into a newByteBufthat is allocated from theByteBufAllocator.(package private) static voidreadBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out) static intReads a big-endian 32-bit integer from the buffer.static intReads a big-endian unsigned 16-bit short integer from the buffer.static intreserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int reserveBytes) static intreserveAndWriteUtf8(ByteBuf buf, CharSequence seq, int start, int end, int reserveBytes) Equivalent tobut avoids subsequence object allocation if possible.reserveAndWriteUtf8(buf, seq.subSequence(start, end), reserveBytes)private static intreserveAndWriteUtf8Seq(ByteBuf buf, CharSequence seq, int start, int end, int reserveBytes) private static intsafeArrayWriteUtf8(byte[] buffer, int writerIndex, CharSequence seq, int start, int end) private static intsafeDirectWriteUtf8(ByteBuffer buffer, int writerIndex, CharSequence seq, int start, int end) private static intsafeWriteUtf8(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int start, int end) static voidsetLeakListener(ResourceLeakDetector.LeakListener leakListener) static ByteBufsetShortBE(ByteBuf buf, int index, int shortValue) Sets a big-endian 16-bit short integer to the buffer.static intswapInt(int value) Toggles the endianness of the specified 32-bit integer.static longswapLong(long value) Toggles the endianness of the specified 64-bit long integer.static intswapMedium(int value) Toggles the endianness of the specified 24-bit medium integer.static shortswapShort(short value) Toggles the endianness of the specified 16-bit short integer.static ByteBufReturns a cached thread-local direct buffer, if available.(package private) static byte[]threadLocalTempArray(int minLength) Allocates a new array if minLength >MAX_TL_ARRAY_LENprivate static longuintFromLE(long value) private static intunrolledFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int byteCount, byte value) private static intunrolledLastIndexOf(AbstractByteBuf buffer, int fromIndex, int byteCount, byte value) private static intunsafeWriteUtf8(byte[] buffer, long memoryOffset, int writerIndex, CharSequence seq, int start, int end) private static intutf8ByteCount(CharSequence seq, int start, int end) static intutf8Bytes(CharSequence seq) Returns the exact bytes length of UTF8 character sequence.static intutf8Bytes(CharSequence seq, int start, int end) Equivalent tobut avoids subsequence object allocation.utf8Bytes(seq.subSequence(start, end))private static intutf8BytesNonAscii(CharSequence seq, int start, int end) static intutf8MaxBytes(int seqLength) Returns max bytes length of UTF8 character sequence of the given length.static intutf8MaxBytes(CharSequence seq) Returns max bytes length of UTF8 character sequence.(package private) static intwriteAscii(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int len) static ByteBufwriteAscii(ByteBufAllocator alloc, CharSequence seq) static intwriteAscii(ByteBuf buf, CharSequence seq) private static intwriteAsciiCharSequence(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int len) (package private) static voidwriteAsciiString(AbstractByteBuf buffer, int writerIndex, AsciiString seq, int start, int end) static ByteBufwriteMediumBE(ByteBuf buf, int mediumValue) Writes a big-endian 24-bit medium integer to the buffer.static ByteBufwriteShortBE(ByteBuf buf, int shortValue) Writes a big-endian 16-bit short integer to the buffer.(package private) static intwriteUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int len) (package private) static intwriteUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int start, int end) static ByteBufwriteUtf8(ByteBufAllocator alloc, CharSequence seq) static intwriteUtf8(ByteBuf buf, CharSequence seq) static intwriteUtf8(ByteBuf buf, CharSequence seq, int start, int end) Equivalent tobut avoids subsequence object allocation.writeUtf8(buf, seq.subSequence(start, end))
-
Field Details
-
logger
-
BYTE_ARRAYS
-
WRITE_UTF_UNKNOWN
private static final byte WRITE_UTF_UNKNOWN- See Also:
-
MAX_CHAR_BUFFER_SIZE
private static final int MAX_CHAR_BUFFER_SIZE -
THREAD_LOCAL_BUFFER_SIZE
private static final int THREAD_LOCAL_BUFFER_SIZE -
MAX_BYTES_PER_CHAR_UTF8
private static final int MAX_BYTES_PER_CHAR_UTF8 -
WRITE_CHUNK_SIZE
static final int WRITE_CHUNK_SIZE- See Also:
-
DEFAULT_ALLOCATOR
-
MAX_TL_ARRAY_LEN
static final int MAX_TL_ARRAY_LEN- See Also:
-
FIND_NON_ASCII
Aborts on a byte which is not a valid ASCII character.
-
-
Constructor Details
-
ByteBufUtil
private ByteBufUtil()
-
-
Method Details
-
threadLocalTempArray
static byte[] threadLocalTempArray(int minLength) Allocates a new array if minLength >MAX_TL_ARRAY_LEN -
isAccessible
- Returns:
- whether the specified buffer has a nonzero ref count
-
ensureAccessible
- Returns:
- the passed in buffer
- Throws:
IllegalReferenceCountException- if the buffer has a zero ref count
-
hexDump
-
hexDump
-
hexDump
-
hexDump
-
decodeHexByte
Decode a 2-digit hex byte from within a string. -
decodeHexDump
Decodes a string generated byhexDump(byte[]) -
decodeHexDump
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:
trueifensureWritableResultmeans that there is adequate space and a write operation will succeed.
-
hashCode
Calculates the hash code of the specified buffer. This method is useful when implementing a new buffer type. -
indexOf
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. -
maxSuf
-
equals
Returnstrueif and only if the two specified buffers are identical to each other forlengthbytes starting ataStartIndexindex for theabuffer andbStartIndexindex for thebbuffer. A more compact way to express this is:a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length] -
equals
Returnstrueif 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
Compares the two specified buffers as described inByteBuf.compareTo(ByteBuf). This method is useful when implementing a new buffer type. -
compareUintBigEndian
-
compareUintLittleEndian
-
compareUintBigEndianA
-
compareUintBigEndianB
-
uintFromLE
private static long uintFromLE(long value) -
unrolledFirstIndexOf
private static int unrolledFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int byteCount, byte value) -
firstIndexOf
This is using a SWAR (SIMD Within A Register) batch read technique to minimize bound-checks and improve memory usage while searching forvalue. -
linearFirstIndexOf
private static int linearFirstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value) -
indexOf
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
-
setShortBE
-
writeMediumBE
-
readUnsignedShortBE
Reads a big-endian unsigned 16-bit short integer from the buffer. -
readIntBE
Reads a big-endian 32-bit integer from the buffer. -
readBytes
Read the given amount of bytes into a newByteBufthat is allocated from theByteBufAllocator. -
lastIndexOf
-
linearLastIndexOf
private static int linearLastIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte value) -
unrolledLastIndexOf
private static int unrolledLastIndexOf(AbstractByteBuf buffer, int fromIndex, int byteCount, byte value) -
checkCharSequenceBounds
-
writeUtf8
-
writeUtf8
Encode aCharSequencein UTF-8 and write it to aByteBuf.It behaves like
reserveAndWriteUtf8(ByteBuf, CharSequence, int)withreserveBytescomputed byutf8MaxBytes(CharSequence).
This method returns the actual number of bytes written. -
writeUtf8
Equivalent tobut avoids subsequence object allocation.writeUtf8(buf, seq.subSequence(start, end)) -
reserveAndWriteUtf8
Encode aCharSequencein UTF-8 and write it intoreserveBytesof aByteBuf.The
reserveBytesmust 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, CharSequence seq, int start, int end, int reserveBytes) Equivalent tobut avoids subsequence object allocation if possible.reserveAndWriteUtf8(buf, seq.subSequence(start, end), reserveBytes)- Returns:
- actual number of bytes written
-
reserveAndWriteUtf8Seq
private static int reserveAndWriteUtf8Seq(ByteBuf buf, CharSequence seq, int start, int end, int reserveBytes) -
writeUtf8
static int writeUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int len) -
writeUtf8
static int writeUtf8(AbstractByteBuf buffer, int writerIndex, int reservedBytes, CharSequence seq, int start, int end) -
writeAsciiString
static void writeAsciiString(AbstractByteBuf buffer, int writerIndex, AsciiString seq, int start, int end) -
safeDirectWriteUtf8
private static int safeDirectWriteUtf8(ByteBuffer buffer, int writerIndex, CharSequence seq, int start, int end) -
safeWriteUtf8
private static int safeWriteUtf8(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int start, int end) -
safeArrayWriteUtf8
private static int safeArrayWriteUtf8(byte[] buffer, int writerIndex, CharSequence seq, int start, int end) -
unsafeWriteUtf8
private static int unsafeWriteUtf8(byte[] buffer, long memoryOffset, int writerIndex, CharSequence seq, int start, int end) -
utf8MaxBytes
public static int utf8MaxBytes(int seqLength) Returns max bytes length of UTF8 character sequence of the given length. -
utf8MaxBytes
Returns max bytes length of UTF8 character sequence.It behaves like
utf8MaxBytes(int)applied toseqCharSequence.length(). -
utf8Bytes
Returns the exact bytes length of UTF8 character sequence.This method is producing the exact length according to
writeUtf8(ByteBuf, CharSequence). -
utf8Bytes
Equivalent tobut avoids subsequence object allocation.utf8Bytes(seq.subSequence(start, end))This method is producing the exact length according to
writeUtf8(ByteBuf, CharSequence, int, int). -
utf8ByteCount
-
utf8BytesNonAscii
-
writeAscii
-
writeAscii
Encode aCharSequencein ASCII and write it to aByteBuf. This method returns the actual number of bytes written. -
writeAscii
-
writeAsciiCharSequence
private static int writeAsciiCharSequence(AbstractByteBuf buffer, int writerIndex, CharSequence seq, int len) -
encodeString
Encode the givenCharBufferusing the givenCharsetinto a newByteBufwhich is allocated via theByteBufAllocator. -
encodeString
public static ByteBuf encodeString(ByteBufAllocator alloc, CharBuffer src, Charset charset, int extraCapacity) Encode the givenCharBufferusing the givenCharsetinto a newByteBufwhich is allocated via theByteBufAllocator.- Parameters:
alloc- TheByteBufAllocatorto allocateByteBuf.src- TheCharBufferto encode.charset- The specifiedCharset.extraCapacity- the extra capacity to alloc except the space for decoding.
-
encodeString0
static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset, int extraCapacity) -
decodeString
-
threadLocalDirectBuffer
Returns a cached thread-local direct buffer, if available.- Returns:
- a cached thread-local direct buffer, if available.
nullotherwise.
-
getBytes
Create a copy of the underlying storage frombufinto a byte array. The copy will start atByteBuf.readerIndex()and copyByteBuf.readableBytes()bytes. -
getBytes
Create a copy of the underlying storage frombufinto a byte array. The copy will start atstartand copylengthbytes. -
getBytes
Return an array of the underlying storage frombufinto a byte array. The copy will start atstartand copylengthbytes. Ifcopyis true a copy will be made of the memory. Ifcopyis false the underlying storage will be shared, if possible. -
copy
- Parameters:
src- the source string to copydst- the destination buffer
-
copy
Copies the content ofsrcto aByteBufusingByteBuf.setBytes(int, byte[], int, int). Unlike thecopy(AsciiString, ByteBuf)andcopy(AsciiString, int, ByteBuf, int)methods, this method do not increase awriterIndexofdstbuffer.- 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
- Parameters:
src- the source string to copysrcIdx- the starting offset of characters to copydst- the destination bufferlength- the number of characters to copy
-
prettyHexDump
-
prettyHexDump
-
appendPrettyHexDump
Appends the prettified multi-line hexadecimal dump of the specifiedByteBufto the specifiedStringBuilderthat is easy to read by humans. -
appendPrettyHexDump
Appends the prettified multi-line hexadecimal dump of the specifiedByteBufto the specifiedStringBuilderthat is easy to read by humans, starting at the givenoffsetusing the givenlength. -
isText
-
isText
Returnstrueif the specifiedByteBufstarting atindexwithlengthis 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:
IndexOutOfBoundsException- ifindex+lengthis greater thanbuf.readableBytes
-
isAscii
-
isUtf8
-
readBytes
static void readBytes(ByteBufAllocator allocator, ByteBuffer buffer, int position, int length, OutputStream out) throws IOException Read bytes from the givenByteBufferinto the givenOutputStreamusing thepositionandlength. The position and limit of the givenByteBuffermay be adjusted.- Throws:
IOException
-
getBytes
private static void getBytes(ByteBuffer inBuffer, byte[] in, int inOffset, int inLen, OutputStream out, int outLen) throws IOException - Throws:
IOException
-
setLeakListener
- Parameters:
leakListener- If leakListener is not null, it will be notified once a ByteBuf leak is detected.
-