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 Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      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.
      static void copy​(io.netty5.util.AsciiString src, int srcIdx, io.netty.buffer.ByteBuf dst, int length)
      Copies the content of src to a ByteBuf using ByteBuf.writeBytes(byte[], int, int).
      static void copy​(io.netty5.util.AsciiString src, int srcIdx, io.netty.buffer.ByteBuf dst, int dstIdx, int length)
      Copies the content of src to a ByteBuf using ByteBuf.setBytes(int, byte[], int, int).
      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.
      static byte[] getBytes​(Buffer buf)
      Create a copy of the underlying storage from buf into a byte array.
      static byte[] getBytes​(Buffer buf, int start, int length)
      Create a copy of the underlying storage from buf into a byte array.
      static String hexDump​(byte[] array)
      Returns a hex dump of the specified byte array.
      static String hexDump​(byte[] array, int fromIndex, int length)
      Returns a hex dump of the specified byte array's sub-region.
      static String hexDump​(Buffer buffer)
      Returns a hex dump of the specified buffer's readable bytes.
      static String hexDump​(Buffer buffer, int fromIndex, int length)
      Returns a hex dump of the specified buffer's sub-region.
      static int reverseMedium​(int value)
      Reverses the byte order (endianness) of a medium
      static long reverseUnsignedInt​(long value)
      Reverses the byte order (endianness) of an unsigned integer
      static int reverseUnsignedMedium​(int value)
      Reverses the byte order (endianness) of an unsigned medium
      static int reverseUnsignedShort​(int value)
      Reverses the byte order (endianness) of an unsigned short
      static Buffer threadLocalDirectBuffer()
      Returns a cached thread-local direct buffer, if available.
      static Buffer writeAscii​(BufferAllocator alloc, CharSequence seq)
      Encode a CharSequence in ASCII and write it to a Buffer allocated with alloc.
    • 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.
      • copy

        public static void copy​(io.netty5.util.AsciiString src,
                                int srcIdx,
                                io.netty.buffer.ByteBuf dst,
                                int dstIdx,
                                int length)
        Copies the content of src to a ByteBuf using ByteBuf.setBytes(int, byte[], int, int). Unlike the copy(AsciiString, int, ByteBuf, int) method, this method do not increase a writerIndex of dst buffer.
        Parameters:
        src - the source string to copy
        srcIdx - the starting offset of characters to copy
        dst - the destination buffer
        dstIdx - the starting offset in the destination buffer
        length - the number of characters to copy
      • copy

        public static void copy​(io.netty5.util.AsciiString src,
                                int srcIdx,
                                io.netty.buffer.ByteBuf dst,
                                int length)
        Copies the content of src to a ByteBuf using ByteBuf.writeBytes(byte[], int, int).
        Parameters:
        src - the source string to copy
        srcIdx - the starting offset of characters to copy
        dst - the destination buffer
        length - the number of characters to copy
      • 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.