Class BufferUtils


  • public final class BufferUtils
    extends Object
    • Method Detail

      • clearAndEnsureCapacity

        public static ByteBuffer clearAndEnsureCapacity​(ByteBuffer buffer,
                                                        int elements)
        Ensure the buffer's capacity is large enough to hold a given number of elements. If the input buffer is not large enough, a new buffer is allocated and returned.
        Parameters:
        elements - The required number of elements to be appended to the buffer.
        buffer - The buffer to check or null if a new buffer should be allocated.
        Returns:
        Returns the same buffer or a new buffer with the given capacity.
      • clearAndEnsureCapacity

        public static CharBuffer clearAndEnsureCapacity​(CharBuffer buffer,
                                                        int elements)
        Ensure the buffer's capacity is large enough to hold a given number of elements. If the input buffer is not large enough, a new buffer is allocated and returned.
        Parameters:
        elements - The required number of elements to be appended to the buffer.
        buffer - The buffer to check or null if a new buffer should be allocated.
        Returns:
        Returns the same buffer or a new buffer with the given capacity.
      • toString

        public static String toString​(ByteBuffer buffer,
                                      Charset charset)
        Parameters:
        buffer - The buffer to convert to a string.
        charset - The charset to use when converting bytes to characters.
        Returns:
        A string representation of buffer's content.
      • toArray

        public static byte[] toArray​(ByteBuffer buffer)
        Parameters:
        buffer - The buffer to read from.
        Returns:
        Returns the remaining bytes from the buffer copied to an array.
      • bytesToChars

        public static CharBuffer bytesToChars​(CharsetDecoder decoder,
                                              ByteBuffer bytes,
                                              CharBuffer chars)
        Convert byte buffer's content into characters. The input buffer's bytes are not consumed (mark is set and reset).