Class DeflatingZstdEntityProducer

  • 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 DeflatingZstdEntityProducer
    extends java.lang.Object
    implements org.apache.hc.core5.http.nio.AsyncEntityProducer
    AsyncEntityProducer that compresses the bytes produced by a delegate entity into a single Zstandard (zstd) frame on the fly.

    This producer wraps a AsyncEntityProducer and performs streaming, ByteBuffer-to-ByteBuffer compression as the delegate writes to the provided DataStreamChannel. No InputStream is used in the client pipeline.

    Metadata reported by this producer:

    Behavior

    • Streaming & back-pressure: compressed output is staged in direct ByteBuffers and written only when the channel accepts bytes. When DataStreamChannel.write(...) returns 0, the producer pauses and requests another output turn.
    • Finalization: after the delegate signals endStream(), this producer emits the zstd frame epilogue and then calls DataStreamChannel.endStream().
    • Repeatability: repeatable only if the delegate is repeatable.
    • Headers: callers are responsible for sending Content-Encoding: zstd on the request if required by the server. Content length is not known in advance.
    • Resources: invoke releaseResources() to free native compressor resources.

    Constructors

    • DeflatingZstdEntityProducer(delegate) – uses a default compression level.
    • DeflatingZstdEntityProducer(delegate, level) – explicitly sets the zstd level.

    Thread-safety

    Not thread-safe; one instance per message exchange.

    Runtime dependency

    Requires com.github.luben:zstd-jni on the classpath.

    Since:
    5.6
    See Also:
    InflatingZstdDataConsumer, BasicRequestProducer, StringAsyncEntityProducer, ContentCompressionAsyncExec
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.apache.hc.core5.http.nio.AsyncEntityProducer delegate  
      private boolean finished  
      private static int IN_BUF  
      private java.nio.ByteBuffer inDirect
      Direct staging for heap inputs.
      private int level  
      private static int OUT_BUF_DEFAULT  
      private java.nio.ByteBuffer outBuf
      Current output buffer owned by zstd; replaced when it overflows or flushes.
      private int outCap  
      private java.util.Deque<java.nio.ByteBuffer> pending
      Pending compressed output buffers, ready to write (pos=0..limit).
      private java.util.concurrent.atomic.AtomicBoolean released  
      private boolean upstreamEnded  
      private com.github.luben.zstd.ZstdDirectBufferCompressingStream zstream
      Zstd compressor stream.
    • Field Detail

      • delegate

        private final org.apache.hc.core5.http.nio.AsyncEntityProducer delegate
      • inDirect

        private final java.nio.ByteBuffer inDirect
        Direct staging for heap inputs.
      • pending

        private final java.util.Deque<java.nio.ByteBuffer> pending
        Pending compressed output buffers, ready to write (pos=0..limit).
      • outBuf

        private java.nio.ByteBuffer outBuf
        Current output buffer owned by zstd; replaced when it overflows or flushes.
      • zstream

        private com.github.luben.zstd.ZstdDirectBufferCompressingStream zstream
        Zstd compressor stream.
      • upstreamEnded

        private volatile boolean upstreamEnded
      • finished

        private volatile boolean finished
      • released

        private final java.util.concurrent.atomic.AtomicBoolean released
      • level

        private final int level
      • outCap

        private final int outCap
    • Constructor Detail

      • DeflatingZstdEntityProducer

        public DeflatingZstdEntityProducer​(org.apache.hc.core5.http.nio.AsyncEntityProducer delegate)
      • DeflatingZstdEntityProducer

        public DeflatingZstdEntityProducer​(org.apache.hc.core5.http.nio.AsyncEntityProducer delegate,
                                           int level)
    • Method Detail

      • isRepeatable

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

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

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

        public java.lang.String getContentEncoding()
        Specified by:
        getContentEncoding 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 java.util.Set<java.lang.String> getTrailerNames()
        Specified by:
        getTrailerNames in interface org.apache.hc.core5.http.EntityDetails
      • 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 chan)
                     throws java.io.IOException
        Specified by:
        produce in interface org.apache.hc.core5.http.nio.AsyncDataProducer
        Throws:
        java.io.IOException
      • ensureStreamInitialized

        private void ensureStreamInitialized()
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • flushPending

        private boolean flushPending​(org.apache.hc.core5.http.nio.DataStreamChannel chan)
                              throws java.io.IOException
        Try to write as much of the pending compressed data as the channel accepts.
        Throws:
        java.io.IOException
      • compressFrom

        private int compressFrom​(java.nio.ByteBuffer src)
                          throws java.io.IOException
        Compress the bytes in src (may be heap or direct).
        Throws:
        java.io.IOException
      • failed

        public void failed​(java.lang.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