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 ByteBuffer
s which provides:
Retainability
so that reference counts can be maintained for shared buffers.Pooled
buffers that use theByteBufferPool
for any operations and which are returned to theByteBufferPool
when fullyreleased
.- Either
fixed capacity
buffers over a singleByteBuffer
ordynamic capacity
over possible multipleByteBuffer
s. - Access APIs to
get
,slice
ortake
from the buffer - A
Mutable
API variant toput
,append
oradd
to 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
ByteBuffer
viagetByteBuffer()
, 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 class
An abstract implementation ofRetainableByteBuffer
that provides the basicRetainable
functionalitystatic class
AnRetainableByteBuffer.Mutable
RetainableByteBuffer
that can grow its capacity, backed by a chain ofByteBuffer
, which may grow either by aggregation and/or retention.static class
ARetainableByteBuffer
that is empty and not retainable.static class
static interface
ExtendedRetainableByteBuffer
API with mutator methods.static class
ARetainableByteBuffer.FixedCapacity
buffer that is neither poolable norretainable
.static class
Apooled
buffer that knows the pool from which it was allocated.static class
A wrapper forRetainableByteBuffer
instancesNested classes/interfaces inherited from interface org.eclipse.jetty.io.Retainable
Retainable.ReferenceCounter
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RetainableByteBuffer
A Zero-capacity, non-retainableRetainableByteBuffer
.Fields inherited from interface org.eclipse.jetty.io.Retainable
NON_RETAINABLE
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
appendTo
(ByteBuffer buffer) Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.default boolean
appendTo
(RetainableByteBuffer buffer) Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.default RetainableByteBuffer.Mutable
Access this buffer via theRetainableByteBuffer.Mutable
API.default int
capacity()
default void
clear()
default RetainableByteBuffer
copy()
Creates a deep copy of this RetainableByteBuffer that is entirely independentdefault byte
get()
Consumes and returns a byte from this RetainableByteBufferdefault int
get
(byte[] bytes, int offset, int length) Consumes and copies the bytes from this RetainableByteBuffer to the given byte array.default byte
get
(long index) Returns a byte from this RetainableByteBuffer at a specific indexGet the wrapped, notnull
,ByteBuffer
.default boolean
default boolean
isDirect()
default boolean
isEmpty()
default boolean
isFull()
default boolean
Check if the underlying implementation is mutable.default void
limit
(long size) Limit this buffer's contents to the size.default long
maxSize()
default void
putTo
(ByteBuffer toInfillMode) Consumes and puts the contents of this retainable byte buffer at the end of the given byte buffer.default int
default long
size()
default long
skip
(long length) Skips, advancing the ByteBuffer position, the given number of bytes.default RetainableByteBuffer
slice()
Get a slice of the buffer.default RetainableByteBuffer
slice
(long length) Get a partial slice of the buffer.default long
space()
default RetainableByteBuffer
take()
Take the contents of this buffer, leaving it clear.default RetainableByteBuffer
take
(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 RetainableByteBuffer
takeFrom
(long skip) Take the contents of this buffer, from the tail, leaving remaining bytes in this buffer.default String
static RetainableByteBuffer.Mutable
wrap
(ByteBuffer byteBuffer) Returns a non-retainableRetainableByteBuffer
that wraps the givenByteBuffer
.static RetainableByteBuffer.Mutable
wrap
(ByteBuffer byteBuffer, Runnable releaser) static RetainableByteBuffer.Mutable
wrap
(ByteBuffer byteBuffer, Retainable retainable) default void
writeTo
(Content.Sink sink, boolean last) Writes and consumes the contents of this retainable byte buffer into the given sink.default void
writeTo
(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 org.eclipse.jetty.io.Retainable
canRetain, getRetained, isRetained, release, retain
-
Field Details
-
EMPTY
A Zero-capacity, non-retainableRetainableByteBuffer
.
-
-
Method Details
-
wrap
Returns a non-retainable
RetainableByteBuffer
that wraps the givenByteBuffer
.Use this method to wrap user-provided
ByteBuffer
s, orByteBuffer
s that hold constant bytes, to make them look likeRetainableByteBuffer
s.The returned
RetainableByteBuffer
Retainable.canRetain()
method always returnsfalse
.RetainableByteBuffer
s returned by this method are not suitable to be wrapped in otherRetainable
implementations that may delegate calls toRetainable.retain()
.- Parameters:
byteBuffer
- theByteBuffer
to wrap- Returns:
- a
RetainableByteBuffer.FixedCapacity
buffer wrapping the passedByteBuffer
- See Also:
-
wrap
Returns a
RetainableByteBuffer
that wraps the givenByteBuffer
andRetainable
.- Parameters:
byteBuffer
- theByteBuffer
to wrapretainable
- the associatedRetainable
.- Returns:
- a
RetainableByteBuffer.FixedCapacity
buffer wrapping the passedByteBuffer
- See Also:
-
wrap
Returns a
RetainableByteBuffer
that wraps the givenByteBuffer
andRunnable
releaser.- Parameters:
byteBuffer
- theByteBuffer
to wrapreleaser
- aRunnable
to call when the buffer is released.- Returns:
- a
RetainableByteBuffer.FixedCapacity
buffer wrapping the passedByteBuffer
-
isMutable
default boolean isMutable()Check if the underlying implementation is mutable. Note that the immutableRetainableByteBuffer
API may be backed by a mutableByteBuffer
or theRetainableByteBuffer.Mutable
API may be backed by an immutableByteBuffer
.- Returns:
- whether this buffers implementation is mutable
- See Also:
-
asMutable
Access this buffer via theRetainableByteBuffer.Mutable
API. Note that theRetainableByteBuffer.Mutable
API may be backed by an immutableByteBuffer
.- Returns:
- An
RetainableByteBuffer.Mutable
representation of this buffer with same data and pointers. - Throws:
ReadOnlyBufferException
- If the buffer is notRetainableByteBuffer.Mutable
or the backingByteBuffer
isread-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:
true
if 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:
true
if 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
ByteBuffer
is direct
-
remaining
default int remaining()- Returns:
- the number of remaining bytes in the
ByteBuffer
- See Also:
-
hasRemaining
default boolean hasRemaining()- Returns:
- whether the
ByteBuffer
has remaining bytes
-
isEmpty
default boolean isEmpty()- Returns:
- whether the
ByteBuffer
has 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
RetainableByteBuffer
sharing this buffers data and reference count, but with independent position. The buffer isretained
by 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
RetainableByteBuffer
sharing the firstlength
bytes of this buffers data and reference count, but with independent position. The buffer isretained
by 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
-