Module 
Package org.xnio

Class ByteBufferPool


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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T,​U,​E extends java.lang.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 java.lang.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.
      java.nio.ByteBuffer allocate()
      Allocate a buffer from this source pool.
      void allocate​(java.nio.ByteBuffer[] array, int offs)
      Bulk-allocate buffers from this pool.
      void allocate​(java.nio.ByteBuffer[] array, int offs, int len)
      Bulk-allocate buffers from this pool.
      <T,​U,​R,​E extends java.lang.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 java.lang.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 flushAllCaches()
      Flush all thread-local caches for all buffer sizes.
      void flushCaches()
      Flush thread-local caches.
      static void free​(java.nio.ByteBuffer buffer)
      Free a buffer into its appropriate pool based on its size.
      static void free​(java.nio.ByteBuffer[] array, int offs, int len)
      Bulk-free buffers from an array as with free(ByteBuffer).
      int getSize()
      Get the size of buffers returned by this source.
      <R,​E extends java.lang.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 isDirect()
      Determine if this source returns direct buffers.
      void runWithCache​(int cacheSize, java.lang.Runnable runnable)
      Perform the given operation with the addition of a buffer cache of the given size.
      <E extends java.lang.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 zeroAndFree​(java.nio.ByteBuffer buffer)
      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 Detail

      • LARGE_SIZE

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

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

        public static final int SMALL_SIZE
        The size of small buffers.
        See Also:
        Constant Field Values
      • 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 Detail

      • allocate

        public java.nio.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​(java.nio.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​(java.nio.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​(java.nio.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​(java.nio.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​(java.nio.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 java.lang.Exception> void acceptWithCacheEx​(int cacheSize,
                                                                                      org.wildfly.common.function.ExceptionBiConsumer<T,​U,​E> consumer,
                                                                                      T param1,
                                                                                      U param2)
                                                                               throws E extends java.lang.Exception
        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
        E extends java.lang.Exception
      • acceptWithCacheEx

        public <T,​E extends java.lang.Exception> void acceptWithCacheEx​(int cacheSize,
                                                                              org.wildfly.common.function.ExceptionConsumer<T,​E> consumer,
                                                                              T param)
                                                                       throws E extends java.lang.Exception
        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
        E extends java.lang.Exception
      • runWithCacheEx

        public <E extends java.lang.Exception> void runWithCacheEx​(int cacheSize,
                                                                   org.wildfly.common.function.ExceptionRunnable<E> runnable)
                                                            throws E extends java.lang.Exception
        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
        E extends java.lang.Exception
      • runWithCache

        public void runWithCache​(int cacheSize,
                                 java.lang.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 java.lang.Exception> R applyWithCacheEx​(int cacheSize,
                                                                                          org.wildfly.common.function.ExceptionBiFunction<T,​U,​R,​E> function,
                                                                                          T param1,
                                                                                          U param2)
                                                                                   throws E extends java.lang.Exception
        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
        E extends java.lang.Exception
      • applyWithCacheEx

        public <T,​R,​E extends java.lang.Exception> R applyWithCacheEx​(int cacheSize,
                                                                                  org.wildfly.common.function.ExceptionFunction<T,​R,​E> function,
                                                                                  T param)
                                                                           throws E extends java.lang.Exception
        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
        E extends java.lang.Exception
      • getWithCacheEx

        public <R,​E extends java.lang.Exception> R getWithCacheEx​(int cacheSize,
                                                                        org.wildfly.common.function.ExceptionSupplier<R,​E> supplier)
                                                                 throws E extends java.lang.Exception
        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
        E extends java.lang.Exception