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 2019/11/19 13:35:33 UTC

[httpcomponents-core] branch master updated (a50d790 -> f45003f)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git.


 discard a50d790  EntityUtils clean ups and internal refactoring (#161)
     new f45003f  EntityUtils clean ups and internal refactoring (#161)

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (a50d790)
            \
             N -- N -- N   refs/heads/master (f45003f)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


[httpcomponents-core] 01/01: EntityUtils clean ups and internal refactoring (#161)

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit f45003f11d5d4871d3f928734fa4a9520247f5d5
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Mon Nov 18 11:55:20 2019 -0500

    EntityUtils clean ups and internal refactoring (#161)
    
    * Refactor and reuse magic numbers into constants.
    * Refactor some common code into private methods.
---
 .../hc/core5/http/io/entity/EntityUtils.java       | 83 ++++++++++++----------
 .../main/java/org/apache/hc/core5/util/Args.java   |  5 +-
 2 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
index ca37f5f..8e406c5 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EntityUtils.java
@@ -55,7 +55,12 @@ import org.apache.hc.core5.util.CharArrayBuffer;
  */
 public final class EntityUtils {
 
+    private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
+    private static final int DEFAULT_CHAR_BUFFER_SIZE = 1024;
+    private static final int DEFAULT_BYTE_BUFFER_SIZE = 4096;
+
     private EntityUtils() {
+        // NoOp
     }
 
     /**
@@ -64,13 +69,13 @@ public final class EntityUtils {
      *
      * @param entity the entity to consume.
      *
-     *
      * @since 4.2
      */
     public static void consumeQuietly(final HttpEntity entity) {
         try {
           consume(entity);
         } catch (final IOException ignore) {
+            // Ignore exception
         }
     }
 
@@ -93,7 +98,17 @@ public final class EntityUtils {
     }
 
     /**
-     * Read the contents of an entity and return it as a byte array.
+     * Gets a usable content length value for the given candidate.
+     *
+     * @param contentLength an integer.
+     * @return The given content length or {@value #DEFAULT_BYTE_BUFFER_SIZE} if it is &lt 0.
+     */
+    private static int toContentLength(final int contentLength) {
+        return contentLength < 0 ? DEFAULT_BYTE_BUFFER_SIZE : contentLength;
+    }
+
+    /**
+     * Reads the contents of an entity and return it as a byte array.
      *
      * @param entity the entity to read from=
      * @return byte array containing the entity content. May be null if
@@ -103,16 +118,13 @@ public final class EntityUtils {
      */
     public static byte[] toByteArray(final HttpEntity entity) throws IOException {
         Args.notNull(entity, "Entity");
+        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return null;
             }
-            int i = (int) Args.checkContentLength(entity);
-            if (i < 0) {
-                i = 4096;
-            }
-            final ByteArrayBuffer buffer = new ByteArrayBuffer(i);
-            final byte[] tmp = new byte[4096];
+            final ByteArrayBuffer buffer = new ByteArrayBuffer(contentLength);
+            final byte[] tmp = new byte[DEFAULT_BYTE_BUFFER_SIZE];
             int l;
             while ((l = inStream.read(tmp)) != -1) {
                 buffer.append(tmp, 0, l);
@@ -121,17 +133,26 @@ public final class EntityUtils {
         }
     }
 
-    private static String toString(
-            final HttpEntity entity,
-                    final ContentType contentType) throws IOException {
+    private static CharArrayBuffer toCharArrayBuffer(final InputStream inStream, final long contentLength,
+            final Charset charset) throws IOException {
+        final Charset actualCharset = charset == null ? DEFAULT_CHARSET : charset;
+        final CharArrayBuffer buf = new CharArrayBuffer(
+                contentLength > 0 ? (int) contentLength : DEFAULT_CHAR_BUFFER_SIZE);
+        final Reader reader = new InputStreamReader(inStream, actualCharset);
+        final char[] tmp = new char[DEFAULT_CHAR_BUFFER_SIZE];
+        int chReadCount;
+        while ((chReadCount = reader.read(tmp)) != -1) {
+            buf.append(tmp, 0, chReadCount);
+        }
+        return buf;
+    }
+
+    private static String toString(final HttpEntity entity, final ContentType contentType) throws IOException {
+        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return null;
             }
-            int contentLength = (int) Args.checkContentLength(entity);
-            if (contentLength < 0) {
-                contentLength = 4096;
-            }
             Charset charset = null;
             if (contentType != null) {
                 charset = contentType.getCharset();
@@ -140,22 +161,12 @@ public final class EntityUtils {
                     charset = defaultContentType != null ? defaultContentType.getCharset() : null;
                 }
             }
-            if (charset == null) {
-                charset = StandardCharsets.ISO_8859_1;
-            }
-            final Reader reader = new InputStreamReader(inStream, charset);
-            final CharArrayBuffer buffer = new CharArrayBuffer(contentLength);
-            final char[] tmp = new char[1024];
-            int chReadCount;
-            while ((chReadCount = reader.read(tmp)) != -1) {
-                buffer.append(tmp, 0, chReadCount);
-            }
-            return buffer.toString();
+            return toCharArrayBuffer(inStream, contentLength, charset).toString();
         }
     }
 
     /**
-     * Get the entity content as a String, using the provided default character set
+     * Gets the entity content as a String, using the provided default character set
      * if none is found in the entity.
      * If defaultCharset is null, the default "ISO-8859-1" is used.
      *
@@ -192,7 +203,7 @@ public final class EntityUtils {
     }
 
     /**
-     * Get the entity content as a String, using the provided default character set
+     * Gets the entity content as a String, using the provided default character set
      * if none is found in the entity.
      * If defaultCharset is null, the default "ISO-8859-1" is used.
      *
@@ -212,7 +223,7 @@ public final class EntityUtils {
     }
 
     /**
-     * Read the contents of an entity and return it as a String.
+     * Reads the contents of an entity and return it as a String.
      * The content is converted using the character set from the entity (if any),
      * failing that, "ISO-8859-1" is used.
      *
@@ -234,6 +245,7 @@ public final class EntityUtils {
      * The encoding is taken from the entity's Content-Encoding header.
      * <p>
      * This is typically used while parsing an HTTP POST.
+     * </p>
      *
      * @param entity
      *            The entity to parse
@@ -243,26 +255,19 @@ public final class EntityUtils {
      */
     public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
         Args.notNull(entity, "HTTP entity");
+        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
         final ContentType contentType = ContentType.parse(entity.getContentType());
         if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
             return Collections.emptyList();
         }
-        final long len = entity.getContentLength();
-        Args.checkRange(len, 0, Integer.MAX_VALUE, "HTTP entity is too large");
         final Charset charset = contentType.getCharset() != null ? contentType.getCharset()
-                        : StandardCharsets.ISO_8859_1;
+                        : DEFAULT_CHARSET;
         final CharArrayBuffer buf;
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return Collections.emptyList();
             }
-            buf = new CharArrayBuffer(len > 0 ? (int) len : 1024);
-            final Reader reader = new InputStreamReader(inStream, charset);
-            final char[] tmp = new char[1024];
-            int l;
-            while ((l = reader.read(tmp)) != -1) {
-                buf.append(tmp, 0, l);
-            }
+            buf = toCharArrayBuffer(inStream, contentLength, charset);
 
         }
         if (buf.isEmpty()) {
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
index aeab2c7..b7867a0 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
@@ -52,8 +52,9 @@ public class Args {
     }
 
     public static long checkContentLength(final EntityDetails entityDetails) {
-        // -1 is a special value
-        // 0 is allowed as well
+        // -1 is a special value,
+        // 0 is allowed as well,
+        // but never more than Integer.MAX_VALUE.
         return checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
                         "HTTP entity too large to be buffered in memory)");
     }