Class ReadableEntityBase
java.lang.Object
io.helidon.http.media.ReadableEntityBase
- All Implemented Interfaces:
ReadableEntity
- Direct Known Subclasses:
ClientResponseEntity, Http2ServerRequestEntity, ServerRequestEntity
Base for readable entities.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedReadableEntityBase(Consumer<Boolean> entityRequestedCallback, Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize) Create a new base.protectedReadableEntityBase(Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize) Create a new base. -
Method Summary
Modifier and TypeMethodDescriptionfinal <T> Tas(GenericType<T> type) Get the entity as a specific type.<T> TGet the entity as a specific class.<T> Optional<T> asOptional(GenericType<T> type) Get the entity as a specific type (optional).<T> Optional<T> asOptional(Class<T> type) Get the entity as a specific class (optional).voidbuffer()If called beforeReadableEntity.inputStream(), it will buffer the entity and support multiple calls toReadableEntity.inputStream()after that.protected booleanWhether an entity has been buffered and can be read again.protected RuntimeExceptionException to throw when explicit buffering exceeds its configured size.voidconsume()Consume the entity if not yet consumed.booleanconsumed()Whether this entity has been consumed already.static ReadableEntityempty()Create a new empty readable entity (when there is no entity).protected abstract <T> TentityAs(GenericType<T> type) Read entity as a specific generic type.protected RunnableRunnable to run once the entity is fully consumed.booleanWhether an entity actually exists.Input stream to read bytes of the entity.protected RuntimeExceptionException to throw when a materializing reader exceeds its configured size limit.protected longMaximum entity length for materializing reads and explicit buffering.protected byte[]Read all bytes of the entity into memory.protected <T> TreadEntity(EntityReader<T> reader, GenericType<T> type, Headers headers) Read a server request with the selected media reader, enforcing the materialization limit.protected <T> TreadEntity(EntityReader<T> reader, GenericType<T> type, Headers requestHeaders, Headers responseHeaders) Read a client response with the selected media reader, enforcing the materialization limit.protected Function<Integer, BufferData> Function to request entity bytes.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ReadableEntity
copyModifier and TypeMethodDescriptionCopy this entity and add a new runnable to be executed after this entity is consumed.
-
Constructor Details
-
ReadableEntityBase
protected ReadableEntityBase(Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize) Create a new base.- Parameters:
readEntityFunction- accepts estimate of needed bytes, returns buffer data (the length of buffer data may differ from the estimate)entityProcessedRunnable- runnable to run when entity is fully readmaxBufferedEntitySize- maximum size of a buffered entity
-
ReadableEntityBase
protected ReadableEntityBase(Consumer<Boolean> entityRequestedCallback, Function<Integer, BufferData> readEntityFunction, Runnable entityProcessedRunnable, long maxBufferedEntitySize) Create a new base.- Parameters:
entityRequestedCallback- callback invoked when entity is requestedreadEntityFunction- accepts estimate of needed bytes, returns buffer data (the length of buffer data may differ from the estimate)entityProcessedRunnable- runnable to run when entity is fully readmaxBufferedEntitySize- maximum length of a buffered entity
-
-
Method Details
-
empty
Create a new empty readable entity (when there is no entity).- Returns:
- empty readable entity
-
inputStream
Description copied from interface:ReadableEntityInput stream to read bytes of the entity. Cannot be combined with methodsReadableEntity.as(Class)orReadableEntity.as(io.helidon.common.GenericType)If there is no entity, returns input stream on empty byte array. This stream is not limited bymax-buffered-entity-size.- Specified by:
inputStreamin interfaceReadableEntity- Returns:
- input stream to entity bytes
-
buffer
public void buffer()Description copied from interface:ReadableEntityIf called beforeReadableEntity.inputStream(), it will buffer the entity and support multiple calls toReadableEntity.inputStream()after that. Normally,ReadableEntity.inputStream()can only be called at most once. On a server request, this buffers decoded bytes and applies both the protocol'smax-buffered-entity-sizeand the configuredmax-payload-sizebefore allowing the entity to be read again.- Specified by:
bufferin interfaceReadableEntity
-
as
Description copied from interface:ReadableEntityGet the entity as a specific class. The entity will useMediaContextto find correct media mapper.- Specified by:
asin interfaceReadableEntity- Type Parameters:
T- type of the entity- Parameters:
type- class of the entity- Returns:
- entity correctly typed
-
asOptional
Description copied from interface:ReadableEntityGet the entity as a specific class (optional). The entity will useMediaContextto find correct media mapper.- Specified by:
asOptionalin interfaceReadableEntity- Type Parameters:
T- type of the entity- Parameters:
type- class of the entity- Returns:
- entity correctly typed, or an empty optional if entity is not present
-
as
Description copied from interface:ReadableEntityGet the entity as a specific type. The entity will useMediaContextto find correct media mapper.- Specified by:
asin interfaceReadableEntity- Type Parameters:
T- type of the entity- Parameters:
type- generic type of the entity- Returns:
- entity correctly typed
-
asOptional
Description copied from interface:ReadableEntityGet the entity as a specific type (optional). The entity will useMediaContextto find correct media mapper.- Specified by:
asOptionalin interfaceReadableEntity- Type Parameters:
T- type of the entity- Parameters:
type- generic type of the entity- Returns:
- entity correctly typed, or an empty optional if entity is not present
-
consumed
public boolean consumed()Description copied from interface:ReadableEntityWhether this entity has been consumed already.- Specified by:
consumedin interfaceReadableEntity- Returns:
trueif the entity is already consumed; a consumed entity cannot be consumed again
-
consume
public void consume()Description copied from interface:ReadableEntityConsume the entity if not yet consumed. Reads all bytes from the entity and discards them. If entity is larger than allowed, appropriate HTTP exception is thrown. If entity is already consumed, this is a no-op.- Specified by:
consumein interfaceReadableEntity
-
hasEntity
public boolean hasEntity()Description copied from interface:ReadableEntityWhether an entity actually exists.- Specified by:
hasEntityin interfaceReadableEntity- Returns:
trueif an entity exists and can be read
-
entityAs
Read entity as a specific generic type.- Type Parameters:
T- type of the entity- Parameters:
type- type the entity should be coerced into- Returns:
- entity value
-
readAllBytes
protected byte[] readAllBytes()Read all bytes of the entity into memory.- Returns:
- all bytes of the entity
-
readEntityFunction
Function to request entity bytes.- Returns:
- function that accepts an integer with a suggestion of how many bytes are requested, and returns a buffer
-
entityProcessedRunnable
Runnable to run once the entity is fully consumed.- Returns:
- runnable to run on consumed
-
maxBufferedEntitySize
protected long maxBufferedEntitySize()Maximum entity length for materializing reads and explicit buffering.- Returns:
- maximum number of input bytes to materialize
-
bufferedEntityAvailable
protected boolean bufferedEntityAvailable()Whether an entity has been buffered and can be read again. An entity implementation that decodes its input stream must not decode this buffer again.- Returns:
- whether a buffered entity is available
-
bufferedEntityLimitExceeded
Exception to throw when explicit buffering exceeds its configured size.- Returns:
- exception for a buffered entity that exceeds the limit
-
materializedEntityLimitExceeded
Exception to throw when a materializing reader exceeds its configured size limit. Server request implementations override this to produce an HTTP error response.- Returns:
- exception for a materialized entity that exceeds the limit
-
readEntity
Read a server request with the selected media reader, enforcing the materialization limit. If a reader wraps or suppresses a materialization-limit failure, the original limit failure is thrown. Streaming readers retain responsibility for stream ownership as described byEntityReader.streaming().- Type Parameters:
T- type of the entity- Parameters:
reader- the selected media readertype- type of the entityheaders- request headers- Returns:
- entity value
-
readEntity
protected <T> T readEntity(EntityReader<T> reader, GenericType<T> type, Headers requestHeaders, Headers responseHeaders) Read a client response with the selected media reader, enforcing the materialization limit. If a reader wraps or suppresses a materialization-limit failure, the original limit failure is thrown. Streaming readers retain responsibility for stream ownership as described byEntityReader.streaming().- Type Parameters:
T- type of the entity- Parameters:
reader- the selected media readertype- type of the entityrequestHeaders- request headersresponseHeaders- response headers- Returns:
- entity value
-