ok2c commented on code in PR #660: URL: https://github.com/apache/httpcomponents-client/pull/660#discussion_r2173669502
########## httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/Decoder.java: ########## @@ -0,0 +1,60 @@ +/* + * ==================================================================== + * 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.entity.compress; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Pull-side transformer that takes a compressed {@link InputStream} and + * returns a lazily-decoded view of the same byte sequence. + * + * <p>Implementations <strong>must</strong> return a stream that honours the + * usual {@code InputStream} contract and propagates {@link IOException}s + * raised by the underlying transport.</p> + * + * @since 5.6 + */ +@FunctionalInterface +public interface Decoder { Review Comment: @arturobernalg to me it is more important for the API to be logically consistent than saving a few lines of header handling code that will likely get optimized out by the compiler. In my opinion The `Encoder` / `Decoder` interfaces should work either with `InputStream` / `OutputStream` or with `HttpEntity`, but not with a mix. Feel free to leave it as is and merge the change-set but I will likely try to normalize the APIs later. ########## httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/ContentCodecRegistry.java: ########## @@ -0,0 +1,171 @@ +/* + * ==================================================================== + * 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.entity.compress; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumMap; +import java.util.Map; +import java.util.zip.GZIPInputStream; + +import org.apache.hc.client5.http.entity.DeflateInputStream; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.http.HttpEntity; +import org.brotli.dec.BrotliInputStream; + + +/** + * Run-time catalogue of built-in and Commons-Compress + * {@linkplain Encoder encoders} / {@linkplain Decoder decoders}. + * + * <p>Entries are wired once at class-load time and published through an + * unmodifiable map, so lookups are lock-free and thread-safe.</p> + * + * @since 5.6 + */ +@Internal +@Contract(threading = ThreadingBehavior.STATELESS) +public final class ContentCodecRegistry { + + private static final Map<ContentCoding, Codec> REGISTRY = build(); + + private static Map<ContentCoding, Codec> build() { + final Map<ContentCoding, Codec> m = + new EnumMap<>(ContentCoding.class); + + /* 1. Built-ins ------------------------------------------------- */ + m.put(ContentCoding.GZIP, + new Codec( + // encoder + org.apache.hc.client5.http.entity.GzipCompressingEntity::new, + // decoder + GZIPInputStream::new)); + m.put(ContentCoding.DEFLATE, + new Codec( + org.apache.hc.client5.http.entity.DeflateCompressingEntity::new, + DeflateInputStream::new)); + + /* 2. Commons-Compress extras ---------------------------------- */ + if (CommonsCompressSupport.isPresent()) { + for (final ContentCoding c : Arrays.asList( + ContentCoding.BROTLI, + ContentCoding.ZSTD, + ContentCoding.XZ, + ContentCoding.LZMA, + ContentCoding.LZ4_FRAMED, + ContentCoding.LZ4_BLOCK, + ContentCoding.BZIP2, + ContentCoding.PACK200, + ContentCoding.DEFLATE64)) { + + if (CommonsCompressDecoderFactory.runtimeAvailable(c.token())) { + m.put(c, new Codec(e -> new CommonsCompressingEntity(e, c.token()), new CommonsCompressDecoderFactory(c.token()) + )); + } + } + } + + /* 3. Native Brotli fallback (decode-only) ---------------------- */ + if (!m.containsKey(ContentCoding.BROTLI) + && CommonsCompressDecoderFactory.runtimeAvailable(ContentCoding.BROTLI.token())) { + m.put(ContentCoding.BROTLI, + Codec.decodeOnly(BrotliInputStream::new)); + } + + return Collections.unmodifiableMap(m); + } + + + /** + * Returns the {@link Codec} (or {@code null}). + */ + public static Codec codec(final ContentCoding coding) { Review Comment: @arturobernalg Here we have a scope violation: a public method exposing a package private class. Either make the method package private or make the class public. It is internal anyway. -- 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