Skip to content

Commit

Permalink
Deprecated misplaced method that introduced a dependency between the …
Browse files Browse the repository at this point in the history
…protocol layer and generic utility code
  • Loading branch information
ok2c committed Sep 14, 2023
1 parent b046ba0 commit e7bbed8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -111,6 +112,14 @@ private static int toContentLength(final int contentLength) {
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.
*
Expand All @@ -122,7 +131,7 @@ private static int toContentLength(final int contentLength) {
*/
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;
Expand Down Expand Up @@ -150,7 +159,7 @@ public static byte[] toByteArray(final HttpEntity entity) throws IOException {
*/
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;
Expand Down Expand Up @@ -206,7 +215,7 @@ private static CharArrayBuffer toCharArrayBuffer(final InputStream inStream, fin
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;
Expand Down Expand Up @@ -396,7 +405,7 @@ public static List<NameValuePair> parse(final HttpEntity entity) throws IOExcept
*/
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();
Expand Down
4 changes: 4 additions & 0 deletions httpcore5/src/main/java/org/apache/hc/core5/util/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static void check(final boolean expression, final String message, final O
}
}

/**
* @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,
Expand Down

0 comments on commit e7bbed8

Please sign in to comment.