Skip to content

Commit 10108ba

Browse files
committed
Better performance, messaging, and comments
1 parent a345880 commit 10108ba

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/main/java/com/mashape/unirest/http/Unirest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public static void setConcurrency(int maxTotal, int maxPerRoute) {
111111
/**
112112
* Set the max response size.
113113
*
114-
* @param maxTotal Defines the maximum response size for non-multipart responses. (Multipart responses, which include
115-
* file downloads, will have no size limit).
114+
* @param maxResponseSize Defines the maximum response size for non-multipart responses. (Multipart responses, which
115+
* include file downloads, will have no size limit).
116116
*/
117117
public static void setMaxResponseSize(long maxResponseSize) {
118118
Options.setOption(Option.MAX_RESPONSE_SIZE, maxResponseSize);

src/main/java/com/mashape/unirest/http/utils/ResponseUtils.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,29 @@ public static boolean isMultipartContentType(Header contentType) {
5757
}
5858

5959
public static void checkResponseSize(Long contentLength) throws IOException {
60-
Number maxResponse = (Number) Options.getOption(Option.MAX_RESPONSE_SIZE);
61-
if (maxResponse == null) {
60+
checkMaxResponseSize(contentLength, getMaxResponseAsLong());
61+
}
62+
63+
public static void checkMaxResponseSize(Long contentLength, long maxResponse) throws IOException {
64+
if (contentLength == null) {
6265
return;
6366
}
64-
long maxAsLong= maxResponse.longValue();
65-
if (maxAsLong > -1 && maxAsLong < contentLength) {
66-
throw new IOException("Declared response content too large: " + maxResponse + " > " + contentLength);
67+
if (maxResponse > -1 && maxResponse < contentLength) {
68+
throw new IOException("Response content too large: " + maxResponse + " < " + contentLength);
6769
}
6870
}
6971

72+
public static long getMaxResponseAsLong() {
73+
Number maxResponse = (Number) Options.getOption(Option.MAX_RESPONSE_SIZE);
74+
if (maxResponse == null) return -1;
75+
return maxResponse.longValue();
76+
}
77+
7078
public static byte[] getBytes(InputStream is, boolean checkSize) throws IOException {
7179
int len;
7280
int size = 1024;
7381
byte[] buf;
82+
checkSize = checkSize && Options.getOption(Option.MAX_RESPONSE_SIZE) != null;
7483

7584
if (is instanceof ByteArrayInputStream) {
7685
size = is.available();
@@ -80,6 +89,7 @@ public static byte[] getBytes(InputStream is, boolean checkSize) throws IOExcept
8089
buf = new byte[size];
8190
len = is.read(buf, 0, size);
8291
} else {
92+
long maxResponse = getMaxResponseAsLong();
8393
long total = 0;
8494
ByteArrayOutputStream bos = new ByteArrayOutputStream();
8595
buf = new byte[size];
@@ -88,7 +98,7 @@ public static byte[] getBytes(InputStream is, boolean checkSize) throws IOExcept
8898
while ((len = is.read(buf, 0, size)) != -1) {
8999
total += len;
90100
if (checkSize) {
91-
ResponseUtils.checkResponseSize(total);
101+
ResponseUtils.checkMaxResponseSize(total, maxResponse);
92102
}
93103
bos.write(buf, 0, len);
94104
}

src/test/java/com/mashape/unirest/test/http/UnirestTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class UnirestTest {
6464
public void setUp() {
6565
lock = new CountDownLatch(1);
6666
status = false;
67+
Options.setOption(Option.MAX_RESPONSE_SIZE, -1);
6768
}
6869

6970
private String findAvailableIpAddress() throws UnknownHostException, IOException {

0 commit comments

Comments
 (0)