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 2023/09/15 13:30:19 UTC

[httpcomponents-core] 08/09: Deprecated misplaced method that introduced a dependency between the protocol layer and generic utility code

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 bfba5f9619943cbe9b3b8d4cbc90b997e89d45a6
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Sep 14 16:40:57 2023 +0200

    Deprecated misplaced method that introduced a dependency between the protocol layer and generic utility code
---
 .../org/apache/hc/core5/http/io/entity/EntityUtils.java | 17 +++++++++++++----
 .../src/main/java/org/apache/hc/core5/util/Args.java    |  4 ++++
 2 files changed, 17 insertions(+), 4 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 ebc0731cb..c282815e2 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
@@ -41,6 +41,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
 import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.NameValuePair;
 import org.apache.hc.core5.http.ParseException;
@@ -111,6 +112,14 @@ public final class EntityUtils {
         return contentLength < 0 ? DEFAULT_BYTE_BUFFER_SIZE : contentLength;
     }
 
+    static long checkContentLength(final EntityDetails entityDetails) {
+        // -1 is a special value,
+        // 0 is allowed as well,
+        // but never more than Integer.MAX_VALUE.
+        return Args.checkRange(entityDetails.getContentLength(), -1, Integer.MAX_VALUE,
+                "HTTP entity too large to be buffered in memory)");
+    }
+
     /**
      * Reads the contents of an entity and return it as a byte array.
      *
@@ -122,7 +131,7 @@ public final class EntityUtils {
      */
     public static byte[] toByteArray(final HttpEntity entity) throws IOException {
         Args.notNull(entity, "HttpEntity");
-        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
+        final int contentLength = toContentLength((int) checkContentLength(entity));
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return null;
@@ -150,7 +159,7 @@ public final class EntityUtils {
      */
     public static byte[] toByteArray(final HttpEntity entity, final int maxResultLength) throws IOException {
         Args.notNull(entity, "HttpEntity");
-        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
+        final int contentLength = toContentLength((int) checkContentLength(entity));
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return null;
@@ -206,7 +215,7 @@ public final class EntityUtils {
     private static String toString(final HttpEntity entity, final ContentType contentType, final int maxResultLength)
             throws IOException {
         Args.notNull(entity, "HttpEntity");
-        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
+        final int contentLength = toContentLength((int) checkContentLength(entity));
         try (final InputStream inStream = entity.getContent()) {
             if (inStream == null) {
                 return null;
@@ -396,7 +405,7 @@ public final class EntityUtils {
      */
     public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws IOException {
         Args.notNull(entity, "HttpEntity");
-        final int contentLength = toContentLength((int) Args.checkContentLength(entity));
+        final int contentLength = toContentLength((int) checkContentLength(entity));
         final ContentType contentType = ContentType.parse(entity.getContentType());
         if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
             return Collections.emptyList();
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 f5363f8e9..94e5f6bd3 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
@@ -54,6 +54,10 @@ public class Args {
         }
     }
 
+    /**
+     * @deprecated Use {@link #checkRange(long, long, long, String)}.
+     */
+    @Deprecated
     public static long checkContentLength(final EntityDetails entityDetails) {
         // -1 is a special value,
         // 0 is allowed as well,