garydgregory commented on code in PR #703:
URL: 
https://github.com/apache/httpcomponents-client/pull/703#discussion_r2265286350


##########
httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/DeflatingZstdEntityProducer.java:
##########
@@ -0,0 +1,345 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.client5.http.async.methods;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.github.luben.zstd.ZstdDirectBufferCompressingStream;
+
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.nio.AsyncEntityProducer;
+import org.apache.hc.core5.http.nio.DataStreamChannel;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * {@code AsyncEntityProducer} that compresses the bytes produced by a 
delegate entity
+ * into a single <a 
href="https://www.rfc-editor.org/rfc/rfc8878";>Zstandard</a> (zstd) frame
+ * on the fly.
+ *
+ * <p>This producer wraps a {@link 
org.apache.hc.core5.http.nio.AsyncEntityProducer} and
+ * performs streaming, ByteBuffer-to-ByteBuffer compression as the delegate 
writes to the
+ * provided {@link org.apache.hc.core5.http.nio.DataStreamChannel}. No {@code 
InputStream}
+ * is used in the client pipeline.</p>
+ *
+ * <p>Metadata reported by this producer:</p>
+ * <ul>
+ *   <li>{@link #getContentEncoding()} returns {@code "zstd"}.</li>
+ *   <li>{@link #getContentLength()} returns {@code -1} (unknown after 
compression).</li>
+ *   <li>{@link #isChunked()} returns {@code true} (requests are typically 
sent chunked).</li>
+ * </ul>
+ *
+ * <h3>Usage</h3>
+ * <pre>{@code
+ * AsyncEntityProducer plain = new StringAsyncEntityProducer("payload", 
ContentType.TEXT_PLAIN);
+ * AsyncEntityProducer zstd  = new DeflatingZstdEntityProducer(plain);
+ *
+ * SimpleHttpRequest req = SimpleRequestBuilder.post("http://localhost/echo";)
+ *     .setHeader(HttpHeaders.CONTENT_ENCODING, "zstd") // inform the server
+ *     .build();
+ *
+ * client.execute(
+ *     new BasicRequestProducer(req, zstd),
+ *     new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
+ *     null);
+ * }</pre>
+ *
+ * <h3>Behavior</h3>
+ * <ul>
+ *   <li><b>Streaming & back-pressure:</b> compressed output is staged in 
direct
+ *       {@link java.nio.ByteBuffer}s and written only when the channel 
accepts bytes.
+ *       When {@code DataStreamChannel.write(...)} returns {@code 0}, the 
producer pauses and
+ *       requests another output turn.</li>
+ *   <li><b>Finalization:</b> after the delegate signals {@code endStream()}, 
this producer emits
+ *       the zstd frame epilogue and then calls {@code 
DataStreamChannel.endStream()}.</li>
+ *   <li><b>Repeatability:</b> repeatable only if the delegate is 
repeatable.</li>
+ *   <li><b>Headers:</b> callers are responsible for sending {@code 
Content-Encoding: zstd} on
+ *       the request if required by the server. Content length is not known in 
advance.</li>
+ *   <li><b>Resources:</b> invoke {@link #releaseResources()} to free native 
compressor resources.</li>
+ * </ul>
+ *
+ * <h3>Constructors</h3>
+ * <ul>
+ *   <li>{@code DeflatingZstdEntityProducer(delegate)} – uses a default 
compression level.</li>
+ *   <li>{@code DeflatingZstdEntityProducer(delegate, level)} – explicitly 
sets the zstd level.</li>
+ * </ul>
+ *
+ * <h3>Thread-safety</h3>
+ * <p>Not thread-safe; one instance per message exchange.</p>
+ *
+ * <h3>Runtime dependency</h3>
+ * <p>Requires {@code com.github.luben:zstd-jni} on the classpath.</p>
+ *
+ * @see org.apache.hc.client5.http.async.methods.InflatingZstdDataConsumer
+ * @see org.apache.hc.core5.http.nio.support.BasicRequestProducer
+ * @see org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer
+ * @see org.apache.hc.client5.http.impl.async.ContentCompressionAsyncExec
+ * @since 5.6
+ */
+public final class DeflatingZstdEntityProducer implements AsyncEntityProducer {
+
+    private static final int IN_BUF = 64 * 1024;
+    private static final int OUT_BUF_DEFAULT = 128 * 1024;
+
+    private final AsyncEntityProducer delegate;
+
+    /**
+     * Direct staging for heap inputs.
+     */
+    private final ByteBuffer inDirect = ByteBuffer.allocateDirect(IN_BUF);
+
+    /**
+     * Pending compressed output buffers, ready to write (pos=0..limit).
+     */
+    private final Deque<ByteBuffer> pending = new ArrayDeque<>();
+
+    /**
+     * Current output buffer owned by zstd; replaced when it overflows or 
flushes.
+     */
+    private ByteBuffer outBuf;
+
+    /**
+     * Zstd compressor stream.
+     */
+    private final ZstdDirectBufferCompressingStream zstream;
+
+    private volatile boolean upstreamEnded = false;

Review Comment:
   Initializing to default values creates clutter IMO.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to