Class ByteBufUtil


  • public final class ByteBufUtil
    extends java.lang.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 int compare​(ByteBuf bufferA, ByteBuf bufferB)
      Compares the two specified buffers as described in ByteBuf.compareTo(ByteBuf).
      static byte decodeHexByte​(java.lang.CharSequence s, int pos)
      Decode a 2-digit hex byte from within a string.
      static ByteBuf encodeString​(java.nio.CharBuffer src, java.nio.charset.Charset charset)
      Encode the given CharBuffer using the given Charset into a new ByteBuf which is allocated via the ByteBufAllocator.
      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 ChannelBuffer#equals(Object).
      static int hashCode​(ByteBuf buffer)
      Calculates the hash code of the specified buffer.
      static int indexOf​(ByteBuf buffer, int fromIndex, int toIndex, byte value)
      The default implementation of ByteBuf.indexOf(int, int, byte).
      static boolean isText​(ByteBuf buf, int index, int length, java.nio.charset.Charset charset)
      Returns true if the specified ByteBuf starting at index with length is valid text using the given Charset, otherwise return false.
      static boolean isText​(ByteBuf buf, java.nio.charset.Charset charset)
      Returns true if the given ByteBuf is valid text using the given Charset, otherwise return false.
      static ByteBuf readBytes​(ByteBuf buffer, int length)
      Read the given amount of bytes into a new ByteBuf that is allocated from the ByteBufAllocator.
      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 writeUtf8​(java.lang.CharSequence seq)
      Encode a CharSequence in UTF-8 and write it to a ByteBuf allocated with alloc.
      static int writeUtf8​(ByteBuf buf, java.lang.CharSequence seq)
      Encode a CharSequence in UTF-8 and write it to a ByteBuf.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • decodeHexByte

        public static byte decodeHexByte​(java.lang.CharSequence s,
                                         int pos)
        Decode a 2-digit hex byte from within a string.
      • 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.
      • equals

        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 ChannelBuffer#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 in ByteBuf.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 of ByteBuf.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.
      • readBytes

        public static ByteBuf readBytes​(ByteBuf buffer,
                                        int length)
        Read the given amount of bytes into a new ByteBuf that is allocated from the ByteBufAllocator.
      • writeUtf8

        public static ByteBuf writeUtf8​(java.lang.CharSequence seq)
        Encode a CharSequence in UTF-8 and write it to a ByteBuf allocated with alloc.
        Parameters:
        alloc - The allocator used to allocate a new ByteBuf.
        seq - The characters to write into a buffer.
        Returns:
        The ByteBuf which contains the UTF-8 encoded result.
      • writeUtf8

        public static int writeUtf8​(ByteBuf buf,
                                    java.lang.CharSequence seq)
        Encode a CharSequence in UTF-8 and write it to a ByteBuf. This method returns the actual number of bytes written.
      • encodeString

        public static ByteBuf encodeString​(java.nio.CharBuffer src,
                                           java.nio.charset.Charset charset)
        Encode the given CharBuffer using the given Charset into a new ByteBuf which is allocated via the ByteBufAllocator.
      • isText

        public static boolean isText​(ByteBuf buf,
                                     java.nio.charset.Charset charset)
        Returns true if the given ByteBuf is valid text using the given Charset, otherwise return false.
        Parameters:
        buf - The given ByteBuf.
        charset - The specified Charset.
      • isText

        public static boolean isText​(ByteBuf buf,
                                     int index,
                                     int length,
                                     java.nio.charset.Charset charset)
        Returns true if the specified ByteBuf starting at index with length is valid text using the given Charset, otherwise return false.
        Parameters:
        buf - The given ByteBuf.
        index - The start index of the specified buffer.
        length - The length of the specified buffer.
        charset - The specified Charset.
        Throws:
        java.lang.IndexOutOfBoundsException - if index + length is greater than buf.readableBytes