Package org.xnio
Class ByteBufferPool
java.lang.Object
org.xnio.ByteBufferPool
A fast source of pooled buffers.
- Author:
- David M. Lloyd
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A set of buffer pools for each size, which can either beByteBufferPool.Set.DIRECT
orByteBufferPool.Set.HEAP
. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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 TypeMethodDescription<T,
U, E extends 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 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.allocate()
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>
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 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
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 withfree(ByteBuffer)
.int
getSize()
Get the size of buffers returned by this source.<R,
E extends 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, Runnable runnable) Perform the given operation with the addition of a buffer cache of the given size.<E extends 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
(ByteBuffer buffer) Free a buffer as withfree(ByteBuffer)
except the buffer is first zeroed and cleared.
-
Field Details
-
LARGE_SIZE
public static final int LARGE_SIZEThe size of large buffers.- See Also:
-
MEDIUM_SIZE
public static final int MEDIUM_SIZEThe size of medium buffers.- See Also:
-
SMALL_SIZE
public static final int SMALL_SIZEThe size of small buffers.- See Also:
-
LARGE_DIRECT
The large direct buffer pool. This pool produces buffers ofLARGE_SIZE
. -
MEDIUM_DIRECT
The medium direct buffer pool. This pool produces buffers ofMEDIUM_SIZE
. -
SMALL_DIRECT
The small direct buffer pool. This pool produces buffers ofSMALL_SIZE
. -
LARGE_HEAP
The large heap buffer pool. This pool produces buffers ofLARGE_SIZE
. -
MEDIUM_HEAP
The medium heap buffer pool. This pool produces buffers ofMEDIUM_SIZE
. -
SMALL_HEAP
The small heap buffer pool. This pool produces buffers ofSMALL_SIZE
.
-
-
Method Details
-
allocate
Allocate a buffer from this source pool. The buffer must be freed through thefree(ByteBuffer)
method.- Returns:
- the allocated buffer
-
allocate
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
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
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
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
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, void acceptWithCacheExE extends Exception> (int cacheSize, org.wildfly.common.function.ExceptionBiConsumer<T, U, throws EE> consumer, T param1, U param2) 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 exception
-
acceptWithCacheEx
public <T,E extends Exception> void acceptWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionConsumer<T, E> consumer, T param) throws EPerform 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 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 underrunnable
- the action to run- Throws:
E
- if the nested action threw an exception
-
runWithCache
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 applyWithCacheExR, E extends Exception> (int cacheSize, org.wildfly.common.function.ExceptionBiFunction<T, U, throws ER, E> function, T param1, U param2) 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 exception
-
applyWithCacheEx
public <T,R, R applyWithCacheExE extends Exception> (int cacheSize, org.wildfly.common.function.ExceptionFunction<T, R, throws EE> function, T param) 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 exception
-
getWithCacheEx
public <R,E extends Exception> R getWithCacheEx(int cacheSize, org.wildfly.common.function.ExceptionSupplier<R, E> supplier) throws EPerform 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 exception
-