Class ByteBufferUtils


  • public final class ByteBufferUtils
    extends Object
    Utility methods to help working with ByteBuffers.
    Since:
    3.0
    • Field Detail

      • EMPTY_BUFFER

        public static final ByteBuffer EMPTY_BUFFER
        A handy constant to use for no-op send/receive calls.
    • Method Detail

      • put

        public static void put​(ByteBuffer src,
                               ByteBuffer dst)
        Transfer bytes from src to dst. If the source has more bytes than the destination then only as many bytes as the destination has capacity to take will be transferred.
        Parameters:
        src - the source.
        dst - the destination.
      • putUTF8

        public static void putUTF8​(String src,
                                   ByteBuffer dst)
        Transfer a string into the destination buffer as a UTF-8 encoded string prefixed by a two byte length.
        Parameters:
        src - the string to encode and copy.
        dst - the destination.
      • getUTF8

        public static String getUTF8​(ByteBuffer src)
        Read from the source buffer a string that has been encoded in UTF-8 and prefixed by a two byte length.
        Parameters:
        src - the source buffer.
        Returns:
        the string.
        Throws:
        BufferUnderflowException - if the source buffer has not got the full string.
      • wrapUTF8

        public static ByteBuffer wrapUTF8​(String string)
        Wrap a string encoded as a UTF-8 string prefixed by a two byte length.
        Parameters:
        string - the string.
        Returns:
        a ByteBuffer containing the two byte length followed by the string encoded as UTF-8.
      • accumulate

        public static ByteBuffer accumulate​(ByteBuffer src,
                                            ByteBuffer dst)
        Accumulate the given buffer into the current context. Allocation is performed only if needed.
        Parameters:
        src - the buffer to accumulate
        dst - the buffer to accumulate into
        Returns:
        the accumulated buffer (may be dst or a new buffer if dst did not have sufficient capacity)
      • duplicate

        public static ByteBuffer duplicate​(ByteBuffer buffer)
        Duplicate a byte buffer for storing for future use.
        Parameters:
        buffer - the buffer to duplicate
        Returns:
        the newly allocated buffer