Class BufferUtil


  • public final class BufferUtil
    extends Object
    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.
    • Method Detail

      • reverseUnsignedShort

        public static int reverseUnsignedShort​(int value)
        Reverses the byte order (endianness) of an unsigned short
        Parameters:
        value - the number with original byte order
        Returns:
        the number with reversed byte order
      • reverseMedium

        public static int reverseMedium​(int value)
        Reverses the byte order (endianness) of a medium
        Parameters:
        value - the number with original byte order
        Returns:
        the number with reversed byte order
      • reverseUnsignedMedium

        public static int reverseUnsignedMedium​(int value)
        Reverses the byte order (endianness) of an unsigned medium
        Parameters:
        value - the number with original byte order
        Returns:
        the number with reversed byte order
      • reverseUnsignedInt

        public static long reverseUnsignedInt​(long value)
        Reverses the byte order (endianness) of an unsigned integer
        Parameters:
        value - the number with original byte order
        Returns:
        the number with reversed byte order
      • hexDump

        public static String hexDump​(Buffer buffer)
        Returns a hex dump of the specified buffer's readable bytes.
      • hexDump

        public static String hexDump​(Buffer buffer,
                                     int fromIndex,
                                     int length)
        Returns a hex dump of the specified buffer's sub-region.
      • hexDump

        public static String hexDump​(byte[] array)
        Returns a hex dump of the specified byte array.
      • hexDump

        public static String hexDump​(byte[] array,
                                     int fromIndex,
                                     int length)
        Returns a hex dump of the specified byte array's sub-region.
      • getBytes

        public static byte[] getBytes​(Buffer buf,
                                      int start,
                                      int length)
        Create a copy of the underlying storage from buf into a byte array. The copy will start at start and copy length bytes.
      • equals

        public static boolean equals​(Buffer first,
                                     int firstReaderOffset,
                                     Buffer second,
                                     int secondReaderOffset,
                                     int length)
        Returns true if and only if the two specified buffers are identical to each other for length bytes starting at firstReaderOffset index for the first buffer and secondReaderOffset index for the second buffer. A more compact way to express this is:

        first[firstRoff : firstRoff + length] == second[secondRoff : secondRoff + length]

      • appendPrettyHexDump

        public static void appendPrettyHexDump​(StringBuilder dump,
                                               Buffer buf)
        Appends the prettified multi-line hexadecimal dump of the specified Buffer to the specified StringBuilder that is easy to read by humans.
      • appendPrettyHexDump

        public static void appendPrettyHexDump​(StringBuilder dump,
                                               Buffer buf,
                                               int offset,
                                               int length)
        Appends the prettified multi-line hexadecimal dump of the specified Buffer to the specified StringBuilder that is easy to read by humans, starting at the given offset using the given length.
      • threadLocalDirectBuffer

        public static Buffer threadLocalDirectBuffer()
        Returns a cached thread-local direct buffer, if available.
        Returns:
        a cached thread-local direct buffer, if available. null otherwise.