hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Johnson <sjohn...@mercury.com>
Subject RE: HTTPClient 2.0 - Limiting response body read length
Date Wed, 08 Dec 2004 15:32:05 GMT
Hi Oleg and All,

Thank you for this excellent information.
The next problem is that when the inputStream is prematurely closed the auto-watcher continues
consuming the
stream looking for EOF, which we don't want.
We want to stop reading so that we do not spend time or memory on
more info than we want.
// see HttpMethodBase.releaseConnection(), close(), notifyWatcher(), close, close,
ChunkedInputStream.exhaustInputStream().
https://websmp101.sap-ag.de/~sapidb/011000358700000604972003D

Thanks for your help,



Steve Johnson, Software Engineer, sjohnson@mercury.com
direct 720.564.6532 
www.mercury.com 

 
 
www.mercury.com 

-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org] 
Sent: Monday, December 06, 2004 11:33 AM
To: HttpClient Project
Cc: David Clements; Steve Johnson
Subject: Re: HTTPClient 2.0 - Limiting response body read length

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/>  
> 
>  
> 
> 
> 

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message