Class ContentSourceTransformer
- All Implemented Interfaces:
Content.Source
This abstract Content.Source
wraps another Content.Source
and implementers need only
to implement the transform(Content.Chunk)
method, which is used to transform Content.Chunk
read from the wrapped source.
The demand(Runnable)
conversation is passed directly to the wrapped Content.Source
,
which means that transformations that may fully consume bytes read can result in a null return from
Content.Source.read()
even after a callback to the demand Runnable
(as per spurious
invocation in Content.Source.demand(Runnable)
.
-
Constructor Summary
ModifierConstructorDescriptionprotected
ContentSourceTransformer
(Content.Source rawSource) protected
ContentSourceTransformer
(Content.Source rawSource, SerializedInvoker invoker) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Demands to invoke the given demand callback parameter when a chunk of content is available.void
Fails this content source with alast
failure chunk
, failing and discarding accumulated content chunks that were not yet read.protected Content.Source
read()
Reads a chunk of content.protected abstract Content.Chunk
transform
(Content.Chunk inputChunk) Transforms the input chunk parameter into an output chunk.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.io.Content.Source
fail, getLength, rewind
-
Constructor Details
-
ContentSourceTransformer
-
ContentSourceTransformer
-
-
Method Details
-
getContentSource
-
read
Description copied from interface:Content.Source
Reads a chunk of content.
See how to use this method idiomatically.
The returned chunk could be:
null
, to signal that there isn't a chunk of content available- an
Content.Chunk
instance with non nullContent.Chunk.getFailure()
, to signal that there was a failure trying to produce a chunk of content, or that the content production has beenfailed
externally - a
Content.Chunk
instance, containing the chunk of content.
Once a read returns an
Content.Chunk
instance with non-nullContent.Chunk.getFailure()
then if the failure islast
further reads will continue to return the same failure chunk instance, otherwise furtherread()
operations may return different non-failure chunks.Once a read returns a
last chunk
, further reads will continue to return a last chunk (although the instance may be different).The content reader code must ultimately arrange for a call to
Retainable.release()
on the returnedContent.Chunk
.Additionally, prior to the ultimate call to
Retainable.release()
, the reader code may make additional calls toRetainable.retain()
, that must ultimately be matched by a correspondent number of calls toRetainable.release()
.Concurrent reads from different threads are not recommended, as they are inherently in a race condition.
Reads performed outside the invocation context of a
demand callback
are allowed. However, reads performed with a pending demand are inherently in a race condition (the thread that reads with the thread that invokes the demand callback).- Specified by:
read
in interfaceContent.Source
- Returns:
- a chunk of content, possibly a failure instance, or
null
- See Also:
-
demand
Description copied from interface:Content.Source
Demands to invoke the given demand callback parameter when a chunk of content is available.
See how to use this method idiomatically.
Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the
Runnable
callback and a call to this method. Invocations of the passedRunnable
are serialized and a callback fordemand
call is not invoked until any previousdemand
callback has returned. Thus theRunnable
should not block waiting for a callback of a future demand call.The demand callback may be invoked spuriously: a subsequent call to
Content.Source.read()
may returnnull
.Calling this method establishes a pending demand, which is fulfilled when the demand callback is invoked.
Calling this method when there is already a pending demand results in an
IllegalStateException
to be thrown.If the invocation of the demand callback throws an exception, then
Content.Source.fail(Throwable)
is called.- Specified by:
demand
in interfaceContent.Source
- Parameters:
demandCallback
- the demand callback to invoke where there is a content chunk available- See Also:
-
fail
Description copied from interface:Content.Source
Fails this content source with a
last
failure chunk
, failing and discarding accumulated content chunks that were not yet read.The failure may be notified to the content reader at a later time, when the content reader reads a content chunk, via a
Content.Chunk
instance with a non nullContent.Chunk.getFailure()
.If
Content.Source.read()
has returned a last chunk, this is a no operation.Typical failure: the content being aborted by user code, or idle timeouts.
If this method has already been called, then it is a no operation.
- Specified by:
fail
in interfaceContent.Source
- Parameters:
failure
- the cause of the failure- See Also:
-
transform
Transforms the input chunk parameter into an output chunk.
When this method produces a non-
null
, non-last chunk, it is subsequently invoked with anull
input chunk to try to produce more output chunks from the previous input chunk. For example, a single compressed input chunk may be transformed into multiple uncompressed output chunks.The input chunk is released as soon as this method returns, so implementations that must hold onto the input chunk must arrange to call
Retainable.retain()
and its correspondentRetainable.release()
.Implementations should return an
Content.Chunk
with non-nullContent.Chunk.getFailure()
in case of transformation errors.Exceptions thrown by this method are equivalent to returning an error chunk.
Implementations of this method may return:
null
, if more input chunks are necessary to produce an output chunk- the
inputChunk
itself, typically in case of non-nullContent.Chunk.getFailure()
, or when no transformation is required - a new
Content.Chunk
derived frominputChunk
.
- Parameters:
inputChunk
- a chunk read from the wrappedContent.Source
- Returns:
- a transformed chunk or
null
-