- java.lang.Object
-
- io.helidon.webserver.ByteBufDataChunk
-
- All Implemented Interfaces:
DataChunk
,Iterable<ByteBuffer>
public class ByteBufDataChunk extends Object implements DataChunk
A special DataChunk implementation based on Netty's buffers. This is used by our Jersey SPI implementation to take advantage of Netty's buffer pooling.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
bytes()
Gets the content of the underlying byte buffers as an array of bytes.static ByteBufDataChunk
create(boolean flush, boolean readOnly, io.netty.buffer.ByteBuf... byteBufs)
Creates an instance given an array ofByteBuf
's.static ByteBufDataChunk
create(boolean flush, boolean readOnly, Runnable releaseCallback, io.netty.buffer.ByteBuf... byteBufs)
Creates an instance given an array ofByteBuf
's.ByteBuffer[]
data()
This method is needed for testing some of our examples.<T> T[]
data(Class<T> clazz)
Returns a representation of this chunk as an array of T's.DataChunk
duplicate()
Makes a copy of this data chunk including its underlyingByteBuffer
.boolean
flush()
Returnstrue
if all caches are requested to flush when this chunk is written.<T> boolean
isBackedBy(Class<T> clazz)
Checks if this instance is backed by buffers of a certain kind.boolean
isReadOnly()
Returnstrue
if the underlying byte buffer of this chunk is read only orfalse
otherwise.boolean
isReleased()
Whether this chunk is released and the associated data structures returned by methods (such asDataChunk.iterator()
orDataChunk.bytes()
) should not be used.Iterator<ByteBuffer>
iterator()
void
release()
Releases this chunk.int
remaining()
Returns the sum of elements between the current position and the limit of each of the underlying ByteBuffer.Optional<CompletableFuture<DataChunk>>
writeFuture()
Returns a write future associated with this chunk.void
writeFuture(CompletableFuture<DataChunk> writeFuture)
Set a write future that will complete when data chunk has been written to a connection.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.helidon.common.http.DataChunk
id, isFlushChunk
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
create
public static ByteBufDataChunk create(boolean flush, boolean readOnly, io.netty.buffer.ByteBuf... byteBufs)
Creates an instance given an array ofByteBuf
's.- Parameters:
flush
- a signal that this chunk should be written and flushed from any cache if possiblereadOnly
- marks this buffer as read onlybyteBufs
- the data for this chunk. Should not be reused untilreleaseCallback
is used- Returns:
- new chunk
-
create
public static ByteBufDataChunk create(boolean flush, boolean readOnly, Runnable releaseCallback, io.netty.buffer.ByteBuf... byteBufs)
Creates an instance given an array ofByteBuf
's.- Parameters:
flush
- a signal that this chunk should be written and flushed from any cache if possiblereadOnly
- marks this buffer as read onlyreleaseCallback
- a callback which is called when this chunk is completely processedbyteBufs
- the data for this chunk. Should not be reused untilreleaseCallback
is used- Returns:
- new chunk
-
isBackedBy
public <T> boolean isBackedBy(Class<T> clazz)
Description copied from interface:DataChunk
Checks if this instance is backed by buffers of a certain kind.- Specified by:
isBackedBy
in interfaceDataChunk
- Type Parameters:
T
- the buffer type- Parameters:
clazz
- a buffer class instance- Returns:
- outcome of test
-
data
public <T> T[] data(Class<T> clazz)
Description copied from interface:DataChunk
Returns a representation of this chunk as an array of T's.
-
isReleased
public boolean isReleased()
Description copied from interface:DataChunk
Whether this chunk is released and the associated data structures returned by methods (such asDataChunk.iterator()
orDataChunk.bytes()
) should not be used. The implementations may choose to not implement this optimization and to never mutate the underlying memory; in such case this method does no-op.Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.
- Specified by:
isReleased
in interfaceDataChunk
- Returns:
- whether this chunk has been released, defaults to false
-
flush
public boolean flush()
Description copied from interface:DataChunk
Returnstrue
if all caches are requested to flush when this chunk is written. This method is only meaningful when handing data over to Helidon APIs (e.g. for server response and client requests).
-
isReadOnly
public boolean isReadOnly()
Description copied from interface:DataChunk
Returnstrue
if the underlying byte buffer of this chunk is read only orfalse
otherwise.- Specified by:
isReadOnly
in interfaceDataChunk
- Returns:
- Immutability outcome.
-
release
public void release()
Description copied from interface:DataChunk
Releases this chunk. The underlying data as well as the data structure instances returned by methodsDataChunk.bytes()
andDataChunk.iterator()
may become stale and should not be used anymore. The implementations may choose to not implement this optimization and to never mutate the underlying memory; in such case this method does no-op.Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.
-
writeFuture
public void writeFuture(CompletableFuture<DataChunk> writeFuture)
Description copied from interface:DataChunk
Set a write future that will complete when data chunk has been written to a connection.- Specified by:
writeFuture
in interfaceDataChunk
- Parameters:
writeFuture
- Write future.
-
writeFuture
public Optional<CompletableFuture<DataChunk>> writeFuture()
Description copied from interface:DataChunk
Returns a write future associated with this chunk.- Specified by:
writeFuture
in interfaceDataChunk
- Returns:
- Write future if one has ben set.
-
remaining
public int remaining()
Description copied from interface:DataChunk
Returns the sum of elements between the current position and the limit of each of the underlying ByteBuffer.
-
data
public ByteBuffer[] data()
This method is needed for testing some of our examples. It bypasses the optimization for which this class was created.
-
duplicate
public DataChunk duplicate()
Description copied from interface:DataChunk
Makes a copy of this data chunk including its underlyingByteBuffer
. This may be necessary for caching in caseByteBuffer.rewind()
is called to reuse a byte buffer. Note that only the actual bytes used in the data chunk are copied, the resulting data chunk's capacity may be less than the original.
-
bytes
public byte[] bytes()
Description copied from interface:DataChunk
Gets the content of the underlying byte buffers as an array of bytes. The returned array contains only the part of data that wasn't read yet. Calling this method doesn't cause the underlying byte buffers to be read.It is expected the returned byte array holds a reference to data that will become stale upon calling method
DataChunk.release()
. (For instance, the memory segment is pooled by the underlying TCP server and is reused for a subsequent request chunk.) The idea behind this class is to be able to minimize data copying; ideally, in order to achieve the best performance, to not copy them at all. However, the implementations may choose otherwise.Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.
- Specified by:
bytes
in interfaceDataChunk
- Returns:
- an array of bytes that is guarantied to stay immutable as long as
method
DataChunk.release()
is not called
-
iterator
public Iterator<ByteBuffer> iterator()
- Specified by:
iterator
in interfaceDataChunk
- Specified by:
iterator
in interfaceIterable<ByteBuffer>
-
-