Top Description Inners Fields Constructors Methods
org.xnio

public abstract Class ByteBufferPool

extends Object
Class Inheritance
Static Imports
java.lang.Math.max

A fast source of pooled buffers.
Author
David M. Lloyd

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
pack-priv class
pack-priv static class
pack-priv static class
public static class
ByteBufferPool.Set

A set of buffer pools for each size, which can either be DIRECT or HEAP.

pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final int
private final ByteBufferPool.Cache
private final boolean
public static final ByteBufferPool
LARGE_DIRECT

The large direct buffer pool.

public static final ByteBufferPool
LARGE_HEAP

The large heap buffer pool.

public static final int
LARGE_SIZE

The size of large buffers.

private final ConcurrentLinkedQueue<ByteBuffer>
public static final ByteBufferPool
MEDIUM_DIRECT

The medium direct buffer pool.

public static final ByteBufferPool
MEDIUM_HEAP

The medium heap buffer pool.

public static final int
MEDIUM_SIZE

The size of medium buffers.

private final int
private static final boolean
public static final ByteBufferPool
SMALL_DIRECT

The small direct buffer pool.

public static final ByteBufferPool
SMALL_HEAP

The small heap buffer pool.

public static final int
SMALL_SIZE

The size of small buffers.

private final ThreadLocal<ByteBufferPool.Cache>

Constructor Summary

AccessConstructor and Description
pack-priv
ByteBufferPool(final int size, final boolean direct)

Method Summary

Modifier and TypeMethod and Description
public <
the type of the first parameter
T
,
the type of the second parameter
U
,
the exception type thrown by the operation
E extends Exception
>
void
acceptWithCacheEx(int
the cache size to run under
cacheSize
,
ExceptionBiConsumer<T, U, E>
the action to run
consumer
,
T
the first parameter to pass to the action
param1
,
U
the second parameter to pass to the action
param2
)

Perform the given operation with the addition of a buffer cache of the given size.

public <
the type of the parameter
T
,
the exception type thrown by the operation
E extends Exception
>
void
acceptWithCacheEx(int
the cache size to run under
cacheSize
,
ExceptionConsumer<T, E>
the action to run
consumer
,
T
the parameter to pass to the action
param
)

Perform the given operation with the addition of a buffer cache of the given size.

public ByteBuffer

Returns:

the allocated buffer
allocate
()

Allocate a buffer from this source pool.

public void
allocate(ByteBuffer[]
the array of buffers to fill
array
,
int
the offset into the array to fill
offs
)

Bulk-allocate buffers from this pool.

public void
allocate(ByteBuffer[]
the array of buffers to fill
array
,
int
the offset into the array to fill
offs
,
int
the number of buffers to fill in the array
len
)

Bulk-allocate buffers from this pool.

private ByteBuffer
public <
the type of the first parameter
T
,
the type of the second parameter
U
,
the return type of the operation
R
,
the exception type thrown by the operation
E extends Exception
>
R

Returns:

the result of the action
applyWithCacheEx
(int
the cache size to run under
cacheSize
,
ExceptionBiFunction<T, U, R, E>
the action to run
function
,
T
the first parameter to pass to the action
param1
,
U
the second parameter to pass to the action
param2
)

Perform the given operation with the addition of a buffer cache of the given size.

public <
the type of the parameter
T
,
the return type of the operation
R
,
the exception type thrown by the operation
E extends Exception
>
R

Returns:

the result of the action
applyWithCacheEx
(int
the cache size to run under
cacheSize
,
ExceptionFunction<T, R, E>
the action to run
function
,
T
the parameter to pass to the action
param
)

Perform the given operation with the addition of a buffer cache of the given size.

pack-priv static ByteBufferPool
create(final int size, final boolean direct)

pack-priv abstract ByteBuffer
pack-priv final void
doFree(final ByteBuffer buffer)

public static void
flushAllCaches()

Flush all thread-local caches for all buffer sizes.

public void
flushCaches()

Flush thread-local caches.

public static void
free(ByteBuffer
the buffer to free
buffer
)

Free a buffer into its appropriate pool based on its size.

public static void
free(ByteBuffer[]
the buffer array
array
,
int
the offset into the array
offs
,
int
the number of buffers to free
len
)

Bulk-free buffers from an array as with free(ByteBuffer).

pack-priv final void
pack-priv ByteBufferPool.Cache
pack-priv ConcurrentLinkedQueue<ByteBuffer>
public int

Returns:

the size of buffers returned by this source
getSize
()

Get the size of buffers returned by this source.

public <
the return type of the operation
R
,
the exception type thrown by the operation
E extends Exception
>
R

Returns:

the result of the action
getWithCacheEx
(int
the cache size to run under
cacheSize
,
ExceptionSupplier<R, E>
the action to run
supplier
)

Perform the given operation with the addition of a buffer cache of the given size.

public boolean

Returns:

true if the buffers are direct, false if they are heap
isDirect
()

Determine if this source returns direct buffers.

public void
runWithCache(int
the cache size to run under
cacheSize
,
Runnable
the action to run
runnable
)

Perform the given operation with the addition of a buffer cache of the given size.

public <
the exception type thrown by the operation
E extends Exception
>
void
runWithCacheEx(int
the cache size to run under
cacheSize
,
ExceptionRunnable<E>
the action to run
runnable
)

Perform the given operation with the addition of a buffer cache of the given size.

pack-priv static ByteBufferPool
subPool(final ByteBufferPool parent, final int size)

public static void
zeroAndFree(ByteBuffer
the buffer to free
buffer
)

Free a buffer as with free(ByteBuffer) except the buffer is first zeroed and cleared.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CACHE_LINE_SIZEback to summary
pack-priv static final int CACHE_LINE_SIZE
defaultCacheback to summary
private final ByteBufferPool.Cache defaultCache
directback to summary
private final boolean direct
LARGE_DIRECTback to summary
public static final ByteBufferPool LARGE_DIRECT

The large direct buffer pool. This pool produces buffers of LARGE_SIZE.

LARGE_HEAPback to summary
public static final ByteBufferPool LARGE_HEAP

The large heap buffer pool. This pool produces buffers of LARGE_SIZE.

LARGE_SIZEback to summary
public static final int LARGE_SIZE

The size of large buffers.

masterQueueback to summary
private final ConcurrentLinkedQueue<ByteBuffer> masterQueue
MEDIUM_DIRECTback to summary
public static final ByteBufferPool MEDIUM_DIRECT

The medium direct buffer pool. This pool produces buffers of MEDIUM_SIZE.

MEDIUM_HEAPback to summary
public static final ByteBufferPool MEDIUM_HEAP

The medium heap buffer pool. This pool produces buffers of MEDIUM_SIZE.

MEDIUM_SIZEback to summary
public static final int MEDIUM_SIZE

The size of medium buffers.

sizeback to summary
private final int size
sliceLargeBuffersback to summary
private static final boolean sliceLargeBuffers
SMALL_DIRECTback to summary
public static final ByteBufferPool SMALL_DIRECT

The small direct buffer pool. This pool produces buffers of SMALL_SIZE.

SMALL_HEAPback to summary
public static final ByteBufferPool SMALL_HEAP

The small heap buffer pool. This pool produces buffers of SMALL_SIZE.

SMALL_SIZEback to summary
public static final int SMALL_SIZE

The size of small buffers.

threadLocalCacheback to summary
private final ThreadLocal<ByteBufferPool.Cache> threadLocalCache

Constructor Detail

ByteBufferPoolback to summary
pack-priv ByteBufferPool(final int size, final boolean direct)

Method Detail

acceptWithCacheExback to summary
public <T, U, E extends Exception> void acceptWithCacheEx(int cacheSize, ExceptionBiConsumer<T, U, E> consumer, T param1, U param2) throws ExceptionBiConsumer-T-U-: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.

Parameters
<T>
the type of the first parameter
<U>
the type of the second parameter
<E>
the exception type thrown by the operation
cacheSize:int

the cache size to run under

consumer:ExceptionBiConsumer<T, U, E>

the action to run

param1:T

the first parameter to pass to the action

param2:U

the second parameter to pass to the action

Exceptions
ExceptionBiConsumer-T-U-:E:
if the nested action threw an exception
acceptWithCacheExback to summary
public <T, E extends Exception> void acceptWithCacheEx(int cacheSize, ExceptionConsumer<T, E> consumer, T param) throws ExceptionConsumer-T-: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.

Parameters
<T>
the type of the parameter
<E>
the exception type thrown by the operation
cacheSize:int

the cache size to run under

consumer:ExceptionConsumer<T, E>

the action to run

param:T

the parameter to pass to the action

Exceptions
ExceptionConsumer-T-:E:
if the nested action threw an exception
allocateback to summary
public ByteBuffer allocate()

Allocate a buffer from this source pool. The buffer must be freed through the free(ByteBuffer) method.

Returns:ByteBuffer

the allocated buffer

allocateback to summary
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:ByteBuffer[]

the array of buffers to fill

offs:int

the offset into the array to fill

allocateback to summary
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:ByteBuffer[]

the array of buffers to fill

offs:int

the offset into the array to fill

len:int

the number of buffers to fill in the array

allocateMasterback to summary
private ByteBuffer allocateMaster()
applyWithCacheExback to summary
public <T, U, R, E extends Exception> R applyWithCacheEx(int cacheSize, ExceptionBiFunction<T, U, R, E> function, T param1, U param2) throws ExceptionBiFunction-T-U-: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.

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
cacheSize:int

the cache size to run under

function:ExceptionBiFunction<T, U, R, E>

the action to run

param1:T

the first parameter to pass to the action

param2:U

the second parameter to pass to the action

Returns:R

the result of the action

Exceptions
ExceptionBiFunction-T-U-:E:
if the nested action threw an exception
applyWithCacheExback to summary
public <T, R, E extends Exception> R applyWithCacheEx(int cacheSize, ExceptionFunction<T, R, E> function, T param) throws ExceptionFunction-T-: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.

Parameters
<T>
the type of the parameter
<R>
the return type of the operation
<E>
the exception type thrown by the operation
cacheSize:int

the cache size to run under

function:ExceptionFunction<T, R, E>

the action to run

param:T

the parameter to pass to the action

Returns:R

the result of the action

Exceptions
ExceptionFunction-T-:E:
if the nested action threw an exception
createback to summary
pack-priv static ByteBufferPool create(final int size, final boolean direct)
createBufferback to summary
pack-priv abstract ByteBuffer createBuffer()
doFreeback to summary
pack-priv final void doFree(final ByteBuffer buffer)
flushAllCachesback to summary
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.

flushCachesback to summary
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.

freeback to summary
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:ByteBuffer

the buffer to free

freeback to summary
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:ByteBuffer[]

the buffer array

offs:int

the offset into the array

len:int

the number of buffers to free

freeMasterback to summary
pack-priv final void freeMaster(ByteBuffer buffer)
getDefaultCacheback to summary
pack-priv ByteBufferPool.Cache getDefaultCache()
getMasterQueueback to summary
pack-priv ConcurrentLinkedQueue<ByteBuffer> getMasterQueue()
getSizeback to summary
public int getSize()

Get the size of buffers returned by this source. The size will be a power of two.

Returns:int

the size of buffers returned by this source

getWithCacheExback to summary
public <R, E extends Exception> R getWithCacheEx(int cacheSize, ExceptionSupplier<R, E> supplier) throws ExceptionSupplier-: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.

Parameters
<R>
the return type of the operation
<E>
the exception type thrown by the operation
cacheSize:int

the cache size to run under

supplier:ExceptionSupplier<R, E>

the action to run

Returns:R

the result of the action

Exceptions
ExceptionSupplier-:E:
if the nested action threw an exception
isDirectback to summary
public boolean isDirect()

Determine if this source returns direct buffers.

Returns:boolean

true if the buffers are direct, false if they are heap

runWithCacheback to summary
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:int

the cache size to run under

runnable:Runnable

the action to run

runWithCacheExback to summary
public <E extends Exception> void runWithCacheEx(int cacheSize, ExceptionRunnable<E> runnable) throws ExceptionRunnable-: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.

Parameters
<E>
the exception type thrown by the operation
cacheSize:int

the cache size to run under

runnable:ExceptionRunnable<E>

the action to run

Exceptions
ExceptionRunnable-:E:
if the nested action threw an exception
subPoolback to summary
pack-priv static ByteBufferPool subPool(final ByteBufferPool parent, final int size)
zeroAndFreeback to summary
public static void zeroAndFree(ByteBuffer buffer)

Free a buffer as with free(ByteBuffer) except the buffer is first zeroed and cleared.

Parameters
buffer:ByteBuffer

the buffer to free

org.xnio back to summary

pack-priv Interface ByteBufferPool.Cache

Known Direct Implementers
org.xnio.ByteBufferPool.OneCache, org.xnio.ByteBufferPool.TwoCache, org.xnio.ByteBufferPool.MultiCache, org.xnio.ByteBufferPool.DefaultCache

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer
public void
public void
public void
public void

Method Detail

allocateback to summary
public ByteBuffer allocate()
destroyback to summary
public void destroy()
flushback to summary
public void flush()
flushBufferback to summary
public void flushBuffer(ByteBuffer bb)
freeback to summary
public void free(ByteBuffer bb)
org.xnio back to summary

pack-priv final Class ByteBufferPool.DefaultCache

extends Object
implements Cache
Class Inheritance
All Implemented Interfaces
org.xnio.ByteBufferPool.Cache

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer
public void
public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultCacheback to summary
pack-priv DefaultCache()

Method Detail

allocateback to summary
public ByteBuffer allocate()

Implements org.xnio.ByteBufferPool.Cache.allocate.

destroyback to summary
public void destroy()

Implements org.xnio.ByteBufferPool.Cache.destroy.

flushback to summary
public void flush()

Implements org.xnio.ByteBufferPool.Cache.flush.

flushBufferback to summary
public void flushBuffer(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.flushBuffer.

freeback to summary
public void free(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.free.

org.xnio back to summary

pack-priv final Class ByteBufferPool.MultiCache

extends Object
implements Cache
Class Inheritance
All Implemented Interfaces
org.xnio.ByteBufferPool.Cache

Field Summary

Modifier and TypeField and Description
private long
private final ByteBuffer[]
private final long
private final ByteBufferPool.Cache

Constructor Summary

AccessConstructor and Description
pack-priv
MultiCache(final ByteBufferPool.Cache parent, final int size)

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer
public void
public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

availableBitsback to summary
private long availableBits
cacheback to summary
private final ByteBuffer[] cache
maskback to summary
private final long mask
parentback to summary
private final ByteBufferPool.Cache parent

Constructor Detail

MultiCacheback to summary
pack-priv MultiCache(final ByteBufferPool.Cache parent, final int size)

Method Detail

allocateback to summary
public ByteBuffer allocate()

Implements org.xnio.ByteBufferPool.Cache.allocate.

destroyback to summary
public void destroy()

Implements org.xnio.ByteBufferPool.Cache.destroy.

flushback to summary
public void flush()

Implements org.xnio.ByteBufferPool.Cache.flush.

flushBufferback to summary
public void flushBuffer(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.flushBuffer.

freeback to summary
public void free(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.free.

org.xnio back to summary

pack-priv final Class ByteBufferPool.OneCache

extends Object
implements Cache
Class Inheritance
All Implemented Interfaces
org.xnio.ByteBufferPool.Cache

Field Summary

Modifier and TypeField and Description
private ByteBuffer
private final ByteBufferPool.Cache

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer
public void
public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private ByteBuffer buffer
parentback to summary
private final ByteBufferPool.Cache parent

Constructor Detail

OneCacheback to summary
pack-priv OneCache(final ByteBufferPool.Cache parent)

Method Detail

allocateback to summary
public ByteBuffer allocate()

Implements org.xnio.ByteBufferPool.Cache.allocate.

destroyback to summary
public void destroy()

Implements org.xnio.ByteBufferPool.Cache.destroy.

flushback to summary
public void flush()

Implements org.xnio.ByteBufferPool.Cache.flush.

flushBufferback to summary
public void flushBuffer(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.flushBuffer.

freeback to summary
public void free(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.free.

org.xnio back to summary

public final Class ByteBufferPool.Set

extends Object
Class Inheritance

A set of buffer pools for each size, which can either be DIRECT or HEAP.

Field Summary

Modifier and TypeField and Description
public static final ByteBufferPool.Set
DIRECT

The direct buffer source set.

public static final ByteBufferPool.Set
HEAP

The heap buffer source set.

private final ByteBufferPool
private final ByteBufferPool
private final ByteBufferPool

Constructor Summary

AccessConstructor and Description
pack-priv
Set(final ByteBufferPool small, final ByteBufferPool normal, final ByteBufferPool large)

Method Summary

Modifier and TypeMethod and Description
public ByteBufferPool

Returns:

the large buffer pool for this set
getLarge
()

Get the large buffer pool for this set.

public ByteBufferPool

Returns:

the medium buffer pool for this set
getNormal
()

Get the medium buffer pool for this set.

public ByteBufferPool

Returns:

the small buffer pool for this set
getSmall
()

Get the small buffer pool for this set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DIRECTback to summary
public static final ByteBufferPool.Set DIRECT

The direct buffer source set.

HEAPback to summary
public static final ByteBufferPool.Set HEAP

The heap buffer source set.

largeback to summary
private final ByteBufferPool large
normalback to summary
private final ByteBufferPool normal
smallback to summary
private final ByteBufferPool small

Constructor Detail

Setback to summary
pack-priv Set(final ByteBufferPool small, final ByteBufferPool normal, final ByteBufferPool large)

Method Detail

getLargeback to summary
public ByteBufferPool getLarge()

Get the large buffer pool for this set.

Returns:ByteBufferPool

the large buffer pool for this set

getNormalback to summary
public ByteBufferPool getNormal()

Get the medium buffer pool for this set.

Returns:ByteBufferPool

the medium buffer pool for this set

getSmallback to summary
public ByteBufferPool getSmall()

Get the small buffer pool for this set.

Returns:ByteBufferPool

the small buffer pool for this set

org.xnio back to summary

pack-priv final Class ByteBufferPool.TwoCache

extends Object
implements Cache
Class Inheritance
All Implemented Interfaces
org.xnio.ByteBufferPool.Cache

Field Summary

Modifier and TypeField and Description
private ByteBuffer
private ByteBuffer
private final ByteBufferPool.Cache

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer
public void
public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

buffer1back to summary
private ByteBuffer buffer1
buffer2back to summary
private ByteBuffer buffer2
parentback to summary
private final ByteBufferPool.Cache parent

Constructor Detail

TwoCacheback to summary
pack-priv TwoCache(final ByteBufferPool.Cache parent)

Method Detail

allocateback to summary
public ByteBuffer allocate()

Implements org.xnio.ByteBufferPool.Cache.allocate.

destroyback to summary
public void destroy()

Implements org.xnio.ByteBufferPool.Cache.destroy.

flushback to summary
public void flush()

Implements org.xnio.ByteBufferPool.Cache.flush.

flushBufferback to summary
public void flushBuffer(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.flushBuffer.

freeback to summary
public void free(final ByteBuffer bb)

Implements org.xnio.ByteBufferPool.Cache.free.