@@ -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 }
0 commit comments