You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/04/04 15:22:49 UTC

svn commit: r1088614 - /httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java

Author: olegk
Date: Mon Apr  4 13:22:48 2011
New Revision: 1088614

URL: http://svn.apache.org/viewvc?rev=1088614&view=rev
Log:
EntityUtils#toString() and EntityUtils#toByteArray fail to close content stream in case of content length check failure

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java?rev=1088614&r1=1088613&r2=1088614&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/util/EntityUtils.java Mon Apr  4 13:22:48 2011
@@ -86,24 +86,24 @@ public final class EntityUtils {
         if (instream == null) {
             return null;
         }
-        if (entity.getContentLength() > Integer.MAX_VALUE) {
-            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
-        }
-        int i = (int)entity.getContentLength();
-        if (i < 0) {
-            i = 4096;
-        }
-        ByteArrayBuffer buffer = new ByteArrayBuffer(i);
         try {
+            if (entity.getContentLength() > Integer.MAX_VALUE) {
+                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
+            }
+            int i = (int)entity.getContentLength();
+            if (i < 0) {
+                i = 4096;
+            }
+            ByteArrayBuffer buffer = new ByteArrayBuffer(i);
             byte[] tmp = new byte[4096];
             int l;
             while((l = instream.read(tmp)) != -1) {
                 buffer.append(tmp, 0, l);
             }
+            return buffer.toByteArray();
         } finally {
             instream.close();
         }
-        return buffer.toByteArray();
     }
 
     /**
@@ -177,32 +177,32 @@ public final class EntityUtils {
         if (instream == null) {
             return null;
         }
-        if (entity.getContentLength() > Integer.MAX_VALUE) {
-            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
-        }
-        int i = (int)entity.getContentLength();
-        if (i < 0) {
-            i = 4096;
-        }
-        String charset = getContentCharSet(entity);
-        if (charset == null) {
-            charset = defaultCharset;
-        }
-        if (charset == null) {
-            charset = HTTP.DEFAULT_CONTENT_CHARSET;
-        }
-        Reader reader = new InputStreamReader(instream, charset);
-        CharArrayBuffer buffer = new CharArrayBuffer(i);
         try {
+            if (entity.getContentLength() > Integer.MAX_VALUE) {
+                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
+            }
+            int i = (int)entity.getContentLength();
+            if (i < 0) {
+                i = 4096;
+            }
+            String charset = getContentCharSet(entity);
+            if (charset == null) {
+                charset = defaultCharset;
+            }
+            if (charset == null) {
+                charset = HTTP.DEFAULT_CONTENT_CHARSET;
+            }
+            Reader reader = new InputStreamReader(instream, charset);
+            CharArrayBuffer buffer = new CharArrayBuffer(i);
             char[] tmp = new char[1024];
             int l;
             while((l = reader.read(tmp)) != -1) {
                 buffer.append(tmp, 0, l);
             }
+            return buffer.toString();
         } finally {
-            reader.close();
+            instream.close();
         }
-        return buffer.toString();
     }
 
     /**