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
Nested ClassesModifier and TypeClassDescriptionfinal classA read-only view of the managed buffer’s content (backing buffer) and a reference to the managed buffer.final classA read-only view of the managed buffer’s content (backing buffer) and a reference to the managed buffer. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Tstatic final ManagedBuffer<ByteBuffer> static final ManagedBuffer<CharBuffer> protected T -
Constructor Summary
ConstructorsConstructorDescriptionManagedBuffer(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()intReturn the backing buffer.final intcapacity()final Bufferclear()final TDuplicate the buffer.intfillFromChannel(ReadableByteChannel channel) Convenience method to fill the buffer from the channel.final Bufferflip()booleanhasArray()final booleanbooleanisDirect()booleanfinal intlimit()final Bufferlimit(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 Buffermark()Creates a newByteBufferview.Creates a newCharBufferview.final intposition()final Bufferposition(int newPosition) final intreplaceBackingBuffer(T buffer) Replace the backing buffer.final Bufferreset()final Bufferrewind()toString()voidDecreases the buffer’s lock count.static <B extends Buffer>
ManagedBuffer<B> wrap(B buffer) Convenience method for creating aManagedBufferwith aBufferCollector.NOOP_COLLECTORfrom 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 aManagedBufferwith aBufferCollector.NOOP_COLLECTORfrom 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
nullor 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
IOExceptionoccurs. This method may only be invoked forManagedBuffers 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 newByteBufferview.- Returns:
- the byte buffer view
-
newCharBufferView
Creates a newCharBufferview.- Returns:
- the byte buffer view
-