Class ManagedBuffer<T extends Buffer>
Buffer
that maintains a lock count for that
buffer.All methods known from Buffer are provided and delegate to the backing buffer. Managed buffers can be used to maintain pools of buffers. Buffers are locked when retrieved from the pool and can automatically be returned when the last lock is released.
Newly created managed buffer always have a lock count of 1 (you create them for using them, don’t you).
-
Nested Class Summary
Modifier and TypeClassDescriptionfinal class
A read-only view of the managed buffer’s content (backing buffer) and a reference to the managed buffer.final class
A read-only view of the managed buffer’s content (backing buffer) and a reference to the managed buffer. -
Field Summary
Modifier and TypeFieldDescriptionprotected T
static final ManagedBuffer
<ByteBuffer> static final ManagedBuffer
<CharBuffer> protected T
-
Constructor Summary
ConstructorDescriptionManagedBuffer
(T buffer, BufferCollector<ManagedBuffer<T>> manager) Create a new Managed buffer, backed by the given buffer, with a lock count of one. -
Method Summary
Modifier and TypeMethodDescriptionarray()
int
Return the backing buffer.final int
capacity()
final Buffer
clear()
final T
Duplicate the buffer.int
fillFromChannel
(ReadableByteChannel channel) Convenience method to fill the buffer from the channel.final Buffer
flip()
boolean
hasArray()
final boolean
boolean
isDirect()
boolean
final int
limit()
final Buffer
limit
(int newLimit) linkBackingBuffer
(ManagedBuffer<T> buffer) Links this instance’s backing buffer (temporarily) to the given buffer’s backing buffer.Increases the buffer’s lock count.manager()
Return the buffer’s manager.final Buffer
mark()
Creates a newByteBuffer
view.Creates a newCharBuffer
view.final int
position()
final Buffer
position
(int newPosition) final int
replaceBackingBuffer
(T buffer) Replace the backing buffer.final Buffer
reset()
final Buffer
rewind()
toString()
void
Decreases the buffer’s lock count.static <B extends Buffer>
ManagedBuffer<B> wrap
(B buffer) Convenience method for creating aManagedBuffer
with aBufferCollector.NOOP_COLLECTOR
from a NIO buffer.
-
Field Details
-
EMPTY_BYTE_BUFFER
-
EMPTY_CHAR_BUFFER
-
backing
-
savedBacking
-
-
Constructor Details
-
ManagedBuffer
Create a new Managed buffer, backed by the given buffer, with a lock count of one.- Parameters:
buffer
- the backing buffermanager
- used for restoring the buffer when the lock count reaches zero
-
-
Method Details
-
wrap
Convenience method for creating aManagedBuffer
with aBufferCollector.NOOP_COLLECTOR
from a NIO buffer.Effectively, this creates an unmanaged buffer that looks like a managed buffer from an existing NIO buffer that does not belong to any pool.
- Type Parameters:
B
- the buffer type- Parameters:
buffer
- the buffer to wrap- Returns:
- the managed buffer
-
backingBuffer
Return the backing buffer.- Returns:
- the buffer
-
replaceBackingBuffer
Replace the backing buffer.- Parameters:
buffer
- the new buffer- Returns:
- the managed buffer for easy chaining
-
linkBackingBuffer
Links this instance’s backing buffer (temporarily) to the given buffer’s backing buffer.Locks the given buffer. The buffer is unlocked again and the original backing buffer restored if this method is called with
null
or this managed buffer is recollected (i.e. no longer used).This method may be used to “assign” data that is already available in a buffer to this buffer without copying it over.
- Parameters:
buffer
- the buffer- Returns:
- the managed buffer for easy chaining
-
manager
Return the buffer’s manager.- Returns:
- the manager
-
lockBuffer
Increases the buffer’s lock count.- Returns:
- the managed buffer for easy chaining
-
unlockBuffer
Decreases the buffer’s lock count.If the lock count reached zero, the buffer collect’s
BufferCollector.recollect(B)
method is invoked.- Throws:
IllegalStateException
- if the buffer is not locked or has been released already
-
fillFromChannel
Convenience method to fill the buffer from the channel.Unlocks the buffer if an
IOException
occurs. This method may only be invoked forManagedBuffer
s backed by aByteBuffer
.- Parameters:
channel
- the channel- Returns:
- the bytes read
- Throws:
IOException
- Signals that an I/O exception has occurred.
-
toString
-
array
- Returns:
- the backing array
- See Also:
-
arrayOffset
- Returns:
- the backing array offset
- See Also:
-
capacity
- Returns:
- the capacity
- See Also:
-
clear
- Returns:
- the buffer
- See Also:
-
duplicate
Duplicate the buffer.- Returns:
- the t
-
flip
- Returns:
- the buffer
- See Also:
-
hasArray
- Returns:
- the result
- See Also:
-
hasRemaining
- Returns:
- the result
- See Also:
-
isDirect
- Returns:
- the result
- See Also:
-
isReadOnly
- Returns:
- the result
- See Also:
-
limit
- Returns:
- the result
- See Also:
-
limit
- Parameters:
newLimit
- the new limit- Returns:
- the result
- See Also:
-
mark
- Returns:
- the buffer
- See Also:
-
position
- Returns:
- the result
- See Also:
-
position
- Parameters:
newPosition
- the new position- Returns:
- the buffer
- See Also:
-
remaining
- Returns:
- the result
- See Also:
-
reset
- Returns:
- the Buffer
- See Also:
-
rewind
- Returns:
- the Buffer
- See Also:
-
newByteBufferView
Creates a newByteBuffer
view.- Returns:
- the byte buffer view
-
newCharBufferView
Creates a newCharBuffer
view.- Returns:
- the byte buffer view
-