Package io.micronaut.http.body.stream
Class BaseSharedBuffer<C extends BufferConsumer,F>
java.lang.Object
io.micronaut.http.body.stream.BaseSharedBuffer<C,F>
- Type Parameters:
C
- TheBufferConsumer
type for the specific buffer typeF
- The type returned tosubscribeFull0(io.micronaut.core.execution.DelayedExecutionFlow<F>, io.micronaut.http.body.stream.BufferConsumer.Upstream, boolean)
subscribers. This is usually just the buffer type
- Direct Known Subclasses:
StreamingNettyByteBody.SharedBuffer
Base type for a shared buffer that distributes a single
The subclass handles concurrency (for netty, event loop) and the specific buffer type (for netty, ByteBuf).
BufferConsumer
input to multiple
streaming ByteBody
s.The subclass handles concurrency (for netty, event loop) and the specific buffer type (for netty, ByteBuf).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
-
Constructor Summary
ConstructorDescriptionBaseSharedBuffer
(BodySizeLimits limits, BufferConsumer.Upstream rootUpstream) -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
add
(int n) This method implements theBufferConsumer
add
logic in a buffer-type-independent way.protected abstract void
Buffer the input buffer.protected void
Do not buffer the input buffer.protected abstract void
addForward
(List<C> consumers) Forward the input buffer to the given list of consumers.protected void
afterSubscribe
(boolean last) Called after a subscribe operation.void
complete()
Implementation ofBufferConsumer.complete()
.
Not thread safe, caller must handle concurrency.protected abstract void
Discard the previously buffered bytes.void
Implementation ofBufferConsumer.error(Throwable)
.
Not thread safe, caller must handle concurrency.static void
protected abstract void
forwardInitialBuffer
(C subscriber, boolean last) Forward any already-buffered data to the given new subscriber.final OptionalLong
Get the exact body length, if available.final BodySizeLimits
final BufferConsumer.Upstream
static void
logClaim()
protected void
reserve0()
Reserve a spot for a future subscribe operation.
Not thread safe, caller must handle concurrency.final void
setExpectedLength
(long length) final void
setExpectedLengthFrom
(String contentLength) protected final void
subscribe0
(C subscriber, BufferConsumer.Upstream specificUpstream) Add a subscriber.protected final ExecutionFlow<F>
subscribeFull0
(DelayedExecutionFlow<F> targetFlow, BufferConsumer.Upstream specificUpstream, boolean canReturnImmediate) Optimized version ofsubscribe0(C, io.micronaut.http.body.stream.BufferConsumer.Upstream)
for subscribers that want to buffer the full body.protected abstract F
subscribeFullResult
(boolean last) Called aftercompletion
to get the data that should be forwarded to asubscribeFull0(io.micronaut.core.execution.DelayedExecutionFlow<F>, io.micronaut.http.body.stream.BufferConsumer.Upstream, boolean)
subscriber.
-
Constructor Details
-
BaseSharedBuffer
-
-
Method Details
-
failClaim
@Contract("-> fail") public static void failClaim() -
logClaim
public static void logClaim() -
getExpectedLength
Get the exact body length, if available. This is either set fromContent-Length
or when the body is fully buffered.- Returns:
- The expected body length
-
getLimits
-
getRootUpstream
-
setExpectedLengthFrom
-
setExpectedLength
public final void setExpectedLength(long length) -
reserve0
protected void reserve0()Reserve a spot for a future subscribe operation.
Not thread safe, caller must handle concurrency. -
forwardInitialBuffer
Forward any already-buffered data to the given new subscriber.- Parameters:
subscriber
- The new subscriber, ornull
if the reservation has been cancelled and the data can just be discardedlast
-true
iff this was the last reservation and the buffer can be discarded after this call
-
afterSubscribe
protected void afterSubscribe(boolean last) Called after a subscribe operation. Used for leak detection.- Parameters:
last
-true
iff this was the last reservation
-
subscribeFullResult
Called aftercompletion
to get the data that should be forwarded to asubscribeFull0(io.micronaut.core.execution.DelayedExecutionFlow<F>, io.micronaut.http.body.stream.BufferConsumer.Upstream, boolean)
subscriber.- Parameters:
last
-true
iff this was the last reservation and the buffer can be discarded after this call- Returns:
- The full result that should be returned from the
subscribeFull0(io.micronaut.core.execution.DelayedExecutionFlow<F>, io.micronaut.http.body.stream.BufferConsumer.Upstream, boolean)
execution flow
-
subscribe0
Add a subscriber. Must be preceded by a reservation.
Not thread safe, caller must handle concurrency.- Parameters:
subscriber
- The subscriber to add. Can benull
, then the bytes will just be discardedspecificUpstream
- The upstream for the subscriber. This is used to call allowDiscard if there was an error
-
subscribeFull0
protected final ExecutionFlow<F> subscribeFull0(DelayedExecutionFlow<F> targetFlow, BufferConsumer.Upstream specificUpstream, boolean canReturnImmediate) Optimized version ofsubscribe0(C, io.micronaut.http.body.stream.BufferConsumer.Upstream)
for subscribers that want to buffer the full body. The returned flow will complete when the input is buffered. The returned flow will always be identical to thetargetFlow
parameter IFcanReturnImmediate
is false. IfcanReturnImmediate
is true, this method will SOMETIMES return an immediate ExecutionFlow instead as an optimization.- Parameters:
targetFlow
- The delayed flow to use ifcanReturnImmediate
is false and/or we have to wait for the resultspecificUpstream
- The upstream for the subscriber. This is used to call allowDiscard if there was an errorcanReturnImmediate
- Whether we can return an immediate ExecutionFlow instead oftargetFlow
, when appropriate- Returns:
- A flow that will complete when all data has arrived, with a buffer containing that data
-
addForward
Forward the input buffer to the given list of consumers.- Parameters:
consumers
- The consumers to forward the data to- See Also:
-
addDoNotBuffer
protected void addDoNotBuffer()Do not buffer the input buffer.- See Also:
-
addBuffer
protected abstract void addBuffer()Buffer the input buffer.- See Also:
-
discardBuffer
protected abstract void discardBuffer()Discard the previously buffered bytes.- See Also:
-
add
protected final void add(int n) This method implements theBufferConsumer
add
logic in a buffer-type-independent way. It is not thread-safe: The subclass must take care of concurrency. The caller should store the actual buffer to be added in a field, then call this method, and use the stored buffer in the calls this method makes toaddBuffer()
,addDoNotBuffer()
andaddBuffer()
. After this method completes, the field can be cleared and those methods will not be called again. Example:ByteBuf adding; public void add(ByteBuf buf) { this.adding = buf; add(buf.readableBytes()); this.adding = null; } @Override protected void addForward(List<ByteBufConsumer> consumers) { for (ByteBufConsumer c : consumers) { c.add(this.adding); } }
- Parameters:
n
- The number of bytes to add
-
complete
public void complete()Implementation ofBufferConsumer.complete()
.
Not thread safe, caller must handle concurrency. -
error
Implementation ofBufferConsumer.error(Throwable)
.
Not thread safe, caller must handle concurrency.- Parameters:
e
- The error
-