Class RetainableByteBuffer.DynamicCapacity
java.lang.Object
org.eclipse.jetty.io.Retainable.Wrapper
org.eclipse.jetty.io.RetainableByteBuffer.Abstract
org.eclipse.jetty.io.RetainableByteBuffer.DynamicCapacity
- All Implemented Interfaces:
Retainable
,RetainableByteBuffer
,RetainableByteBuffer.Mutable
- Enclosing interface:
RetainableByteBuffer
public static class RetainableByteBuffer.DynamicCapacity
extends RetainableByteBuffer.Abstract
implements RetainableByteBuffer.Mutable
An
RetainableByteBuffer.Mutable
RetainableByteBuffer
that can grow its capacity, backed by a chain of ByteBuffer
,
which may grow either by aggregation and/or retention.
When retaining, a chain of zero copy buffers are kept.
When aggregating, this class avoid repetitive copies of the same data during growth by aggregating
to a chain of buffers, which are only copied to a single buffer if required.
If the minRetainSize
is {code 0}, then appending to this buffer will always retain and accumulate.
If the minRetainSize
is Integer.MAX_VALUE
, then appending to this buffer will always aggregate.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.eclipse.jetty.io.Retainable
Retainable.ReferenceCounter, Retainable.Wrapper
Nested classes/interfaces inherited from interface org.eclipse.jetty.io.RetainableByteBuffer
RetainableByteBuffer.Abstract, RetainableByteBuffer.DynamicCapacity, RetainableByteBuffer.EmptyRetainableByteBuffer, RetainableByteBuffer.FixedCapacity, RetainableByteBuffer.Mutable, RetainableByteBuffer.NonRetainableByteBuffer, RetainableByteBuffer.Pooled, RetainableByteBuffer.Wrapper
-
Field Summary
Fields inherited from interface org.eclipse.jetty.io.Retainable
NON_RETAINABLE
Fields inherited from interface org.eclipse.jetty.io.RetainableByteBuffer
EMPTY
-
Constructor Summary
ConstructorsConstructorDescriptionA buffer with no size limit and default aggregation and retention settings.DynamicCapacity
(ByteBufferPool.Sized sizedPool) DynamicCapacity
(ByteBufferPool.Sized sizedPool, long maxSize) DynamicCapacity
(ByteBufferPool.Sized sizedPool, long maxSize, int minRetainSize) DynamicCapacity
(ByteBufferPool pool, boolean direct, long maxSize) DynamicCapacity
(ByteBufferPool pool, boolean direct, long maxSize, int aggregationSize) DynamicCapacity
(ByteBufferPool pool, boolean direct, long maxSize, int aggregationSize, int minRetainSize) -
Method Summary
Modifier and TypeMethodDescriptionadd
(ByteBuffer bytes) Add the passedByteBuffer
to this buffer, growing this buffer if necessary and possible.add
(RetainableByteBuffer bytes) Add the passedRetainableByteBuffer
to this buffer, growing this buffer if necessary and possible.protected void
addExtraStringInfo
(StringBuilder builder) protected void
addValueMarker
(StringBuilder builder, boolean beginning) protected void
addValueString
(StringBuilder builder) boolean
append
(ByteBuffer bytes) Copies the contents of the given byte buffer to the end of this buffer, growing this buffer if necessary and possible.boolean
append
(RetainableByteBuffer retainableBytes) Retain or copy the contents of the given retainable byte buffer to the end of this buffer, growing this buffer if necessary and possible.boolean
appendTo
(ByteBuffer to) Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.boolean
Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.Access this buffer via theRetainableByteBuffer.Mutable
API.int
capacity()
void
clear()
copy()
Creates a deep copy of this RetainableByteBuffer that is entirely independentbyte
get()
Consumes and returns a byte from this RetainableByteBufferint
get
(byte[] bytes, int offset, int length) Consumes and copies the bytes from this RetainableByteBuffer to the given byte array.byte
get
(long index) Returns a byte from this RetainableByteBuffer at a specific indexint
Get the wrapped, notnull
,ByteBuffer
.long
int
boolean
boolean
isDirect()
boolean
isFull()
boolean
Check if the underlying implementation is mutable.void
limit
(long limit) Limit this buffer's contents to the size.long
maxSize()
put
(byte b) Put abyte
to the buffer, growing this buffer if necessary and possible.put
(byte[] bytes, int offset, int length) Put abyte
array to the buffer, growing this buffer if necessary and possible.put
(long index, byte b) Put abyte
to the buffer at a given index.putInt
(int i) Put anint
to the buffer, growing this buffer if necessary and possible.putLong
(long l) Put along
to the buffer, growing this buffer if necessary and possible.putShort
(short s) Put ashort
to the buffer, growing this buffer if necessary and possible.void
putTo
(ByteBuffer toInfillMode) Consumes and puts the contents of this retainable byte buffer at the end of the given byte buffer.boolean
release()
Releases this resource, potentially decrementing a reference count (if any).int
long
size()
long
skip
(long length) Skips, advancing the ByteBuffer position, the given number of bytes.slice()
Get a slice of the buffer.slice
(long length) Get a partial slice of the buffer.long
space()
take
(long length) Take the contents of this buffer, from the head, leaving remaining bytes in this buffer.byte[]
Take the contents of this buffer, leaving it clear and independent.takeFrom
(long skip) Take the contents of this buffer, from the tail, leaving remaining bytes in this buffer.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 class org.eclipse.jetty.io.RetainableByteBuffer.Abstract
addStringInfo, toDetailString, toString
Methods inherited from class org.eclipse.jetty.io.Retainable.Wrapper
canRetain, getRetained, getWrapped, isRetained, retain
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.io.Retainable
canRetain, getRetained, isRetained, retain
Methods inherited from interface org.eclipse.jetty.io.RetainableByteBuffer
isEmpty, take, toDetailString, writeTo
Methods inherited from interface org.eclipse.jetty.io.RetainableByteBuffer.Mutable
put
-
Constructor Details
-
DynamicCapacity
public DynamicCapacity()A buffer with no size limit and default aggregation and retention settings. -
DynamicCapacity
- Parameters:
sizedPool
- The pool from which to allocate buffers, withByteBufferPool.Sized.isDirect()
configured andByteBufferPool.Sized.getSize()
used for the size of aggregation buffers.
-
DynamicCapacity
- Parameters:
sizedPool
- The pool from which to allocate buffers, withByteBufferPool.Sized.isDirect()
configured andByteBufferPool.Sized.getSize()
used for the size of aggregation buffers.maxSize
- The maximum length of the accumulated buffers or -1 for 2GB limit
-
DynamicCapacity
- Parameters:
sizedPool
- The pool from which to allocate buffers, withByteBufferPool.Sized.isDirect()
configured andByteBufferPool.Sized.getSize()
used for the size of aggregation buffers.maxSize
- The maximum length of the accumulated buffers or -1 for 2GB limitminRetainSize
- The minimal size of aRetainableByteBuffer
before it will be retained; or 0 to always retain; or -1 for a heuristic;
-
DynamicCapacity
- Parameters:
pool
- The pool from which to allocate buffers
-
DynamicCapacity
- Parameters:
pool
- The pool from which to allocate buffersdirect
- true if direct buffers should be usedmaxSize
- The maximum length of the accumulated buffers or -1 for 2GB limit
-
DynamicCapacity
- Parameters:
pool
- The pool from which to allocate buffersdirect
- true if direct buffers should be usedmaxSize
- The maximum length of the accumulated buffers or -1 for 2GB limitaggregationSize
- The default size of aggregation buffers; or 0 for no aggregation growth; or -1 for a default size. If theaggregationSize
is 0 and themaxSize
is less thatInteger.MAX_VALUE
, then a single aggregation buffer may be allocated and the class will behave similarly toRetainableByteBuffer.FixedCapacity
.
-
DynamicCapacity
public DynamicCapacity(ByteBufferPool pool, boolean direct, long maxSize, int aggregationSize, int minRetainSize) - Parameters:
pool
- The pool from which to allocate buffersdirect
- true if direct buffers should be usedmaxSize
- The maximum length of the accumulated buffers or -1 for 2GB limitaggregationSize
- The default size of aggregation buffers; or 0 for no aggregation growth; or -1 for a default size. If theaggregationSize
is 0 and themaxSize
is less thatInteger.MAX_VALUE
, then a single aggregation buffer may be allocated and the class will behave similarly toRetainableByteBuffer.FixedCapacity
.minRetainSize
- The minimal size of aRetainableByteBuffer
before it will be retained; or 0 to always retain; or -1 for a heuristic;
-
-
Method Details
-
getMaxSize
public long getMaxSize() -
getAggregationSize
public int getAggregationSize() -
getMinRetainSize
public int getMinRetainSize() -
isMutable
public boolean isMutable()Description copied from interface:RetainableByteBuffer
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
.- Specified by:
isMutable
in interfaceRetainableByteBuffer
- Returns:
- whether this buffers implementation is mutable
- See Also:
-
asMutable
Description copied from interface:RetainableByteBuffer
Access this buffer via theRetainableByteBuffer.Mutable
API. Note that theRetainableByteBuffer.Mutable
API may be backed by an immutableByteBuffer
.- Specified by:
asMutable
in interfaceRetainableByteBuffer
- Returns:
- An
RetainableByteBuffer.Mutable
representation of this buffer with same data and pointers. - See Also:
-
getByteBuffer
Description copied from interface:RetainableByteBuffer
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 withRetainableByteBuffer.writeTo(Content.Sink, boolean)
.- Specified by:
getByteBuffer
in interfaceRetainableByteBuffer
- Returns:
- the wrapped, not
null
,ByteBuffer
- Throws:
BufferOverflowException
- if the contents is too large for a singleByteBuffer
-
take
Description copied from interface:RetainableByteBuffer
Take the contents of this buffer, from the head, leaving remaining bytes in this buffer. This is similar toRetainableByteBuffer.slice(long)
followed by aRetainableByteBuffer.skip(long)
, but avoids shared data.- Specified by:
take
in interfaceRetainableByteBuffer
- 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
Description copied from interface:RetainableByteBuffer
Take the contents of this buffer, from the tail, leaving remaining bytes in this buffer.- Specified by:
takeFrom
in interfaceRetainableByteBuffer
- 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.
-
takeByteArray
public byte[] takeByteArray()Description copied from interface:RetainableByteBuffer
Take the contents of this buffer, leaving it clear and independent.- Specified by:
takeByteArray
in interfaceRetainableByteBuffer
- Returns:
- A possibly newly allocated array with the contents of this buffer, avoiding copies if possible.
-
get
Description copied from interface:RetainableByteBuffer
Consumes and returns a byte from this RetainableByteBuffer- Specified by:
get
in interfaceRetainableByteBuffer
- Returns:
- the byte
- Throws:
BufferUnderflowException
- if the buffer is empty.- See Also:
-
get
Description copied from interface:RetainableByteBuffer
Returns a byte from this RetainableByteBuffer at a specific index- Specified by:
get
in interfaceRetainableByteBuffer
- 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
public int get(byte[] bytes, int offset, int length) Description copied from interface:RetainableByteBuffer
Consumes and copies the bytes from this RetainableByteBuffer to the given byte array.- Specified by:
get
in interfaceRetainableByteBuffer
- 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
-
isDirect
public boolean isDirect()- Specified by:
isDirect
in interfaceRetainableByteBuffer
- Returns:
- whether the
ByteBuffer
is direct
-
hasRemaining
public boolean hasRemaining()- Specified by:
hasRemaining
in interfaceRetainableByteBuffer
- Returns:
- whether the
ByteBuffer
has remaining bytes
-
skip
public long skip(long length) Description copied from interface:RetainableByteBuffer
Skips, advancing the ByteBuffer position, the given number of bytes.
- Specified by:
skip
in interfaceRetainableByteBuffer
- Parameters:
length
- the maximum number of bytes to skip- Returns:
- the number of bytes actually skipped
-
limit
public void limit(long limit) Description copied from interface:RetainableByteBuffer
Limit this buffer's contents to the size.
- Specified by:
limit
in interfaceRetainableByteBuffer
- Parameters:
limit
- the new size of the buffer
-
slice
Description copied from interface:RetainableByteBuffer
Get a slice of the buffer.- Specified by:
slice
in interfaceRetainableByteBuffer
- Returns:
- A sliced
RetainableByteBuffer
sharing this buffers data and reference count, but with independent position. The buffer isretained
by this call. - See Also:
-
slice
Description copied from interface:RetainableByteBuffer
Get a partial slice of the buffer. This is equivalent toRetainableByteBuffer.slice()
.RetainableByteBuffer.limit(long)
, but may be implemented more efficiently.- Specified by:
slice
in interfaceRetainableByteBuffer
- 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.
-
space
public long space()- Specified by:
space
in interfaceRetainableByteBuffer
- Returns:
- the number of bytes that can be added, appended or put into this buffer,
assuming it is
mutable
.
-
isFull
public boolean isFull()- Specified by:
isFull
in interfaceRetainableByteBuffer
- Returns:
- true if no more bytes can be added, appended or put to this buffer,
assuming it is
mutable
.
-
copy
Description copied from interface:RetainableByteBuffer
Creates a deep copy of this RetainableByteBuffer that is entirely independent- Specified by:
copy
in interfaceRetainableByteBuffer
- Returns:
- A copy of this RetainableByteBuffer
-
remaining
public int remaining()- Specified by:
remaining
in interfaceRetainableByteBuffer
- Returns:
Integer.MAX_VALUE
if the length of thisAccumulator
is greater thanInteger.MAX_VALUE
- See Also:
-
size
public long size()- Specified by:
size
in interfaceRetainableByteBuffer
- Returns:
- the number of remaining bytes in the
ByteBuffer
- See Also:
-
capacity
public int capacity()- Specified by:
capacity
in interfaceRetainableByteBuffer
- Returns:
Integer.MAX_VALUE
if the maxLength of thisAccumulator
is greater thanInteger.MAX_VALUE
.- See Also:
-
maxSize
public long maxSize()- Specified by:
maxSize
in interfaceRetainableByteBuffer
- Returns:
- the maximum size in bytes.
- See Also:
-
release
public boolean release()Description copied from interface:Retainable
Releases this resource, potentially decrementing a reference count (if any).
- Specified by:
release
in interfaceRetainable
- Overrides:
release
in classRetainable.Wrapper
- Returns:
true
when the reference count goes to zero or if there was no reference count,false
otherwise.
-
clear
public void clear()- Specified by:
clear
in interfaceRetainableByteBuffer
- See Also:
-
append
Description copied from interface:RetainableByteBuffer.Mutable
Copies the contents of the given byte buffer to the end of this buffer, growing this buffer if necessary and possible.- Specified by:
append
in interfaceRetainableByteBuffer.Mutable
- Parameters:
bytes
- the byte buffer to copy from, which is consumed.- Returns:
- true if all bytes of the given buffer were copied, false otherwise.
- See Also:
-
append
Description copied from interface:RetainableByteBuffer.Mutable
Retain or copy the contents of the given retainable byte buffer to the end of this buffer, growing this buffer if necessary and possible. The implementation will heuristically decide to retain or copy the contents Unlike the similarRetainableByteBuffer.Mutable.add(RetainableByteBuffer)
, implementations of this method mustRetainable.retain()
the passed buffer if they keep a reference to it.- Specified by:
append
in interfaceRetainableByteBuffer.Mutable
- Parameters:
retainableBytes
- the retainable byte buffer to copy from, which is consumed.- Returns:
- true if all bytes of the given buffer were copied, false otherwise.
- See Also:
-
add
public RetainableByteBuffer.Mutable add(ByteBuffer bytes) throws ReadOnlyBufferException, BufferOverflowException Description copied from interface:RetainableByteBuffer.Mutable
Add the passedByteBuffer
to this buffer, growing this buffer if necessary and possible. The sourceByteBuffer
is passed by reference and the caller gives up "ownership", so implementations of this method may choose to avoid copies by keeping a reference to the buffer.- Specified by:
add
in interfaceRetainableByteBuffer.Mutable
- Parameters:
bytes
- the byte buffer to add, which is passed by reference and is not necessarily consumed by the add.- Returns:
this
buffer.- Throws:
ReadOnlyBufferException
- if this buffer is read only.BufferOverflowException
- if this buffer cannot fit the byte- See Also:
-
add
public RetainableByteBuffer.Mutable add(RetainableByteBuffer bytes) throws ReadOnlyBufferException, BufferOverflowException Description copied from interface:RetainableByteBuffer.Mutable
Add the passedRetainableByteBuffer
to this buffer, growing this buffer if necessary and possible. The sourceRetainableByteBuffer
is passed by reference and the caller gives up ownership, so implementations of this method may avoid copies by keeping a reference to the buffer. Unlike the similarRetainableByteBuffer.Mutable.append(RetainableByteBuffer)
and contrary to the general rules ofRetainable
, implementations of this method need not callRetainable.retain()
if keeping a reference, but they must ultimately callRetainable.release()
the passed buffer. Callers should useadd
rather thanRetainableByteBuffer.Mutable.append(RetainableByteBuffer)
if they already have an obligation to release the buffer and wish to delegate that obligation to this buffer.- Specified by:
add
in interfaceRetainableByteBuffer.Mutable
- Parameters:
bytes
- the byte buffer to add, which is passed by reference and is not necessarily consumed by the add.- Returns:
this
buffer.- Throws:
ReadOnlyBufferException
- if this buffer is read only.BufferOverflowException
- if this buffer cannot fit the byte
-
put
Description copied from interface:RetainableByteBuffer.Mutable
Put abyte
to the buffer, growing this buffer if necessary and possible.- Specified by:
put
in interfaceRetainableByteBuffer.Mutable
- Parameters:
b
- thebyte
to put- Returns:
this
buffer.
-
put
Description copied from interface:RetainableByteBuffer.Mutable
Put abyte
to the buffer at a given index.- Specified by:
put
in interfaceRetainableByteBuffer.Mutable
- Parameters:
index
- The index relative to the current start of unconsumed data in the buffer.b
- thebyte
to put- Returns:
this
buffer.
-
putShort
Description copied from interface:RetainableByteBuffer.Mutable
Put ashort
to the buffer, growing this buffer if necessary and possible.- Specified by:
putShort
in interfaceRetainableByteBuffer.Mutable
- Parameters:
s
- theshort
to put- Returns:
this
buffer.
-
putInt
Description copied from interface:RetainableByteBuffer.Mutable
Put anint
to the buffer, growing this buffer if necessary and possible.- Specified by:
putInt
in interfaceRetainableByteBuffer.Mutable
- Parameters:
i
- theint
to put- Returns:
this
buffer.
-
putLong
Description copied from interface:RetainableByteBuffer.Mutable
Put along
to the buffer, growing this buffer if necessary and possible.- Specified by:
putLong
in interfaceRetainableByteBuffer.Mutable
- Parameters:
l
- thelong
to put- Returns:
this
buffer.
-
put
Description copied from interface:RetainableByteBuffer.Mutable
Put abyte
array to the buffer, growing this buffer if necessary and possible.- Specified by:
put
in interfaceRetainableByteBuffer.Mutable
- Parameters:
bytes
- thebyte
array to putoffset
- the offset into the arraylength
- the length in bytes to put- Returns:
this
buffer.
-
appendTo
Description copied from interface:RetainableByteBuffer
Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.- Specified by:
appendTo
in interfaceRetainableByteBuffer
- Parameters:
to
- 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
Description copied from interface:RetainableByteBuffer
Appends and consumes the contents of this buffer to the passed buffer, limited by the capacity of the target buffer.- Specified by:
appendTo
in interfaceRetainableByteBuffer
- Parameters:
to
- 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:
-
putTo
Description copied from interface:RetainableByteBuffer
Consumes and puts the contents of this retainable byte buffer at the end of the given byte buffer.- Specified by:
putTo
in interfaceRetainableByteBuffer
- Parameters:
toInfillMode
- the destination buffer, whose position is updated.- See Also:
-
writeTo
Description copied from interface:RetainableByteBuffer
Asynchronously writes and consumes the contents of this retainable byte buffer into the given sink.- Specified by:
writeTo
in interfaceRetainableByteBuffer
- Parameters:
sink
- the destination sink.last
- true if this is the last write.callback
- the callback to call upon the write completion.- See Also:
-
addExtraStringInfo
- Overrides:
addExtraStringInfo
in classRetainableByteBuffer.Abstract
-
addValueString
- Overrides:
addValueString
in classRetainableByteBuffer.Abstract
-
addValueMarker
- Overrides:
addValueMarker
in classRetainableByteBuffer.Abstract
-