Class BaseSharedBuffer<C extends BufferConsumer,F>

java.lang.Object
io.micronaut.http.body.stream.BaseSharedBuffer<C,F>
Type Parameters:
C - The BufferConsumer type for the specific buffer type
F - The type returned to subscribeFull0(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

@Internal public abstract class BaseSharedBuffer<C extends BufferConsumer,F> extends Object
Base type for a shared buffer that distributes a single BufferConsumer input to multiple streaming ByteBodys.
The subclass handles concurrency (for netty, event loop) and the specific buffer type (for netty, ByteBuf).
  • Constructor Details

  • Method Details

    • failClaim

      @Contract("-> fail") public static void failClaim()
    • logClaim

      public static void logClaim()
    • getExpectedLength

      public final OptionalLong getExpectedLength()
      Get the exact body length, if available. This is either set from Content-Length or when the body is fully buffered.
      Returns:
      The expected body length
    • getLimits

      public final BodySizeLimits getLimits()
    • getRootUpstream

      public final BufferConsumer.Upstream getRootUpstream()
    • setExpectedLengthFrom

      public final void setExpectedLengthFrom(String contentLength)
    • 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

      protected abstract void forwardInitialBuffer(@Nullable C subscriber, boolean last)
      Forward any already-buffered data to the given new subscriber.
      Parameters:
      subscriber - The new subscriber, or null if the reservation has been cancelled and the data can just be discarded
      last - 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

      protected abstract F subscribeFullResult(boolean last)
      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

      protected final void subscribe0(@Nullable C subscriber, BufferConsumer.Upstream specificUpstream)
      Add a subscriber. Must be preceded by a reservation.
      Not thread safe, caller must handle concurrency.
      Parameters:
      subscriber - The subscriber to add. Can be null, then the bytes will just be discarded
      specificUpstream - 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 of subscribe0(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 the targetFlow parameter IF canReturnImmediate is false. If canReturnImmediate is true, this method will SOMETIMES return an immediate ExecutionFlow instead as an optimization.
      Parameters:
      targetFlow - The delayed flow to use if canReturnImmediate is false and/or we have to wait for the result
      specificUpstream - The upstream for the subscriber. This is used to call allowDiscard if there was an error
      canReturnImmediate - Whether we can return an immediate ExecutionFlow instead of targetFlow, when appropriate
      Returns:
      A flow that will complete when all data has arrived, with a buffer containing that data
    • addForward

      protected abstract void addForward(List<C> consumers)
      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 the BufferConsumer 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 to addBuffer(), addDoNotBuffer() and addBuffer(). 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 of BufferConsumer.complete().
      Not thread safe, caller must handle concurrency.
    • error

      public void error(Throwable e)
      Implementation of BufferConsumer.error(Throwable).
      Not thread safe, caller must handle concurrency.
      Parameters:
      e - The error