garydgregory commented on code in PR #688: URL: https://github.com/apache/httpcomponents-client/pull/688#discussion_r2233030207
########## httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/GzipAsyncEntityConsumer.java: ########## @@ -0,0 +1,331 @@ +/* + * ==================================================================== + * 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.List; +import java.util.Queue; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.zip.CRC32; +import java.util.zip.DataFormatException; +import java.util.zip.Inflater; + +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.nio.AsyncEntityConsumer; +import org.apache.hc.core5.http.nio.CapacityChannel; +import org.apache.hc.core5.util.Args; + +/** + * Streaming, non-blocking consumer for responses encoded with + * {@code Content-Encoding: gzip}. + * + * <p>The implementation follows the public GZIP specification:</p> + * <ol> + * <li>Verifies the fixed 10-byte header (ID1 0x1F, ID2 0x8B, CM 8).</li> + * <li>Parses / skips optional sections signalled by the FLG bits<br> + * (FEXTRA, FNAME, FCOMMENT, FHCRC, FTEXT).</li> + * <li>Inflates the raw DEFLATE blocks (<em>nowrap=true</em>) while + * streaming the plain bytes to an inner consumer.</li> + * <li>Collects the 8-byte trailer (CRC-32 & ISIZE) and validates it + * on {@link #streamEnd(List)}.</li> + * </ol> + * + * @param <T> result type produced by the wrapped inner consumer + * @since 5.6 + */ +public final class GzipAsyncEntityConsumer<T> implements AsyncEntityConsumer<T> { + + private static final int FTEXT = 1; // not used, informative only + private static final int FHCRC = 2; + private static final int FEXTRA = 4; + private static final int FNAME = 8; + private static final int FCOMMENT = 16; + + private static final int OUT_BUF = 8 * 1024; // inflate buffer + + private final AsyncEntityConsumer<T> inner; + private final Inflater inflater = new Inflater(true); // raw DEFLATE blocks + private final CRC32 crc32 = new CRC32(); + + private final byte[] out = new byte[OUT_BUF]; + private final Queue<Byte> trailerBuf = new ArrayDeque<Byte>(8); + + private final Queue<Byte> hdrFixed = new ArrayDeque<Byte>(10); + private int flg = 0; + private int extraRemaining = -1; + private boolean wantHdrCrc = false; + private int hdrCrcCalc = 0; // incremental CRC-16 + + private boolean headerDone = false; + private long uncompressed = 0; + + /* ---------- completion plumbing ---------- */ Review Comment: I think we should use the Javadoc format instead of this ---- very noisy style. -- 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