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- TheBufferConsumertype 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:
ReactiveByteBufferByteBody.SharedBuffer,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 ByteBodys.The subclass handles concurrency (for netty, event loop) and the specific buffer type (for netty, ByteBuf).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class -
Constructor Summary
ConstructorsConstructorDescriptionBaseSharedBuffer(BodySizeLimits limits, BufferConsumer.Upstream rootUpstream) -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidadd(int n) This method implements theBufferConsumeraddlogic in a buffer-type-independent way.protected abstract voidBuffer the input buffer.protected voidDo not buffer the input buffer.protected abstract voidaddForward(List<C> consumers) Forward the input buffer to the given list of consumers.protected voidafterSubscribe(boolean last) Called after a subscribe operation.voidcomplete()Implementation ofBufferConsumer.complete().
Not thread safe, caller must handle concurrency.protected abstract voidDiscard the previously buffered bytes.voidImplementation ofBufferConsumer.error(Throwable).
Not thread safe, caller must handle concurrency.static voidprotected abstract voidforwardInitialBuffer(C subscriber, boolean last) Forward any already-buffered data to the given new subscriber.final OptionalLongGet the exact body length, if available.final BodySizeLimitsfinal BufferConsumer.Upstreamstatic voidlogClaim()protected voidreserve0()Reserve a spot for a future subscribe operation.
Not thread safe, caller must handle concurrency.final voidsetExpectedLength(long length) final voidsetExpectedLengthFrom(String contentLength) protected final voidsubscribe0(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 FsubscribeFullResult(boolean last) Called aftercompletionto 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-Lengthor 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, ornullif the reservation has been cancelled and the data can just be discardedlast-trueiff 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-trueiff this was the last reservation
-
subscribeFullResult
Called aftercompletionto 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-trueiff 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 thetargetFlowparameter IFcanReturnImmediateis false. IfcanReturnImmediateis true, this method will SOMETIMES return an immediate ExecutionFlow instead as an optimization.- Parameters:
targetFlow- The delayed flow to use ifcanReturnImmediateis 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 theBufferConsumeraddlogic 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
-