Class SimpleDirectBufferAllocator

  • All Implemented Interfaces:
    IDirectBufferAllocator

    public class SimpleDirectBufferAllocator
    extends java.lang.Object
    implements IDirectBufferAllocator
    A simple buffer allocator helps avoid unnecessary memory reallocations, thereby improving performance and reducing power consumption.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.nio.ByteBuffer allocateBuffer​(int capacity)
      The method returns a byte buffer of the required size from the buffer queue, or allocates a new buffer and returns it.
      void clear()
      Clear the buffers queue
      void retainBuffer​(java.nio.ByteBuffer buffer)
      Retain a buffer to the buffers queue.
      • Methods inherited from class java.lang.Object

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

      • SimpleDirectBufferAllocator

        public SimpleDirectBufferAllocator()
        Create a SimpleDirectBufferAllocator with default queue capacity - 3
      • SimpleDirectBufferAllocator

        public SimpleDirectBufferAllocator​(int capacity)
        Create a SimpleDirectBufferAllocator with specified queue capacity. Important! capacity - should not be too large, otherwise the RAM may run out after a few iterations of use.
        Parameters:
        capacity - - queue size
    • Method Detail

      • allocateBuffer

        @NonNull
        public java.nio.ByteBuffer allocateBuffer​(int capacity)
        The method returns a byte buffer of the required size from the buffer queue, or allocates a new buffer and returns it.
        Specified by:
        allocateBuffer in interface IDirectBufferAllocator
        Parameters:
        capacity - - required buffer size in bytes
        Returns:
        a new ByteBuffer or ByteBuffer from a queue with the specified size
      • retainBuffer

        public void retainBuffer​(@NonNull
                                 java.nio.ByteBuffer buffer)
        Retain a buffer to the buffers queue. This method should be called when the memory buffer is no longer needed for use.
        Parameters:
        buffer - - a buffer that needs to be returned back to the queue
      • clear

        public void clear()
        Clear the buffers queue