title: Response size limit when using Apache HttpComponents
body: <p>I am converting some code from the Http Client 3.x library over to the Http Components 4.x library. The old code contains a check to make sure that the response is not over a certain size. This is fairly easy to do in Http Client 3.x since you can get back a stream from the response using the getResponseBodyAsStream() method and determine when the size has been exceeded. I can't find a similar way in Http Components.</p>
  
  <p>Here's the old code as an example of what I'm trying to do:</p>
  
  <pre><code>private static final long RESPONSE_SIZE_LIMIT = 1024 * 1024 * 10;
  private static final int READ_BUFFER_SIZE = 16384;
  
  private static ByteArrayOutputStream readResponseBody(HttpMethodBase method)
          throws IOException {
  
      int len;
      byte buff[] = new byte[READ_BUFFER_SIZE];
      ByteArrayOutputStream out = null;
      InputStream in = null;
      long byteCount = 0;
  
      in = method.getResponseBodyAsStream();
  
      out = new ByteArrayOutputStream(READ_BUFFER_SIZE);
  
      while ((len = in.read(buff)) != -1 &amp;&amp; byteCount &lt;= RESPONSE_SIZE_LIMIT) {
          byteCount += len;
          out.write(buff, 0, len);
      }
  
      if (byteCount &gt;= RESPONSE_SIZE_LIMIT) {
          throw new IOException(
                  "Size limited exceeded reading from HTTP input stream");
      }
  
      return (out);
  
  }
  </code></pre>