Module 
Package org.xnio

Class ByteBufferPool

java.lang.Object
org.xnio.ByteBufferPool

public abstract class ByteBufferPool extends Object
A fast source of pooled buffers.
Author:
David M. Lloyd
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    A set of buffer pools for each size, which can either be ByteBufferPool.Set.DIRECT or ByteBufferPool.Set.HEAP.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ByteBufferPool
    The large direct buffer pool.
    static final ByteBufferPool
    The large heap buffer pool.
    static final int
    The size of large buffers.
    static final ByteBufferPool
    The medium direct buffer pool.
    static final ByteBufferPool
    The medium heap buffer pool.
    static final int
    The size of medium buffers.
    static final ByteBufferPool
    The small direct buffer pool.
    static final ByteBufferPool
    The small heap buffer pool.
    static final int
    The size of small buffers.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T, U, E extends Exception>
    void
    acceptWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionBiConsumer<T,U,E> consumer, T param1, U param2)
    Perform the given operation with the addition of a buffer cache of the given size.
    <T, E extends Exception>
    void
    acceptWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionConsumer<T,E> consumer, T param)
    Perform the given operation with the addition of a buffer cache of the given size.
    Allocate a buffer from this source pool.
    void
    allocate(ByteBuffer[] array, int offs)
    Bulk-allocate buffers from this pool.
    void
    allocate(ByteBuffer[] array, int offs, int len)
    Bulk-allocate buffers from this pool.
    <T, U, R, E extends Exception>
    R
    applyWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionBiFunction<T,U,R,E> function, T param1, U param2)
    Perform the given operation with the addition of a buffer cache of the given size.
    <T, R, E extends Exception>
    R
    applyWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionFunction<T,R,E> function, T param)
    Perform the given operation with the addition of a buffer cache of the given size.
    static void
    Flush all thread-local caches for all buffer sizes.
    void
    Flush thread-local caches.
    static void
    free(ByteBuffer buffer)
    Free a buffer into its appropriate pool based on its size.
    static void
    free(ByteBuffer[] array, int offs, int len)
    Bulk-free buffers from an array as with free(ByteBuffer).
    int
    Get the size of buffers returned by this source.
    <R, E extends Exception>
    R
    getWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionSupplier<R,E> supplier)
    Perform the given operation with the addition of a buffer cache of the given size.
    boolean
    Determine if this source returns direct buffers.
    void
    runWithCache(int cacheSize, Runnable runnable)
    Perform the given operation with the addition of a buffer cache of the given size.
    <E extends Exception>
    void
    runWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionRunnable<E> runnable)
    Perform the given operation with the addition of a buffer cache of the given size.
    static void
    Free a buffer as with free(ByteBuffer) except the buffer is first zeroed and cleared.

    Methods inherited from class java.lang.Object

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

    • LARGE_SIZE

      public static final int LARGE_SIZE
      The size of large buffers.
      See Also:
    • MEDIUM_SIZE

      public static final int MEDIUM_SIZE
      The size of medium buffers.
      See Also:
    • SMALL_SIZE

      public static final int SMALL_SIZE
      The size of small buffers.
      See Also:
    • LARGE_DIRECT

      public static final ByteBufferPool LARGE_DIRECT
      The large direct buffer pool. This pool produces buffers of LARGE_SIZE.
    • MEDIUM_DIRECT

      public static final ByteBufferPool MEDIUM_DIRECT
      The medium direct buffer pool. This pool produces buffers of MEDIUM_SIZE.
    • SMALL_DIRECT

      public static final ByteBufferPool SMALL_DIRECT
      The small direct buffer pool. This pool produces buffers of SMALL_SIZE.
    • LARGE_HEAP

      public static final ByteBufferPool LARGE_HEAP
      The large heap buffer pool. This pool produces buffers of LARGE_SIZE.
    • MEDIUM_HEAP

      public static final ByteBufferPool MEDIUM_HEAP
      The medium heap buffer pool. This pool produces buffers of MEDIUM_SIZE.
    • SMALL_HEAP

      public static final ByteBufferPool SMALL_HEAP
      The small heap buffer pool. This pool produces buffers of SMALL_SIZE.
  • Method Details

    • allocate

      public ByteBuffer allocate()
      Allocate a buffer from this source pool. The buffer must be freed through the free(ByteBuffer) method.
      Returns:
      the allocated buffer
    • allocate

      public void allocate(ByteBuffer[] array, int offs)
      Bulk-allocate buffers from this pool. The buffer must be freed through the free(ByteBuffer) method.
      Parameters:
      array - the array of buffers to fill
      offs - the offset into the array to fill
    • allocate

      public void allocate(ByteBuffer[] array, int offs, int len)
      Bulk-allocate buffers from this pool. The buffer must be freed through the free(ByteBuffer) method.
      Parameters:
      array - the array of buffers to fill
      offs - the offset into the array to fill
      len - the number of buffers to fill in the array
    • free

      public static void free(ByteBuffer buffer)
      Free a buffer into its appropriate pool based on its size. Care must be taken to avoid returning a slice of a pooled buffer, since this could cause both the buffer and its slice to be separately repooled, leading to likely data corruption.
      Parameters:
      buffer - the buffer to free
    • free

      public static void free(ByteBuffer[] array, int offs, int len)
      Bulk-free buffers from an array as with free(ByteBuffer). The freed entries will be assigned to null.
      Parameters:
      array - the buffer array
      offs - the offset into the array
      len - the number of buffers to free
    • zeroAndFree

      public static void zeroAndFree(ByteBuffer buffer)
      Free a buffer as with free(ByteBuffer) except the buffer is first zeroed and cleared.
      Parameters:
      buffer - the buffer to free
    • isDirect

      public boolean isDirect()
      Determine if this source returns direct buffers.
      Returns:
      true if the buffers are direct, false if they are heap
    • getSize

      public int getSize()
      Get the size of buffers returned by this source. The size will be a power of two.
      Returns:
      the size of buffers returned by this source
    • flushCaches

      public void flushCaches()
      Flush thread-local caches. This is useful when a long blocking operation is being performed, wherein it is unlikely that buffers will be used; calling this method makes any cached buffers available to other threads.
    • flushAllCaches

      public static void flushAllCaches()
      Flush all thread-local caches for all buffer sizes. This is useful when a long blocking operation is being performed, wherein it is unlikely that buffers will be used; calling this method makes any cached buffers available to other threads.
    • acceptWithCacheEx

      public <T, U, E extends Exception> void acceptWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionBiConsumer<T,U,E> consumer, T param1, U param2) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      T - the type of the first parameter
      U - the type of the second parameter
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      consumer - the action to run
      param1 - the first parameter to pass to the action
      param2 - the second parameter to pass to the action
      Throws:
      E - if the nested action threw an exception
    • acceptWithCacheEx

      public <T, E extends Exception> void acceptWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionConsumer<T,E> consumer, T param) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      T - the type of the parameter
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      consumer - the action to run
      param - the parameter to pass to the action
      Throws:
      E - if the nested action threw an exception
    • runWithCacheEx

      public <E extends Exception> void runWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionRunnable<E> runnable) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      runnable - the action to run
      Throws:
      E - if the nested action threw an exception
    • runWithCache

      public void runWithCache(int cacheSize, Runnable runnable)
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Parameters:
      cacheSize - the cache size to run under
      runnable - the action to run
    • applyWithCacheEx

      public <T, U, R, E extends Exception> R applyWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionBiFunction<T,U,R,E> function, T param1, U param2) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      T - the type of the first parameter
      U - the type of the second parameter
      R - the return type of the operation
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      function - the action to run
      param1 - the first parameter to pass to the action
      param2 - the second parameter to pass to the action
      Returns:
      the result of the action
      Throws:
      E - if the nested action threw an exception
    • applyWithCacheEx

      public <T, R, E extends Exception> R applyWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionFunction<T,R,E> function, T param) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      T - the type of the parameter
      R - the return type of the operation
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      function - the action to run
      param - the parameter to pass to the action
      Returns:
      the result of the action
      Throws:
      E - if the nested action threw an exception
    • getWithCacheEx

      public <R, E extends Exception> R getWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionSupplier<R,E> supplier) throws E
      Perform the given operation with the addition of a buffer cache of the given size. When this method returns, any cached free buffers will be returned to the next-higher cache or the global pool. If a cache size of 0 is given, the action is simply run directly.
      Type Parameters:
      R - the return type of the operation
      E - the exception type thrown by the operation
      Parameters:
      cacheSize - the cache size to run under
      supplier - the action to run
      Returns:
      the result of the action
      Throws:
      E - if the nested action threw an exception