diff --git a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java index 00d70f11a..32c5e5177 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java @@ -32,7 +32,6 @@ import java.io.OutputStream; import org.apache.http.HttpEntity; import org.apache.http.entity.HttpEntityWrapper; import org.apache.http.util.Args; -import org.apache.http.util.EntityUtils; /** * Common base class for decompressing {@link HttpEntity} implementations. @@ -47,8 +46,8 @@ abstract class DecompressingEntity extends HttpEntityWrapper { private static final int BUFFER_SIZE = 1024 * 2; /** - * DecompressingEntities are not repeatable, so they will return the same - * InputStream instance when {@link #getContent()} is called. + * {@link #getContent()} method must return the same {@link InputStream} + * instance when DecompressingEntity is wrapping a streaming entity. */ private InputStream content; @@ -62,25 +61,30 @@ abstract class DecompressingEntity extends HttpEntityWrapper { super(wrapped); } - abstract InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException; + abstract InputStream decorate(final InputStream wrapped) throws IOException; + + private InputStream getDecompressingStream() throws IOException { + InputStream in = wrappedEntity.getContent(); + try { + return decorate(in); + } catch (IOException ex) { + in.close(); + throw ex; + } + } /** * {@inheritDoc} */ @Override public InputStream getContent() throws IOException { - try { - if (wrappedEntity.isStreaming()) { - if (content == null) { - content = getDecompressingInputStream(wrappedEntity.getContent()); - } - return content; - } else { - return getDecompressingInputStream(wrappedEntity.getContent()); + if (wrappedEntity.isStreaming()) { + if (content == null) { + content = getDecompressingStream(); } - } catch (IOException e) { - EntityUtils.consume(wrappedEntity); - throw e; + return content; + } else { + return getDecompressingStream(); } } @@ -93,9 +97,7 @@ abstract class DecompressingEntity extends HttpEntityWrapper { InputStream instream = getContent(); try { byte[] buffer = new byte[BUFFER_SIZE]; - int l; - while ((l = instream.read(buffer)) != -1) { outstream.write(buffer, 0, l); } diff --git a/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java index bb9cbf8b0..e74dc4ecd 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java @@ -70,7 +70,7 @@ public class DeflateDecompressingEntity extends DecompressingEntity { * @throws IOException if there was a problem */ @Override - InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { + InputStream decorate(final InputStream wrapped) throws IOException { /* * A zlib stream will have a header. * diff --git a/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java index 9e22eb451..b858f8b00 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java @@ -52,7 +52,7 @@ public class GzipDecompressingEntity extends DecompressingEntity { } @Override - InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { + InputStream decorate(final InputStream wrapped) throws IOException { return new GZIPInputStream(wrapped); } diff --git a/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java b/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java index e3bf408d3..90d08216b 100644 --- a/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java +++ b/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java @@ -100,7 +100,7 @@ public class TestDecompressingEntity { } @Override - InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { + InputStream decorate(final InputStream wrapped) throws IOException { return new CheckedInputStream(wrapped, this.checksum); }