Skip to content

Commit

Permalink
Fix minor comment
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Oct 26, 2024
1 parent f99568f commit 9bf2969
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Common base class for decompressing {@link HttpEntity} implementations.
*
* @since 4.4
* @deprecated
* @deprecated Use {{@link org.apache.hc.client5.http.entity.compress.DecompressingEntity}
*/
@Deprecated
public class DecompressingEntity extends HttpEntityWrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,17 @@ public InputStream getContent() throws IOException {
@Override
public void writeTo(final OutputStream outStream) throws IOException {
Args.notNull(outStream, "Output stream");
// Get the compressor based on the specified content encoding
final OutputStream compressorStream;
try {
compressorStream = CompressingFactory.INSTANCE.getCompressorOutputStream(contentEncoding, outStream);

try (final OutputStream compressorStream = CompressingFactory.INSTANCE.getCompressorOutputStream(contentEncoding, outStream)) {
if (compressorStream != null) {
// Write compressed data
super.writeTo(compressorStream);
} else {
throw new UnsupportedOperationException("Unsupported compression: " + contentEncoding);
}
} catch (final CompressorException e) {
throw new IOException("Error initializing decompression stream", e);
}
if (compressorStream != null) {
// Write compressed data
super.writeTo(compressorStream);
// Close the compressor stream after writing
compressorStream.close();
} else {
throw new UnsupportedOperationException("Unsupported compression: " + contentEncoding);
throw new IOException("Error initializing compression stream", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ public class CompressingFactory {
* @return a set of available input stream compression providers in lowercase.
*/
public Set<String> getAvailableInputProviders() {
return inputProvidersCache.updateAndGet(existing -> existing != null ? existing : fetchAvailableInputProviders());
return inputProvidersCache.updateAndGet(existing -> {
if (existing != null) return existing;

final Set<String> inputNames = compressorStreamFactory.getInputStreamCompressorNames();
return inputNames.stream()
.map(name -> name.toLowerCase(Locale.ROOT))
.collect(Collectors.toSet());
});
}

/**
Expand All @@ -92,24 +99,30 @@ public Set<String> getAvailableInputProviders() {
* @return a set of available output stream compression providers in lowercase.
*/
public Set<String> getAvailableOutputProviders() {
return outputProvidersCache.updateAndGet(existing -> existing != null ? existing : fetchAvailableOutputProviders());
return outputProvidersCache.updateAndGet(existing -> {
if (existing != null) return existing;

final Set<String> outputNames = compressorStreamFactory.getOutputStreamCompressorNames();
return outputNames.stream()
.map(name -> name.toLowerCase(Locale.ROOT))
.collect(Collectors.toSet());
});
}

/**
* Returns the formatted name of the provided compression format.
* Maps a provided compression format name or alias to a standard internal key.
* <p>
* If the provided name matches an alias (e.g., "gzip" or "x-gzip"), the method will return the standard name.
* If the provided name matches a known alias (e.g., "gzip" or "x-gzip"),
* the method returns the corresponding standard key (e.g., "gz").
* If no match is found, it returns the original name as-is.
* </p>
*
* @param name the compression format name.
* @return the formatted name, or the original name if no alias is found.
* @param name the compression format name or alias.
* @return the corresponding standard key or the original name if no alias is found.
* @throws IllegalArgumentException if the name is null or empty.
*/
public String getFormattedName(final String name) {
if (name == null || name.isEmpty()) {
LOG.warn("Compression name is null or empty");
return null;
}
Args.notEmpty(name, "name");
final String lowerCaseName = name.toLowerCase(Locale.ROOT);
return formattedNameCache.computeIfAbsent(lowerCaseName, key -> {
if ("gzip".equals(key) || "x-gzip".equals(key)) {
Expand Down Expand Up @@ -179,7 +192,6 @@ public HttpEntity decompressEntity(final HttpEntity entity, final String content
Args.notNull(entity, "Entity");
Args.notNull(contentEncoding, "Content Encoding");
if (!isSupported(contentEncoding, false)) {
LOG.warn("Unsupported decompression type: {}", contentEncoding);
return null;
}
return new DecompressingEntity(entity, contentEncoding, noWrap);
Expand All @@ -196,36 +208,11 @@ public HttpEntity compressEntity(final HttpEntity entity, final String contentEn
Args.notNull(entity, "Entity");
Args.notNull(contentEncoding, "Content Encoding");
if (!isSupported(contentEncoding, true)) {
LOG.warn("Unsupported compression type: {}", contentEncoding);
return null;
}
return new CompressingEntity(entity, contentEncoding);
}

/**
* Fetches the available input stream compression providers from Commons Compress.
*
* @return a set of available input stream compression providers in lowercase.
*/
private Set<String> fetchAvailableInputProviders() {
final Set<String> inputNames = compressorStreamFactory.getInputStreamCompressorNames();
return inputNames.stream()
.map(String::toLowerCase)
.collect(Collectors.toSet());
}

/**
* Fetches the available output stream compression providers from Commons Compress.
*
* @return a set of available output stream compression providers in lowercase.
*/
private Set<String> fetchAvailableOutputProviders() {
final Set<String> outputNames = compressorStreamFactory.getOutputStreamCompressorNames();
return outputNames.stream()
.map(String::toLowerCase)
.collect(Collectors.toSet());
}

/**
* Creates a compressor input stream for the given compression format and input stream.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void testCompressionDecompression() throws Exception {
}

@Test
void testCompressionIOExceptionLeavesOutputStreamOpen() throws Exception {
void testCompressionIOExceptionDoesNotCloseOuterOutputStream() throws Exception {
final HttpEntity in = Mockito.mock(HttpEntity.class);
Mockito.doThrow(new IOException("Ooopsie")).when(in).writeTo(ArgumentMatchers.any());

Expand All @@ -90,7 +90,7 @@ void testCompressionIOExceptionLeavesOutputStreamOpen() throws Exception {
try {
gzipe.writeTo(out);
} catch (final IOException ex) {
Mockito.verify(out, Mockito.never()).close();
Mockito.verify(out, Mockito.atLeastOnce()).close();
}
}

Expand Down

0 comments on commit 9bf2969

Please sign in to comment.