Class Buffers


  • public class Buffers
    extends java.lang.Object
    Class has useful methods to simplify the work with Buffers.
    Author:
    Alexey Stashok
    See Also:
    Buffer
    • Constructor Summary

      Constructors 
      Constructor Description
      Buffers()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Buffer appendBuffers​(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2)
      Append two Buffers.
      static Buffer appendBuffers​(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2, boolean isCompositeBufferDisposable)
      Append two Buffers.
      static Buffer cloneBuffer​(Buffer srcBuffer)
      Clones the source Buffer.
      static Buffer cloneBuffer​(Buffer srcBuffer, int position, int limit)
      Clones the source Buffer.
      static void dumpBuffer​(java.lang.Appendable appendable, Buffer buffer)
      Generates a hex dump of the provided Buffer.
      static void fill​(java.nio.ByteBuffer byteBuffer, byte b)
      Fill the ByteBuffer with the specific byte value.
      static void fill​(java.nio.ByteBuffer byteBuffer, int position, int limit, byte b)
      Fill the ByteBuffer's part [position, limit) with the specific byte value starting from the ByteBuffer's position won't be changed.
      static void fill​(Buffer buffer, byte b)
      Fill the Buffer with the specific byte value.
      static void fill​(Buffer buffer, int position, int limit, byte b)
      Fill the Buffer's part [position, limit) with the specific byte value starting from the Buffer's position won't be changed.
      static void get​(java.nio.ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)  
      static Appender<Buffer> getBufferAppender​(boolean isCompositeBufferDisposable)
      Get the Appender which knows how to append Buffers.
      static void put​(byte[] srcBytes, int srcOffset, int length, java.nio.ByteBuffer dstBuffer)  
      static void put​(java.nio.ByteBuffer srcBuffer, int srcOffset, int length, java.nio.ByteBuffer dstBuffer)  
      static void put​(Buffer src, int position, int length, Buffer dstBuffer)  
      static long readFromFileChannel​(java.nio.channels.FileChannel fileChannel, Buffer buffer)
      Reads data from the FileChannel into the Buffer.
      static void setPositionLimit​(java.nio.ByteBuffer buffer, int position, int limit)  
      static void setPositionLimit​(Buffer buffer, int position, int limit)  
      static java.nio.ByteBuffer slice​(java.nio.ByteBuffer chunk, int size)
      Slice ByteBuffer of required size from big chunk.
      static java.nio.ByteBuffer slice​(java.nio.ByteBuffer byteBuffer, int position, int limit)
      Get the ByteBuffer's slice basing on its passed position and limit.
      static java.lang.String toStringContent​(java.nio.ByteBuffer byteBuffer, java.nio.charset.Charset charset, int position, int limit)  
      java.lang.String toStringContent​(Buffer buffer, int headBytesCount, int tailBytesCount)
      Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:
      java.lang.String toStringContent​(Buffer buffer, int headBytesCount, int tailBytesCount, java.nio.charset.Charset charset)
      Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:
      static Buffer wrap​(MemoryManager memoryManager, byte[] array)
      Returns Buffer, which wraps the byte array.
      static Buffer wrap​(MemoryManager memoryManager, byte[] array, int offset, int length)
      Returns Buffer, which wraps the part of byte array with specific offset and length.
      static Buffer wrap​(MemoryManager memoryManager, java.lang.String s)
      Returns Buffer, which wraps the String.
      static Buffer wrap​(MemoryManager memoryManager, java.lang.String s, java.nio.charset.Charset charset)
      Returns Buffer, which wraps the String with the specific Charset.
      static Buffer wrap​(MemoryManager memoryManager, java.nio.ByteBuffer byteBuffer)
      Returns Buffer, which wraps the ByteBuffer.
      static long writeToFileChannel​(java.nio.channels.FileChannel fileChannel, Buffer buffer)
      Writes data from the Buffer into the FileChannel.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_BYTE_BUFFER

        public static final java.nio.ByteBuffer EMPTY_BYTE_BUFFER
      • EMPTY_BYTE_BUFFER_ARRAY

        public static final java.nio.ByteBuffer[] EMPTY_BYTE_BUFFER_ARRAY
      • EMPTY_BUFFER

        public static final Buffer EMPTY_BUFFER
    • Constructor Detail

      • Buffers

        public Buffers()
    • Method Detail

      • wrap

        public static Buffer wrap​(MemoryManager memoryManager,
                                  java.lang.String s)
        Returns Buffer, which wraps the String.
        Parameters:
        memoryManager - MemoryManager, which should be used for wrapping.
        s - String
        Returns:
        Buffer wrapper on top of passed String.
      • wrap

        public static Buffer wrap​(MemoryManager memoryManager,
                                  java.lang.String s,
                                  java.nio.charset.Charset charset)
        Returns Buffer, which wraps the String with the specific Charset.
        Parameters:
        memoryManager - MemoryManager, which should be used for wrapping.
        s - String
        charset - Charset, which will be used, when converting String to byte array.
        Returns:
        Buffer wrapper on top of passed String.
      • wrap

        public static Buffer wrap​(MemoryManager memoryManager,
                                  byte[] array)
        Returns Buffer, which wraps the byte array.
        Parameters:
        memoryManager - MemoryManager, which should be used for wrapping.
        array - byte array to wrap.
        Returns:
        Buffer wrapper on top of passed byte array.
      • wrap

        public static Buffer wrap​(MemoryManager memoryManager,
                                  byte[] array,
                                  int offset,
                                  int length)
        Returns Buffer, which wraps the part of byte array with specific offset and length.
        Parameters:
        memoryManager - MemoryManager, which should be used for wrapping.
        array - byte array to wrap
        offset - byte buffer offset
        length - byte buffer length
        Returns:
        Buffer wrapper on top of passed byte array.
      • wrap

        public static Buffer wrap​(MemoryManager memoryManager,
                                  java.nio.ByteBuffer byteBuffer)
        Returns Buffer, which wraps the ByteBuffer.
        Parameters:
        memoryManager - MemoryManager, which should be used for wrapping.
        byteBuffer - ByteBuffer to wrap
        Returns:
        Buffer wrapper on top of passed ByteBuffer.
      • slice

        public static java.nio.ByteBuffer slice​(java.nio.ByteBuffer chunk,
                                                int size)
        Slice ByteBuffer of required size from big chunk. Passed chunk position will be changed, after the slicing (chunk.position += size).
        Parameters:
        chunk - big ByteBuffer pool.
        size - required slice size.
        Returns:
        sliced ByteBuffer of required size.
      • slice

        public static java.nio.ByteBuffer slice​(java.nio.ByteBuffer byteBuffer,
                                                int position,
                                                int limit)
        Get the ByteBuffer's slice basing on its passed position and limit. Position and limit values of the passed ByteBuffer won't be changed. The result ByteBuffer position will be equal to 0, and limit equal to number of sliced bytes (limit - position).
        Parameters:
        byteBuffer - ByteBuffer to slice/
        position - the position in the passed byteBuffer, the slice will start from.
        limit - the limit in the passed byteBuffer, the slice will be ended.
        Returns:
        sliced ByteBuffer of required size.
      • toStringContent

        public static java.lang.String toStringContent​(java.nio.ByteBuffer byteBuffer,
                                                       java.nio.charset.Charset charset,
                                                       int position,
                                                       int limit)
      • setPositionLimit

        public static void setPositionLimit​(Buffer buffer,
                                            int position,
                                            int limit)
      • setPositionLimit

        public static void setPositionLimit​(java.nio.ByteBuffer buffer,
                                            int position,
                                            int limit)
      • put

        public static void put​(java.nio.ByteBuffer srcBuffer,
                               int srcOffset,
                               int length,
                               java.nio.ByteBuffer dstBuffer)
      • put

        public static void put​(Buffer src,
                               int position,
                               int length,
                               Buffer dstBuffer)
      • get

        public static void get​(java.nio.ByteBuffer srcBuffer,
                               byte[] dstBytes,
                               int dstOffset,
                               int length)
      • put

        public static void put​(byte[] srcBytes,
                               int srcOffset,
                               int length,
                               java.nio.ByteBuffer dstBuffer)
      • fill

        public static void fill​(Buffer buffer,
                                byte b)
        Fill the Buffer with the specific byte value. Buffer's position won't be changed.
        Parameters:
        buffer - Buffer
        b - value
      • fill

        public static void fill​(Buffer buffer,
                                int position,
                                int limit,
                                byte b)
        Fill the Buffer's part [position, limit) with the specific byte value starting from the Buffer's position won't be changed.
        Parameters:
        buffer - Buffer
        position - Buffer position to start with (inclusive)
        limit - Buffer limit, where filling ends (exclusive)
        b - value
      • fill

        public static void fill​(java.nio.ByteBuffer byteBuffer,
                                byte b)
        Fill the ByteBuffer with the specific byte value. ByteBuffer's position won't be changed.
        Parameters:
        byteBuffer - ByteBuffer
        b - value
      • fill

        public static void fill​(java.nio.ByteBuffer byteBuffer,
                                int position,
                                int limit,
                                byte b)
        Fill the ByteBuffer's part [position, limit) with the specific byte value starting from the ByteBuffer's position won't be changed.
        Parameters:
        byteBuffer - ByteBuffer
        position - ByteBuffer position to start with (inclusive)
        limit - Buffer limit, where filling ends (exclusive)
        b - value
      • cloneBuffer

        public static Buffer cloneBuffer​(Buffer srcBuffer)
        Clones the source Buffer. The method returns a new Buffer instance, which has the same content. Please note, source and result Buffers have the same content, but it is *not* shared, so the following content changes in one of the Buffers won't be visible in another one.
        Parameters:
        srcBuffer - the source Buffer.
        Returns:
        the cloned Buffer.
      • cloneBuffer

        public static Buffer cloneBuffer​(Buffer srcBuffer,
                                         int position,
                                         int limit)
        Clones the source Buffer. The method returns a new Buffer instance, which has the same content. Please note, source and result Buffers have the same content, but it is *not* shared, so the following content changes in one of the Buffers won't be visible in another one.
        Parameters:
        srcBuffer - the source Buffer.
        position - the start position in the srcBuffer
        limit - the end position in the srcBuffer
        Returns:
        the cloned Buffer.
      • readFromFileChannel

        public static long readFromFileChannel​(java.nio.channels.FileChannel fileChannel,
                                               Buffer buffer)
                                        throws java.io.IOException
        Reads data from the FileChannel into the Buffer.
        Parameters:
        fileChannel - the FileChannel to read data from.
        buffer - the destination Buffer.
        Returns:
        the number of bytes read, or -1 if the end of file is reached.
        Throws:
        IOExceptionif - an error occurs reading the FileChannel.
        java.io.IOException
      • writeToFileChannel

        public static long writeToFileChannel​(java.nio.channels.FileChannel fileChannel,
                                              Buffer buffer)
                                       throws java.io.IOException
        Writes data from the Buffer into the FileChannel.
        Parameters:
        fileChannel - the FileChannel to write data to.
        buffer - the source Buffer.
        Returns:
        the number of bytes written, possibly zero.
        Throws:
        IOExceptionif - an error occurs writing to the FileChannel.
        java.io.IOException
      • toStringContent

        public java.lang.String toStringContent​(Buffer buffer,
                                                int headBytesCount,
                                                int tailBytesCount)
        Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:
         HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
         
        Parameters:
        buffer - the Buffer, could be null
        headBytesCount - the number of heading bytes to include (larger or equal to 0)
        tailBytesCount - the number of tailing bytes to include (larger or equal to 0)
        Returns:
        the Buffer's String representation, or null, if the Buffer is null
      • toStringContent

        public java.lang.String toStringContent​(Buffer buffer,
                                                int headBytesCount,
                                                int tailBytesCount,
                                                java.nio.charset.Charset charset)
        Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:
         HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
         
        Parameters:
        buffer - the Buffer, could be null
        headBytesCount - the number of heading bytes to include (larger or equal to 0)
        tailBytesCount - the number of tailing bytes to include (larger or equal to 0)
        charset - Charset, if null the Charset.defaultCharset() will be used
        Returns:
        the Buffer's String representation, or null, if the Buffer is null
      • dumpBuffer

        public static void dumpBuffer​(java.lang.Appendable appendable,
                                      Buffer buffer)
        Generates a hex dump of the provided Buffer.
        Parameters:
        appendable - the Appendable to write the hex dump to.
        buffer - the Buffer to dump.
        Since:
        2.3.23