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.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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue