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:
parent
877970d7d8
commit
01a7e66d7b
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue