HTTPCLIENT-1281: GzipDecompressingEntity#getContent() to close wrapped entity's stream in case of an I/O error

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1423807 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-12-19 09:46:31 +00:00
parent 877970d7d8
commit 01a7e66d7b
4 changed files with 22 additions and 20 deletions

View File

@ -32,7 +32,6 @@ import java.io.OutputStream;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper; import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.util.Args; import org.apache.http.util.Args;
import org.apache.http.util.EntityUtils;
/** /**
* Common base class for decompressing {@link HttpEntity} implementations. * 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; private static final int BUFFER_SIZE = 1024 * 2;
/** /**
* DecompressingEntities are not repeatable, so they will return the same * {@link #getContent()} method must return the same {@link InputStream}
* InputStream instance when {@link #getContent()} is called. * instance when DecompressingEntity is wrapping a streaming entity.
*/ */
private InputStream content; private InputStream content;
@ -62,25 +61,30 @@ abstract class DecompressingEntity extends HttpEntityWrapper {
super(wrapped); 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} * {@inheritDoc}
*/ */
@Override @Override
public InputStream getContent() throws IOException { public InputStream getContent() throws IOException {
try { if (wrappedEntity.isStreaming()) {
if (wrappedEntity.isStreaming()) { if (content == null) {
if (content == null) { content = getDecompressingStream();
content = getDecompressingInputStream(wrappedEntity.getContent());
}
return content;
} else {
return getDecompressingInputStream(wrappedEntity.getContent());
} }
} catch (IOException e) { return content;
EntityUtils.consume(wrappedEntity); } else {
throw e; return getDecompressingStream();
} }
} }
@ -93,9 +97,7 @@ abstract class DecompressingEntity extends HttpEntityWrapper {
InputStream instream = getContent(); InputStream instream = getContent();
try { try {
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
int l; int l;
while ((l = instream.read(buffer)) != -1) { while ((l = instream.read(buffer)) != -1) {
outstream.write(buffer, 0, l); outstream.write(buffer, 0, l);
} }

View File

@ -70,7 +70,7 @@ public class DeflateDecompressingEntity extends DecompressingEntity {
* @throws IOException if there was a problem * @throws IOException if there was a problem
*/ */
@Override @Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { InputStream decorate(final InputStream wrapped) throws IOException {
/* /*
* A zlib stream will have a header. * A zlib stream will have a header.
* *

View File

@ -52,7 +52,7 @@ public class GzipDecompressingEntity extends DecompressingEntity {
} }
@Override @Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { InputStream decorate(final InputStream wrapped) throws IOException {
return new GZIPInputStream(wrapped); return new GZIPInputStream(wrapped);
} }

View File

@ -100,7 +100,7 @@ public class TestDecompressingEntity {
} }
@Override @Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException { InputStream decorate(final InputStream wrapped) throws IOException {
return new CheckedInputStream(wrapped, this.checksum); return new CheckedInputStream(wrapped, this.checksum);
} }