hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: HTTPClient 2.0 - Limiting response body read length
Date Mon, 06 Dec 2004 18:32:38 GMT
Steve,

Do not override the method. Better provide a simple helper class to
consume the response body as a stream. See this discussion for
additional pointers:


http://nagoya.apache.org/eyebrowse/ReadMsg?listName=httpclient-
user@jakarta.apache.org&msgNo=103

We are trying to deemphasize the use of HttpMethod#getResponseBody and
HttpMethod#getResponseBodyAsString and deprecate these methods
altogether in 4.0

Oleg


On Mon, 2004-12-06 at 08:24 -0800, Steve Johnson wrote: 
> Hi All,
> 
>  
> 
> HTTPClient 2.0 - Limiting response body read length
> 
>  
> 
> This code seems to indicate that it is not possible to limit how
> 
> much stuff gets read. Is this true?
> 
>  
> 
>  HttpMethodBase.  
> 
>  public byte[] getResponseBody() {
> 
>         if (this.responseBody == null) {
> 
>             try {
> 
>                 InputStream instream = getResponseBodyAsStream();
> 
>                 if (instream != null) {
> 
>                     LOG.debug("Buffering response body");
> 
>                     ByteArrayOutputStream outstream = new ByteArrayOutputStream();
> 
>                     byte[] buffer = new byte[4096];
> 
>                     int len;
> 
>                     while ((len = instream.read(buffer)) > 0) {
> 
>                         outstream.write(buffer, 0, len);
> 
>                     }
> 
>                     outstream.close();
> 
>                     setResponseStream(null);
> 
>                     this.responseBody = outstream.toByteArray();
> 
>                 }
> 
>             } catch (IOException e) {
> 
>                 LOG.error("I/O failure reading response body", e);
> 
>                 this.responseBody = null;
> 
>             }
> 
>         }
> 
>         return this.responseBody;
> 
>     }
> 
>  
> 
> If so, would it be best to override this method with a method
> 
> to allow limiting the length like this:
> 
>  
> 
> while ((len = instream.read(buffer)) > 0 && totalLength+len < MaxDesiredLength)
{
> 
>                         totalLength += len;
> 
>                         outstream.write(buffer, 0, len);
> 
>                     }
> 
>  
> 
> Thanks for your time and effort,
> 
>  
> 
> Steve Johnson, Software Engineer, sjohnson@mercury.com
> 
> direct 720.564.6532 
> 
> www.mercury.com <http://www.mercury.com/>  
> 
>  <http://www.mercury.com/> 
> 
>  <http://www.mercury.com/>  
> 
>  
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Mime
View raw message