Class DeflatingBrotliEntityProducer

java.lang.Object
org.apache.hc.client5.http.async.methods.DeflatingBrotliEntityProducer
All Implemented Interfaces:
org.apache.hc.core5.http.EntityDetails, org.apache.hc.core5.http.nio.AsyncDataProducer, org.apache.hc.core5.http.nio.AsyncEntityProducer, org.apache.hc.core5.http.nio.ResourceHolder

public final class DeflatingBrotliEntityProducer extends Object implements org.apache.hc.core5.http.nio.AsyncEntityProducer
AsyncEntityProducer that Brotli-compresses bytes from an upstream producer on the fly and writes the compressed stream to the target DataStreamChannel.

Purely async/streaming: no InputStream/OutputStream. Back-pressure is honored via available() and the I/O reactor’s calls into produce(DataStreamChannel). Trailers from the upstream producer are preserved and emitted once the compressed output has been fully drained.

Content metadata

Returns Content-Encoding: br, Content-Length: -1 and chunked=true. Repeatability matches the upstream producer.

Implementation notes

Uses Brotli4j’s EncoderJNI.Wrapper. JNI-owned output buffers are written directly when possible; if the channel applies back-pressure, the unwritten tail is copied into small pooled direct ByteBuffers to reduce allocation churn. Native resources are released in releaseResources().

Ensure Brotli4jLoader.ensureAvailability() has been called once at startup; this class also invokes it in a static initializer as a safeguard.

Since:
5.6
See Also:
  • AsyncEntityProducer
  • DataStreamChannel
  • EncoderJNI
  • Field Details

    • upstream

      private final org.apache.hc.core5.http.nio.AsyncEntityProducer upstream
    • encoder

      private final com.aayushatharva.brotli4j.encoder.EncoderJNI.Wrapper encoder
    • pendingOut

      private ByteBuffer pendingOut
    • pendingTrailers

      private List<? extends org.apache.hc.core5.http.Header> pendingTrailers
    • state

  • Constructor Details

    • DeflatingBrotliEntityProducer

      public DeflatingBrotliEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer upstream, int quality, int lgwin, com.aayushatharva.brotli4j.encoder.Encoder.Mode mode) throws IOException
      Create a producer with explicit Brotli params.
      Parameters:
      upstream - upstream entity producer whose bytes will be compressed
      quality - Brotli quality level (see brotli4j documentation)
      lgwin - Brotli window size log2 (see brotli4j documentation)
      mode - Brotli mode hint (GENERIC/TEXT/FONT)
      Throws:
      IOException - if the native encoder cannot be created
      Since:
      5.6
    • DeflatingBrotliEntityProducer

      public DeflatingBrotliEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer upstream, int quality, int lgwin, int modeInt) throws IOException
      Convenience constructor mapping 0=GENERIC, 1=TEXT, 2=FONT.
      Throws:
      IOException
      Since:
      5.6
    • DeflatingBrotliEntityProducer

      public DeflatingBrotliEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer upstream) throws IOException
      Create a producer with sensible defaults (quality=5, lgwin=22, GENERIC).
      Throws:
      IOException
      Since:
      5.6
  • Method Details

    • getContentType

      public String getContentType()
      Specified by:
      getContentType in interface org.apache.hc.core5.http.EntityDetails
    • getContentEncoding

      public String getContentEncoding()
      Specified by:
      getContentEncoding in interface org.apache.hc.core5.http.EntityDetails
    • getContentLength

      public long getContentLength()
      Specified by:
      getContentLength in interface org.apache.hc.core5.http.EntityDetails
    • isChunked

      public boolean isChunked()
      Specified by:
      isChunked in interface org.apache.hc.core5.http.EntityDetails
    • getTrailerNames

      public Set<String> getTrailerNames()
      Specified by:
      getTrailerNames in interface org.apache.hc.core5.http.EntityDetails
    • isRepeatable

      public boolean isRepeatable()
      Specified by:
      isRepeatable in interface org.apache.hc.core5.http.nio.AsyncEntityProducer
    • available

      public int available()
      Specified by:
      available in interface org.apache.hc.core5.http.nio.AsyncDataProducer
    • produce

      public void produce(org.apache.hc.core5.http.nio.DataStreamChannel channel) throws IOException
      Specified by:
      produce in interface org.apache.hc.core5.http.nio.AsyncDataProducer
      Throws:
      IOException
    • failed

      public void failed(Exception cause)
      Specified by:
      failed in interface org.apache.hc.core5.http.nio.AsyncEntityProducer
    • releaseResources

      public void releaseResources()
      Specified by:
      releaseResources in interface org.apache.hc.core5.http.nio.ResourceHolder
    • flushPending

      private boolean flushPending(org.apache.hc.core5.http.nio.DataStreamChannel channel) throws IOException
      Throws:
      IOException
    • drainEncoder

      private boolean drainEncoder(org.apache.hc.core5.http.nio.DataStreamChannel channel) throws IOException
      Throws:
      IOException