Index: src/main/java/org/apache/http/client/entity/DecompressingEntity.java =================================================================== --- src/main/java/org/apache/http/client/entity/DecompressingEntity.java (revision 1422900) +++ src/main/java/org/apache/http/client/entity/DecompressingEntity.java (working copy) @@ -62,20 +62,29 @@ abstract InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException; - /** - * {@inheritDoc} - */ - @Override - public InputStream getContent() throws IOException { - if (wrappedEntity.isStreaming()) { - if (content == null) { - content = getDecompressingInputStream(wrappedEntity.getContent()); - } - return content; - } else { - return getDecompressingInputStream(wrappedEntity.getContent()); - } - } + /** + * {@inheritDoc} + */ + @Override + public InputStream getContent() throws IOException { + InputStream wrappedInputStream = null; + try { + if (wrappedEntity.isStreaming()) { + if (content == null) { + wrappedInputStream = wrappedEntity.getContent(); + content = getDecompressingInputStream(wrappedInputStream); + } + return content; + } else { + wrappedInputStream = wrappedEntity.getContent(); + return getDecompressingInputStream(wrappedInputStream); + } + } catch (IOException e) { + if (wrappedInputStream != null) + wrappedInputStream.close(); + throw e; + } + } /** * {@inheritDoc} Index: src/test/java/org/apache/http/client/entity/TestGZip.java =================================================================== --- src/test/java/org/apache/http/client/entity/TestGZip.java (revision 1422900) +++ src/test/java/org/apache/http/client/entity/TestGZip.java (working copy) @@ -28,12 +28,17 @@ package org.apache.http.client.entity; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.concurrent.atomic.AtomicBoolean; import junit.framework.Assert; import org.apache.http.Consts; +import org.apache.http.HttpEntity; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; +import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import org.junit.Test; @@ -62,5 +67,31 @@ GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(out); Assert.assertEquals("some kind of text", EntityUtils.toString(gunzipe, Consts.ASCII)); } + + @Test + public void testGzipDecompressingEntityDoesNotCrashInConstructorAndLeaveInputStreamOpen() + throws Exception { + final AtomicBoolean inputStreamIsClosed = new AtomicBoolean(false); + HttpEntity in = new InputStreamEntity(new InputStream() { + @Override + public int read() throws IOException { + throw new IOException("An exception occurred"); + } + @Override + public void close() throws IOException { + inputStreamIsClosed.set(true); + } + + }, 123); + GzipDecompressingEntity gunzipe = new GzipDecompressingEntity(in); + try { + InputStream is = gunzipe.getContent(); + } catch (IOException e) { + // As I cannot get the content, GzipDecompressingEntity is supposed + // to have released everything + Assert.assertTrue(inputStreamIsClosed.get()); + } + } + }