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.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());
content = getDecompressingStream();
}
return content;
} else {
return getDecompressingInputStream(wrappedEntity.getContent());
}
} catch (IOException e) {
EntityUtils.consume(wrappedEntity);
throw e;
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);
}

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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);
}