Class ByteBufferPool

java.lang.Object
com.lucimber.dbus.util.ByteBufferPool

public final class ByteBufferPool extends Object
A thread-safe ByteBuffer pool to reduce garbage collection pressure by reusing ByteBuffer instances for encoding/decoding operations.

This pool maintains separate queues for different buffer sizes, automatically sizing up to the nearest power of two to improve reuse efficiency. Buffers are cleaned before being returned to ensure data integrity.

The pool has built-in size limits to prevent unbounded memory growth under high load scenarios.

  • Constructor Details

    • ByteBufferPool

      public ByteBufferPool()
      Creates a new ByteBuffer pool instance.
  • Method Details

    • acquire

      public ByteBuffer acquire(int capacity, ByteOrder order)
      Acquires a ByteBuffer with at least the requested capacity. The returned buffer will have its position at 0, limit at capacity, and will be cleared of any previous data.
      Parameters:
      capacity - minimum required capacity
      order - byte order for the buffer
      Returns:
      a ByteBuffer ready for use
    • release

      public void release(ByteBuffer buffer)
      Returns a ByteBuffer to the pool for potential reuse. The buffer should not be used after calling this method.
      Parameters:
      buffer - the buffer to return (may be null)
    • getStatistics

      public String getStatistics()
      Gets statistics about the current pool state. Useful for monitoring and debugging memory usage.
      Returns:
      a string representation of pool statistics
    • clear

      public void clear()
      Clears all pooled buffers, releasing memory back to the system. Useful for cleanup or testing scenarios.