Skip to content

Commit

Permalink
update/improve javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Oct 26, 2024
1 parent 9bf2969 commit 197c21a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* {@link InputStreamFactory} for handling GZIPContent Coded responses.
*
* @since 5.0
* @deprecated Use {@link CompressingFactory#getCompressorInputStream(String, InputStream, boolean)} instead.
* @deprecated Use {@link CompressingFactory#getDecompressorInputStream(String, InputStream, boolean)} instead.
*/
@Deprecated
@Contract(threading = ThreadingBehavior.STATELESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import org.apache.commons.compress.compressors.deflate.DeflateParameters;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.util.Args;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A factory class for managing compression and decompression of HTTP entities using different compression formats.
Expand All @@ -66,7 +64,6 @@
*/
public class CompressingFactory {

private static final Logger LOG = LoggerFactory.getLogger(CompressingFactory.class);
/**
* Singleton instance of the factory.
*/
Expand All @@ -84,8 +81,9 @@ public class CompressingFactory {
*/
public Set<String> getAvailableInputProviders() {
return inputProvidersCache.updateAndGet(existing -> {
if (existing != null) return existing;

if (existing != null) {
return existing;
}
final Set<String> inputNames = compressorStreamFactory.getInputStreamCompressorNames();
return inputNames.stream()
.map(name -> name.toLowerCase(Locale.ROOT))
Expand All @@ -100,8 +98,9 @@ public Set<String> getAvailableInputProviders() {
*/
public Set<String> getAvailableOutputProviders() {
return outputProvidersCache.updateAndGet(existing -> {
if (existing != null) return existing;

if (existing != null) {
return existing;
}
final Set<String> outputNames = compressorStreamFactory.getOutputStreamCompressorNames();
return outputNames.stream()
.map(name -> name.toLowerCase(Locale.ROOT))
Expand Down Expand Up @@ -135,32 +134,36 @@ public String getFormattedName(final String name) {
}

/**
* Creates an input stream for the specified compression format and decompresses the provided input stream.
* Creates a decompressor input stream for the specified format type and decompresses the provided input stream.
* <p>
* This method uses the specified compression name to decompress the input stream and supports the "noWrap" option
* for deflate streams.
* If the format type is supported, this method returns a new input stream that decompresses data from the original input stream.
* For "deflate" format, the "noWrap" option controls the inclusion of zlib headers:
* - If {@code noWrap} is {@code true}, zlib headers and trailers are omitted, resulting in raw deflate data.
* - If {@code noWrap} is {@code false}, the deflate stream includes standard zlib headers.
* </p>
*
* @param name the compression format.
* @param inputStream the input stream to decompress.
* @param noWrap if true, disables the zlib header and trailer for deflate streams.
* @return the decompressed input stream, or the original input stream if the format is not supported.
* @param name the format type to use for decompression.
* @param inputStream the input stream to be decompressed.
* @param noWrap whether to exclude zlib headers and trailers for deflate streams (applicable to "deflate" only).
* @return a decompressed input stream if the format type is supported; otherwise, the original input stream.
* @throws CompressorException if an error occurs while creating the decompressor input stream.
*/
public InputStream getCompressorInputStream(final String name, final InputStream inputStream, final boolean noWrap) throws CompressorException {
public InputStream getDecompressorInputStream(final String name, final InputStream inputStream, final boolean noWrap) throws CompressorException {
Args.notNull(inputStream, "InputStream");
Args.notNull(name, "name");
final String formattedName = getFormattedName(name);
return isSupported(formattedName, false)
? createCompressorInputStream(formattedName, inputStream, noWrap)
? createDecompressorInputStream(formattedName, inputStream, noWrap)
: inputStream;
}

/**
* Creates an output stream for the specified compression format and compresses the provided output stream.
* Creates an output stream to compress the provided output stream based on the specified format type.
*
* @param name the compression format.
* @param name the format type.
* @param outputStream the output stream to compress.
* @return the compressed output stream, or the original output stream if the format is not supported.
* @throws CompressorException if an error occurs while creating the compressor output stream.
*/
public OutputStream getCompressorOutputStream(final String name, final OutputStream outputStream) throws CompressorException {
final String formattedName = getFormattedName(name);
Expand All @@ -170,11 +173,11 @@ public OutputStream getCompressorOutputStream(final String name, final OutputStr
}

/**
* Decompresses the provided HTTP entity using the specified compression format.
* Decompresses the provided HTTP entity based on the specified format type.
*
* @param entity the HTTP entity to decompress.
* @param contentEncoding the compression format.
* @return a decompressed {@link HttpEntity}, or {@code null} if the compression format is unsupported.
* @param contentEncoding the format type.
* @return a decompressed {@link HttpEntity}, or {@code null} if the format type is unsupported.
*/
public HttpEntity decompressEntity(final HttpEntity entity, final String contentEncoding) {
return decompressEntity(entity, contentEncoding, false);
Expand All @@ -198,11 +201,11 @@ public HttpEntity decompressEntity(final HttpEntity entity, final String content
}

/**
* Compresses the provided HTTP entity using the specified compression format.
* Compresses the provided HTTP entity based on the specified format type.
*
* @param entity the HTTP entity to compress.
* @param contentEncoding the compression format.
* @return a compressed {@link HttpEntity}, or {@code null} if the compression format is unsupported.
* @param contentEncoding the format type.
* @return a compressed {@link HttpEntity}, or {@code null} if the format type is unsupported.
*/
public HttpEntity compressEntity(final HttpEntity entity, final String contentEncoding) {
Args.notNull(entity, "Entity");
Expand All @@ -214,21 +217,21 @@ public HttpEntity compressEntity(final HttpEntity entity, final String contentEn
}

/**
* Creates a compressor input stream for the given compression format and input stream.
* Creates a decompressor input stream for the specified format type.
* <p>
* This method handles the special case for deflate compression where the zlib header can be optionally included.
* The noWrap parameter directly controls the behavior of the zlib header:
* - If noWrap is {@code true}, the deflate stream is processed without zlib headers (raw Deflate).
* - If noWrap is {@code false}, the deflate stream includes the zlib header.
* If the format type is supported, this method returns an input stream that decompresses the original input data.
* For "deflate" format, the `noWrap` parameter determines whether the stream should include standard zlib headers:
* - If {@code noWrap} is {@code true}, the stream is created without zlib headers (raw deflate).
* - If {@code noWrap} is {@code false}, zlib headers are included.
* </p>
*
* @param name the compression format (e.g., "gzip", "deflate").
* @param inputStream the input stream to decompress; must not be {@code null}.
* @param noWrap if {@code true}, disables the zlib header and trailer for deflate streams (raw Deflate).
* @return a decompressed input stream, or {@code null} if an error occurs during stream creation.
* @throws CompressorException if an error occurs while creating the compressor input stream or if the compression format is unsupported.
* @param name the format type (e.g., "gzip", "deflate") for decompression.
* @param inputStream the input stream containing compressed data; must not be {@code null}.
* @param noWrap only applicable to "deflate" format. If {@code true}, omits zlib headers for a raw deflate stream.
* @return a decompressed input stream for the specified format, or throws an exception if the format is unsupported.
* @throws CompressorException if an error occurs while creating the decompressor stream or if the format type is unsupported.
*/
private InputStream createCompressorInputStream(final String name, final InputStream inputStream, final boolean noWrap) throws CompressorException {
private InputStream createDecompressorInputStream(final String name, final InputStream inputStream, final boolean noWrap) throws CompressorException {
if ("deflate".equalsIgnoreCase(name)) {
final DeflateParameters parameters = new DeflateParameters();
parameters.setWithZlibHeader(noWrap);
Expand All @@ -238,9 +241,9 @@ private InputStream createCompressorInputStream(final String name, final InputSt
}

/**
* Creates a compressor output stream for the given compression format and output stream.
* Creates a compressor output stream for the given format type and output stream.
*
* @param name the compression format.
* @param name the format type.
* @param outputStream the output stream to compress.
* @return a compressed output stream, or null if an error occurs.
* @throws CompressorException if an error occurs while creating the compressor output stream.
Expand All @@ -250,11 +253,11 @@ private OutputStream createCompressorOutputStream(final String name, final Outpu
}

/**
* Determines if the specified compression format is supported for either input or output streams.
* Determines if the specified format type is supported for either input (decompression) or output (compression) streams.
*
* @param name the compression format.
* @param isOutput if true, checks if the format is supported for output; otherwise, checks for input support.
* @return true if the format is supported, false otherwise.
* @param name the format type.
* @param isOutput if true, checks if the format type is supported for output; otherwise, checks for input support.
* @return true if the format type is supported, false otherwise.
*/
private boolean isSupported(final String name, final boolean isOutput) {
final Set<String> availableProviders = isOutput ? getAvailableOutputProviders() : getAvailableInputProviders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public LazyDecompressingInputStream(final InputStream wrappedStream, final Strin
private InputStream initWrapper() throws IOException {
if (wrapperStream == null) {
try {
wrapperStream = CompressingFactory.INSTANCE.getCompressorInputStream(compressionType, in, noWrap);
wrapperStream = CompressingFactory.INSTANCE.getDecompressorInputStream(compressionType, in, noWrap);
} catch (final CompressorException e) {
throw new IOException("Error initializing decompression stream", e);
}
Expand Down

0 comments on commit 197c21a

Please sign in to comment.