Interface RetainableByteBuffer
- All Superinterfaces:
 Retainable
- All Known Subinterfaces:
 Content.Chunk, RetainableByteBuffer.Mutable
- All Known Implementing Classes:
 AbstractRetainableByteBuffer, ArrayByteBufferPool.Tracking.TrackedBuffer, Content.Chunk.Empty, RetainableByteBuffer.Abstract, RetainableByteBuffer.DynamicCapacity, RetainableByteBuffer.EmptyRetainableByteBuffer, RetainableByteBuffer.FixedCapacity, RetainableByteBuffer.NonRetainableByteBuffer, RetainableByteBuffer.Pooled, RetainableByteBuffer.Wrapper, Trailers
An abstraction over ByteBuffers which provides:
Retainabilityso that reference counts can be maintained for shared buffers.Pooledbuffers that use theByteBufferPoolfor any operations and which are returned to theByteBufferPoolwhen fullyreleased.- Either 
fixed capacitybuffers over a singleByteBufferordynamic capacityover possible multipleByteBuffers. - Access APIs to 
get,sliceortakefrom the buffer - A 
MutableAPI variant toput,appendoraddto the buffer 
When possible and optimal, implementations will avoid data copies. However, copies may be favoured over retaining large buffers with small content.
Accessing data in the buffer can be achieved via:
- The 
get()/get(long)/get(byte[], int, int)methods provide direct access to bytes within the buffer. - The 
slice()/slice(long)methods for shared access to common backing buffers, but with independent indexes. - The 
take()/take(long)methods for minimal copy extraction of bulk data. - Accessing the underlying 
ByteBufferviagetByteBuffer(), which may coalesce multiple buffers into a single. 
The RetainableByteBuffer APIs are non-modal, meaning that there is no need for any flip
operation between a mutable method and an accessor method.
ByteBuffer returned or passed to this API should be in "flush" mode, with valid data between the
position and limit.  The ByteBuffer returned from
getByteBuffer() may used directly and switched to "fill" mode, but it is the callers responsibility to
flip back to "flush" mode, before any RetainableByteBuffer APIs are used.
The RetainableByteBuffer APIs hide any notion of unused space before or after valid data. All indexing is relative
to the first byte of data in the buffer and no manipulation of data pointers is directly supported.
The buffer may be large and the size() is represented as a long in new APIs.  However, APIs that
are tied to a single backing ByteBuffer may use integer representations of size and indexes.
- 
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classAn abstract implementation ofRetainableByteBufferthat provides the basicRetainablefunctionalitystatic classAnRetainableByteBuffer.MutableRetainableByteBufferthat can grow its capacity, backed by a chain ofByteBuffer, which may grow either by aggregation and/or retention.static classARetainableByteBufferthat is empty and not retainable.static classstatic interfaceExtendedRetainableByteBufferAPI with mutator methods.static classARetainableByteBuffer.FixedCapacitybuffer that is neither poolable norretainable.static classApooledbuffer that knows the pool from which it was allocated.static classA wrapper forRetainableByteBufferinstancesNested classes/interfaces inherited from interface Retainable
Retainable.ReferenceCounter - 
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RetainableByteBufferA Zero-capacity, non-retainableRetainableByteBuffer.Fields inherited from interface Retainable
NON_RETAINABLE - 
Method Summary
Modifier and TypeMethodDescriptiondefault booleanappendTo(ByteBuffer buffer) Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.default booleanappendTo(RetainableByteBuffer buffer) Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.default RetainableByteBuffer.MutableAccess this buffer via theRetainableByteBuffer.MutableAPI.default intcapacity()default voidclear()default RetainableByteBuffercopy()Creates a deep copy of this RetainableByteBuffer that is entirely independentdefault byteget()Consumes and returns a byte from this RetainableByteBufferdefault intget(byte[] bytes, int offset, int length) Consumes and copies the bytes from this RetainableByteBuffer to the given byte array.default byteget(long index) Returns a byte from this RetainableByteBuffer at a specific indexGet the wrapped, notnull,ByteBuffer.default booleandefault booleanisDirect()default booleanisEmpty()default booleanisFull()default booleanCheck if the underlying implementation is mutable.default voidlimit(long size) Limit this buffer's contents to the size.default longmaxSize()default voidputTo(ByteBuffer toInfillMode) Consumes and puts the contents of this retainable byte buffer at the end of the given byte buffer.default intdefault longsize()default longskip(long length) Skips, advancing the ByteBuffer position, the given number of bytes.default RetainableByteBufferslice()Get a slice of the buffer.default RetainableByteBufferslice(long length) Get a partial slice of the buffer.default longspace()default RetainableByteBuffertake()Take the contents of this buffer, leaving it clear.default RetainableByteBuffertake(long length) Take the contents of this buffer, from the head, leaving remaining bytes in this buffer.default byte[]Take the contents of this buffer, leaving it clear and independent.default RetainableByteBuffertakeFrom(long skip) Take the contents of this buffer, from the tail, leaving remaining bytes in this buffer.default Stringstatic RetainableByteBuffer.Mutablewrap(ByteBuffer byteBuffer) Returns a non-retainableRetainableByteBufferthat wraps the givenByteBuffer.static RetainableByteBuffer.Mutablewrap(ByteBuffer byteBuffer, Runnable releaser) static RetainableByteBuffer.Mutablewrap(ByteBuffer byteBuffer, Retainable retainable) default voidwriteTo(Content.Sink sink, boolean last) Writes and consumes the contents of this retainable byte buffer into the given sink.default voidwriteTo(Content.Sink sink, boolean last, Callback callback) Asynchronously writes and consumes the contents of this retainable byte buffer into the given sink.Methods inherited from interface Retainable
canRetain, getRetained, isRetained, release, retain 
- 
Field Details
- 
EMPTY
A Zero-capacity, non-retainableRetainableByteBuffer. 
 - 
 - 
Method Details
- 
wrap
Returns a non-retainable
RetainableByteBufferthat wraps the givenByteBuffer.Use this method to wrap user-provided
ByteBuffers, orByteBuffers that hold constant bytes, to make them look likeRetainableByteBuffers.The returned
RetainableByteBufferRetainable.canRetain()method always returnsfalse.RetainableByteBuffers returned by this method are not suitable to be wrapped in otherRetainableimplementations that may delegate calls toRetainable.retain().- Parameters:
 byteBuffer- theByteBufferto wrap- Returns:
 - a 
RetainableByteBuffer.FixedCapacitybuffer wrapping the passedByteBuffer - See Also:
 
 - 
wrap
Returns a
RetainableByteBufferthat wraps the givenByteBufferandRetainable.- Parameters:
 byteBuffer- theByteBufferto wrapretainable- the associatedRetainable.- Returns:
 - a 
RetainableByteBuffer.FixedCapacitybuffer wrapping the passedByteBuffer - See Also:
 
 - 
wrap
Returns a
RetainableByteBufferthat wraps the givenByteBufferandRunnablereleaser.- Parameters:
 byteBuffer- theByteBufferto wrapreleaser- aRunnableto call when the buffer is released.- Returns:
 - a 
RetainableByteBuffer.FixedCapacitybuffer wrapping the passedByteBuffer 
 - 
isMutable
default boolean isMutable()Check if the underlying implementation is mutable. Note that the immutableRetainableByteBufferAPI may be backed by a mutableByteBufferor theRetainableByteBuffer.MutableAPI may be backed by an immutableByteBuffer.- Returns:
 - whether this buffers implementation is mutable
 - See Also:
 
 - 
asMutable
Access this buffer via theRetainableByteBuffer.MutableAPI. Note that theRetainableByteBuffer.MutableAPI may be backed by an immutableByteBuffer.- Returns:
 - An 
RetainableByteBuffer.Mutablerepresentation of this buffer with same data and pointers. - Throws:
 ReadOnlyBufferException- If the buffer is notRetainableByteBuffer.Mutableor the backingByteBufferisread-only.- See Also:
 
 - 
appendTo
Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.- Parameters:
 buffer- The buffer to append bytes to, whose limit will be updated.- Returns:
 trueif all bytes in this buffer are able to be appended.- See Also:
 
 - 
appendTo
Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.- Parameters:
 buffer- The buffer to append bytes to, whose limit will be updated.- Returns:
 trueif all bytes in this buffer are able to be appended.- See Also:
 
 - 
copy
Creates a deep copy of this RetainableByteBuffer that is entirely independent- Returns:
 - A copy of this RetainableByteBuffer
 
 - 
get
Consumes and returns a byte from this RetainableByteBuffer- Returns:
 - the byte
 - Throws:
 BufferUnderflowException- if the buffer is empty.- See Also:
 
 - 
get
Returns a byte from this RetainableByteBuffer at a specific index- Parameters:
 index- The index relative to the current start of unconsumed data in the buffer.- Returns:
 - the byte
 - Throws:
 IndexOutOfBoundsException- if the index is too large.
 - 
get
default int get(byte[] bytes, int offset, int length) Consumes and copies the bytes from this RetainableByteBuffer to the given byte array.- Parameters:
 bytes- the byte array to copy the bytes intooffset- the offset within the byte arraylength- the maximum number of bytes to copy- Returns:
 - the number of bytes actually copied
 
 - 
getByteBuffer
Get the wrapped, notnull,ByteBuffer.If the implementation contains multiple buffers, they are coalesced to a single buffer before being returned. If the content is too large for a single
ByteBuffer, then the content should be access withwriteTo(Content.Sink, boolean).- Returns:
 - the wrapped, not 
null,ByteBuffer - Throws:
 BufferOverflowException- if the contents is too large for a singleByteBuffer
 - 
isDirect
default boolean isDirect()- Returns:
 - whether the 
ByteBufferis direct 
 - 
remaining
default int remaining()- Returns:
 - the number of remaining bytes in the 
ByteBuffer - See Also:
 
 - 
hasRemaining
default boolean hasRemaining()- Returns:
 - whether the 
ByteBufferhas remaining bytes 
 - 
isEmpty
default boolean isEmpty()- Returns:
 - whether the 
ByteBufferhas remaining bytes left for reading 
 - 
size
default long size()- Returns:
 - the number of remaining bytes in the 
ByteBuffer - See Also:
 
 - 
maxSize
default long maxSize()- Returns:
 - the maximum size in bytes.
 - See Also:
 
 - 
capacity
default int capacity()- Returns:
 - the capacity
 - See Also:
 
 - 
clear
default void clear()- See Also:
 
 - 
space
default long space()- Returns:
 - the number of bytes that can be added, appended or put into this buffer,
assuming it is 
mutable. 
 - 
isFull
default boolean isFull()- Returns:
 - true if no more bytes can be added, appended or put to this buffer,
assuming it is 
mutable. 
 - 
skip
default long skip(long length) Skips, advancing the ByteBuffer position, the given number of bytes.
- Parameters:
 length- the maximum number of bytes to skip- Returns:
 - the number of bytes actually skipped
 
 - 
limit
default void limit(long size) Limit this buffer's contents to the size.
- Parameters:
 size- the new size of the buffer
 - 
slice
Get a slice of the buffer.- Returns:
 - A sliced 
RetainableByteBuffersharing this buffers data and reference count, but with independent position. The buffer isretainedby this call. - See Also:
 
 - 
slice
Get a partial slice of the buffer. This is equivalent toslice().limit(long), but may be implemented more efficiently.- Parameters:
 length- The number of bytes to slice, which may beyond the limit and less than the capacity, in which case it will ensure some spare capacity in the slice.- Returns:
 - A sliced 
RetainableByteBuffersharing the firstlengthbytes of this buffers data and reference count, but with independent position. The buffer isretainedby this call. 
 - 
take
Take the contents of this buffer, from the head, leaving remaining bytes in this buffer. This is similar toslice(long)followed by askip(long), but avoids shared data.- Parameters:
 length- The number of bytes to take- Returns:
 - A buffer with the contents of this buffer after limiting bytes, avoiding copies if possible, but with no shared internal buffers.
 
 - 
takeFrom
Take the contents of this buffer, from the tail, leaving remaining bytes in this buffer.- Parameters:
 skip- The number of bytes to skip before taking the tail.- Returns:
 - A buffer with the contents of this buffer after skipping bytes, avoiding copies if possible, but with no shared internal buffers.
 
 - 
take
Take the contents of this buffer, leaving it clear.- Returns:
 - A buffer with the contents of this buffer, avoiding copies if possible.
 - See Also:
 
 - 
takeByteArray
default byte[] takeByteArray()Take the contents of this buffer, leaving it clear and independent.- Returns:
 - A possibly newly allocated array with the contents of this buffer, avoiding copies if possible.
 
 - 
putTo
Consumes and puts the contents of this retainable byte buffer at the end of the given byte buffer.- Parameters:
 toInfillMode- the destination buffer, whose position is updated.- Throws:
 BufferOverflowException- – If there is insufficient space in this buffer for the remaining bytes in the source buffer- See Also:
 
 - 
writeTo
Asynchronously writes and consumes the contents of this retainable byte buffer into the given sink.- Parameters:
 sink- the destination sink.last- true if this is the last write.callback- the callback to call upon the write completion.- See Also:
 
 - 
writeTo
Writes and consumes the contents of this retainable byte buffer into the given sink.- Parameters:
 sink- the destination sink.last- true if this is the last write.- Throws:
 IOException- See Also:
 
 - 
toDetailString
- Returns:
 - A string showing the info and detail about this buffer, as well as a summary of the contents
 
 
 -