Class DeflatingZstdEntityProducer
java.lang.Object
org.apache.hc.client5.http.async.methods.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 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:
getContentEncoding()returns"zstd".getContentLength()returns-1(unknown after compression).isChunked()returnstrue(requests are typically sent chunked).
Behavior
- Streaming & back-pressure: compressed output is staged in direct
ByteBuffers and written only when the channel accepts bytes. WhenDataStreamChannel.write(...)returns0, the producer pauses and requests another output turn. - Finalization: after the delegate signals
endStream(), this producer emits the zstd frame epilogue and then callsDataStreamChannel.endStream(). - Repeatability: repeatable only if the delegate is repeatable.
- Headers: callers are responsible for sending
Content-Encoding: zstdon 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:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final org.apache.hc.core5.http.nio.AsyncEntityProducerprivate booleanprivate static final intprivate final ByteBufferDirect staging for heap inputs.private final intprivate static final intprivate ByteBufferCurrent output buffer owned by zstd; replaced when it overflows or flushes.private final intprivate final Deque<ByteBuffer> Pending compressed output buffers, ready to write (pos=0..limit).private final AtomicBooleanprivate booleanprivate com.github.luben.zstd.ZstdDirectBufferCompressingStreamZstd compressor stream. -
Constructor Summary
ConstructorsConstructorDescriptionDeflatingZstdEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer delegate) DeflatingZstdEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer delegate, int level) -
Method Summary
Modifier and TypeMethodDescriptionintprivate intcompressFrom(ByteBuffer src) Compress the bytes insrc(may be heap or direct).private voidvoidprivate booleanflushPending(org.apache.hc.core5.http.nio.DataStreamChannel chan) Try to write as much of the pending compressed data as the channel accepts.longbooleanbooleanvoidproduce(org.apache.hc.core5.http.nio.DataStreamChannel chan) void
-
Field Details
-
IN_BUF
private static final int IN_BUF- See Also:
-
OUT_BUF_DEFAULT
private static final int OUT_BUF_DEFAULT- See Also:
-
delegate
private final org.apache.hc.core5.http.nio.AsyncEntityProducer delegate -
inDirect
Direct staging for heap inputs. -
pending
Pending compressed output buffers, ready to write (pos=0..limit). -
outBuf
Current output buffer owned by zstd; replaced when it overflows or flushes. -
zstream
private com.github.luben.zstd.ZstdDirectBufferCompressingStream zstreamZstd compressor stream. -
upstreamEnded
private volatile boolean upstreamEnded -
finished
private volatile boolean finished -
released
-
level
private final int level -
outCap
private final int outCap
-
-
Constructor Details
-
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 Details
-
isRepeatable
public boolean isRepeatable()- Specified by:
isRepeatablein interfaceorg.apache.hc.core5.http.nio.AsyncEntityProducer
-
getContentLength
public long getContentLength()- Specified by:
getContentLengthin interfaceorg.apache.hc.core5.http.EntityDetails
-
getContentType
- Specified by:
getContentTypein interfaceorg.apache.hc.core5.http.EntityDetails
-
getContentEncoding
- Specified by:
getContentEncodingin interfaceorg.apache.hc.core5.http.EntityDetails
-
isChunked
public boolean isChunked()- Specified by:
isChunkedin interfaceorg.apache.hc.core5.http.EntityDetails
-
getTrailerNames
-
available
public int available()- Specified by:
availablein interfaceorg.apache.hc.core5.http.nio.AsyncDataProducer
-
produce
- Specified by:
producein interfaceorg.apache.hc.core5.http.nio.AsyncDataProducer- Throws:
IOException
-
ensureStreamInitialized
- Throws:
IOException
-
flushPending
private boolean flushPending(org.apache.hc.core5.http.nio.DataStreamChannel chan) throws IOException Try to write as much of the pending compressed data as the channel accepts.- Throws:
IOException
-
compressFrom
Compress the bytes insrc(may be heap or direct).- Throws:
IOException
-
failed
- Specified by:
failedin interfaceorg.apache.hc.core5.http.nio.AsyncEntityProducer
-
releaseResources
public void releaseResources()- Specified by:
releaseResourcesin interfaceorg.apache.hc.core5.http.nio.ResourceHolder
-