Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

package org.apache.hc.client5.http.entity.compress;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
import org.apache.hc.core5.io.IOFunction;
Expand All @@ -48,19 +50,23 @@ public DecompressingEntity(
this.decoder = Args.notNull(decoder, "Stream decoder");
}

private InputStream getDecompressingStream() throws IOException {
return new LazyDecompressingInputStream(super.getContent(), decoder);
}

/**
* Returns the cached <em>decoded</em> stream, creating it once if necessary.
*/
@Override
public InputStream getContent() throws IOException {
if (!isStreaming()) {
return decoder.apply(super.getContent());
return getDecompressingStream();
}

InputStream local = cached;
if (local == null) {
if (cached == null) {
cached = decoder.apply(super.getContent());
cached = getDecompressingStream();
}
local = cached;
}
Expand Down Expand Up @@ -97,4 +103,61 @@ public void writeTo(final OutputStream out) throws IOException {
}
}
}
}

private static final class LazyDecompressingInputStream extends FilterInputStream {

private final IOFunction<InputStream, InputStream> decoder;
private InputStream decompressedStream;

private LazyDecompressingInputStream(
final InputStream inputStream,
final IOFunction<InputStream, InputStream> decoder) {
super(inputStream);
this.decoder = decoder;
}

private InputStream getDecompressedStream() throws IOException {
if (decompressedStream == null) {
decompressedStream = decoder.apply(in);
}
return decompressedStream;
}

@Override
public int read() throws IOException {
return getDecompressedStream().read();
}

@Override
public int read(final byte[] b) throws IOException {
return getDecompressedStream().read(b);
}

@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
return getDecompressedStream().read(b, off, len);
}

@Override
public long skip(final long n) throws IOException {
return getDecompressedStream().skip(n);
}

@Override
public int available() throws IOException {
return getDecompressedStream().available();
}

@Override
public void close() throws IOException {
final InputStream local = decompressedStream;
if (local != null) {
local.close();
} else {
super.close();
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
import java.util.zip.GZIPInputStream;

import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
Expand Down Expand Up @@ -108,6 +110,28 @@ void testWriteToStream() throws Exception {
}
}

@Test
void testMalformedGzipContentCanBeClosedWithoutReading() throws Exception {
final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.APPLICATION_OCTET_STREAM);
final HttpEntity entity = new org.apache.hc.client5.http.entity.compress.DecompressingEntity(
wrapped, GZIPInputStream::new);

Assertions.assertDoesNotThrow(() -> entity.getContent().close());
}

@Test
void testMalformedGzipContentFailsOnRead() throws Exception {
final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.APPLICATION_OCTET_STREAM);
final HttpEntity entity = new org.apache.hc.client5.http.entity.compress.DecompressingEntity(
wrapped, GZIPInputStream::new);

try (final InputStream content = entity.getContent()) {
Assertions.assertThrows(IOException.class, content::read);
}
}

static class ChecksumEntity extends org.apache.hc.client5.http.entity.compress.DecompressingEntity {

public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
Expand Down
Loading