HTTPCLIENT-1484: GzipCompressingEntity should not close the underlying output stream if the entity has not been fully written out due to an exception

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1578440 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-03-17 15:34:41 +00:00
parent 09027e7286
commit 68855c40f4
3 changed files with 23 additions and 5 deletions

View File

@ -4,6 +4,10 @@ Changes for 4.4-alpha1
Changelog: Changelog:
------------------- -------------------
* [HTTPCLIENT-1484] GzipCompressingEntity should not close the underlying output stream
if the entity has not been fully written out due to an exception.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-1474] Fixed broken entity enclosing requests in HC Fluent. * [HTTPCLIENT-1474] Fixed broken entity enclosing requests in HC Fluent.
Contributed by Oleg Kalnichevski <olegk at apache.org> Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -104,11 +104,10 @@ public class GzipCompressingEntity extends HttpEntityWrapper {
public void writeTo(final OutputStream outstream) throws IOException { public void writeTo(final OutputStream outstream) throws IOException {
Args.notNull(outstream, "Output stream"); Args.notNull(outstream, "Output stream");
final GZIPOutputStream gzip = new GZIPOutputStream(outstream); final GZIPOutputStream gzip = new GZIPOutputStream(outstream);
try {
wrappedEntity.writeTo(gzip); wrappedEntity.writeTo(gzip);
} finally { // Only close output stream if the wrapped entity has been
// successfully written out
gzip.close(); gzip.close();
} }
}
} }

View File

@ -30,6 +30,7 @@ package org.apache.http.client.entity;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.Consts; import org.apache.http.Consts;
@ -41,6 +42,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
public class TestGZip { public class TestGZip {
@ -93,4 +95,17 @@ public class TestGZip {
} }
} }
@Test
public void testCompressionIOExceptionLeavesOutputStreamOpen() throws Exception {
final HttpEntity in = Mockito.mock(HttpEntity.class);
Mockito.doThrow(new IOException("Ooopsie")).when(in).writeTo(Mockito.<OutputStream>any());
final GzipCompressingEntity gzipe = new GzipCompressingEntity(in);
final OutputStream out = Mockito.mock(OutputStream.class);
try {
gzipe.writeTo(out);
} catch (IOException ex) {
Mockito.verify(out, Mockito.never()).close();
}
}
} }