- 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 ByteBufDataChunkcreate(boolean flush, boolean readOnly, io.netty.buffer.ByteBuf... byteBufs)Creates an instance given an array ofByteBuf's.static ByteBufDataChunkcreate(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.DataChunkduplicate()Makes a copy of this data chunk including its underlyingByteBuffer.booleanflush()Returnstrueif all caches are requested to flush when this chunk is written.<T> booleanisBackedBy(Class<T> clazz)Checks if this instance is backed by buffers of a certain kind.booleanisReadOnly()Returnstrueif the underlying byte buffer of this chunk is read only orfalseotherwise.booleanisReleased()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()voidrelease()Releases this chunk.intremaining()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.voidwriteFuture(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 untilreleaseCallbackis 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 untilreleaseCallbackis used- Returns:
- new chunk
-
isBackedBy
public <T> boolean isBackedBy(Class<T> clazz)
Description copied from interface:DataChunkChecks if this instance is backed by buffers of a certain kind.- Specified by:
isBackedByin 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:DataChunkReturns a representation of this chunk as an array of T's.
-
isReleased
public boolean isReleased()
Description copied from interface:DataChunkWhether 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:
isReleasedin interfaceDataChunk- Returns:
- whether this chunk has been released, defaults to false
-
flush
public boolean flush()
Description copied from interface:DataChunkReturnstrueif 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:DataChunkReturnstrueif the underlying byte buffer of this chunk is read only orfalseotherwise.- Specified by:
isReadOnlyin interfaceDataChunk- Returns:
- Immutability outcome.
-
release
public void release()
Description copied from interface:DataChunkReleases 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:DataChunkSet a write future that will complete when data chunk has been written to a connection.- Specified by:
writeFuturein interfaceDataChunk- Parameters:
writeFuture- Write future.
-
writeFuture
public Optional<CompletableFuture<DataChunk>> writeFuture()
Description copied from interface:DataChunkReturns a write future associated with this chunk.- Specified by:
writeFuturein interfaceDataChunk- Returns:
- Write future if one has ben set.
-
remaining
public int remaining()
Description copied from interface:DataChunkReturns 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:DataChunkMakes 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:DataChunkGets 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:
bytesin 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:
iteratorin interfaceDataChunk- Specified by:
iteratorin interfaceIterable<ByteBuffer>
-
-