Class ByteBufferPool

java.lang.Object
com.xpdustry.claj.common.util.ByteBufferPool

public final class ByteBufferPool extends Object
  • Field Details

  • Constructor Details

    • ByteBufferPool

      public ByteBufferPool()
      Creates a buffer pool with a default factor and bucketCap of 1024.
    • ByteBufferPool

      public ByteBufferPool(int factor, int bucketCap)
  • Method Details

    • get

      public static ByteBufferPool get()
    • getHeap

      public static ByteBuffer getHeap(int size)
    • getDirect

      public static ByteBuffer getDirect(int size)
    • free

      public static boolean free(ByteBuffer buff)
    • getBuckets

      protected ConcurrentHashMap<Integer, Pool<ByteBuffer>> getBuckets(boolean direct)
    • newBuffer

      protected ByteBuffer newBuffer(boolean direct, int capacity)
    • bucketOf

      public int bucketOf(int size)
      Returns:
      the bucket fitting the given size according to the factor.
    • obtain

      public ByteBuffer obtain(int size)
    • obtain

      public ByteBuffer obtain(int size, boolean direct)
      Get a buffer of the given size from free pool, or a new one.
      Capacity is rounded to the upper factor, but limited to the given size.
    • release

      public boolean release(ByteBuffer buf)
      Double release protection not handled!
      Returns:
      whether the buffer was added to the free buffer pool. A buffer might not be added for several reason: a null value, zero capacity, not at the defined factor, or simply because the associated bucket is full.
    • fill

      public void fill(int bucket)
      Fill a bucket completely.
    • fill

      public void fill(int bucket, int size)
    • fill

      public void fill(int bucket, int size, boolean direct)
      Fill a bucket with size new buffers.
    • clear

      public void clear()
      Clear all buffer buckets.
    • clear

      public void clear(boolean direct)
    • size

      public int size(int bucket)
    • size

      public int size(int bucket, boolean direct)
      Returns:
      the number of currently freed buffers in a bucket, or -1 if no pool has been created for this bucket.
    • has

      public boolean has(int bucket)
    • has

      public boolean has(int bucket, boolean direct)
      Returns:
      whether a pool has been created for the specified bucket.