- java.lang.Object
-
- org.xnio.ByteBufferPool
-
public abstract class ByteBufferPool extends java.lang.Object
A fast source of pooled buffers.- Author:
- David M. Lloyd
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ByteBufferPool.Set
A set of buffer pools for each size, which can either beByteBufferPool.Set.DIRECT
orByteBufferPool.Set.HEAP
.
-
Field Summary
Fields Modifier and Type Field Description static ByteBufferPool
LARGE_DIRECT
The large direct buffer pool.static ByteBufferPool
LARGE_HEAP
The large heap buffer pool.static int
LARGE_SIZE
The size of large buffers.static ByteBufferPool
MEDIUM_DIRECT
The medium direct buffer pool.static ByteBufferPool
MEDIUM_HEAP
The medium heap buffer pool.static int
MEDIUM_SIZE
The size of medium buffers.static ByteBufferPool
SMALL_DIRECT
The small direct buffer pool.static ByteBufferPool
SMALL_HEAP
The small heap buffer pool.static int
SMALL_SIZE
The size of small buffers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T,U,E extends java.lang.Exception>
voidacceptWithCacheEx(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>
voidacceptWithCacheEx(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>
RapplyWithCacheEx(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>
RapplyWithCacheEx(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 withfree(ByteBuffer)
.int
getSize()
Get the size of buffers returned by this source.<R,E extends java.lang.Exception>
RgetWithCacheEx(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>
voidrunWithCacheEx(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 withfree(ByteBuffer)
except the buffer is first zeroed and cleared.
-
-
-
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 ofLARGE_SIZE
.
-
MEDIUM_DIRECT
public static final ByteBufferPool MEDIUM_DIRECT
The medium direct buffer pool. This pool produces buffers ofMEDIUM_SIZE
.
-
SMALL_DIRECT
public static final ByteBufferPool SMALL_DIRECT
The small direct buffer pool. This pool produces buffers ofSMALL_SIZE
.
-
LARGE_HEAP
public static final ByteBufferPool LARGE_HEAP
The large heap buffer pool. This pool produces buffers ofLARGE_SIZE
.
-
MEDIUM_HEAP
public static final ByteBufferPool MEDIUM_HEAP
The medium heap buffer pool. This pool produces buffers ofMEDIUM_SIZE
.
-
SMALL_HEAP
public static final ByteBufferPool SMALL_HEAP
The small heap buffer pool. This pool produces buffers ofSMALL_SIZE
.
-
-
Method Detail
-
allocate
public java.nio.ByteBuffer allocate()
Allocate a buffer from this source pool. The buffer must be freed through thefree(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 thefree(ByteBuffer)
method.- Parameters:
array
- the array of buffers to filloffs
- 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 thefree(ByteBuffer)
method.- Parameters:
array
- the array of buffers to filloffs
- the offset into the array to filllen
- 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 withfree(ByteBuffer)
. The freed entries will be assigned tonull
.- Parameters:
array
- the buffer arrayoffs
- the offset into the arraylen
- the number of buffers to free
-
zeroAndFree
public static void zeroAndFree(java.nio.ByteBuffer buffer)
Free a buffer as withfree(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 parameterU
- the type of the second parameterE
- the exception type thrown by the operation- Parameters:
cacheSize
- the cache size to run underconsumer
- the action to runparam1
- the first parameter to pass to the actionparam2
- the second parameter to pass to the action- Throws:
E
- if the nested action threw an exceptionE 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 parameterE
- the exception type thrown by the operation- Parameters:
cacheSize
- the cache size to run underconsumer
- the action to runparam
- the parameter to pass to the action- Throws:
E
- if the nested action threw an exceptionE 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 underrunnable
- the action to run- Throws:
E
- if the nested action threw an exceptionE 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 underrunnable
- 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 parameterU
- the type of the second parameterR
- the return type of the operationE
- the exception type thrown by the operation- Parameters:
cacheSize
- the cache size to run underfunction
- the action to runparam1
- the first parameter to pass to the actionparam2
- the second parameter to pass to the action- Returns:
- the result of the action
- Throws:
E
- if the nested action threw an exceptionE 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 parameterR
- the return type of the operationE
- the exception type thrown by the operation- Parameters:
cacheSize
- the cache size to run underfunction
- the action to runparam
- the parameter to pass to the action- Returns:
- the result of the action
- Throws:
E
- if the nested action threw an exceptionE 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 operationE
- the exception type thrown by the operation- Parameters:
cacheSize
- the cache size to run undersupplier
- the action to run- Returns:
- the result of the action
- Throws:
E
- if the nested action threw an exceptionE extends java.lang.Exception
-
-