hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1423810 - in /httpcomponents/httpclient/branches/4.2.x/httpclient/src: main/java/org/apache/http/client/entity/ test/java/org/apache/http/client/entity/
Date Wed, 19 Dec 2012 09:56:39 GMT
Author: olegk
Date: Wed Dec 19 09:56:38 2012
New Revision: 1423810

URL: http://svn.apache.org/viewvc?rev=1423810&view=rev
Log:
HTTPCLIENT-1281: GzipDecompressingEntity#getContent() to close wrapped entity's stream in
case of an I/O error

Modified:
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java?rev=1423810&r1=1423809&r2=1423810&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DecompressingEntity.java
Wed Dec 19 09:56:38 2012
@@ -31,7 +31,6 @@ import java.io.OutputStream;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.entity.HttpEntityWrapper;
-import org.apache.http.util.EntityUtils;
 
 /**
  * Common base class for decompressing {@link HttpEntity} implementations.
@@ -46,8 +45,8 @@ abstract class DecompressingEntity exten
     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;
 
@@ -61,25 +60,30 @@ abstract class DecompressingEntity exten
         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();
         }
     }
 
@@ -94,9 +98,7 @@ abstract class DecompressingEntity exten
         InputStream instream = getContent();
         try {
             byte[] buffer = new byte[BUFFER_SIZE];
-
             int l;
-
             while ((l = instream.read(buffer)) != -1) {
                 outstream.write(buffer, 0, l);
             }

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java?rev=1423810&r1=1423809&r2=1423810&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/DeflateDecompressingEntity.java
Wed Dec 19 09:56:38 2012
@@ -70,7 +70,7 @@ public class DeflateDecompressingEntity 
      * @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.
          *

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java?rev=1423810&r1=1423809&r2=1423810&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/entity/GzipDecompressingEntity.java
Wed Dec 19 09:56:38 2012
@@ -52,7 +52,7 @@ public class GzipDecompressingEntity ext
     }
 
     @Override
-    InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException
{
+    InputStream decorate(final InputStream wrapped) throws IOException {
         return new GZIPInputStream(wrapped);
     }
 

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java?rev=1423810&r1=1423809&r2=1423810&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/client/entity/TestDecompressingEntity.java
Wed Dec 19 09:56:38 2012
@@ -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);
         }
 



Mime
View raw message