You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by George Armhold <ar...@cs.rutgers.edu> on 2004/03/02 12:32:20 UTC

[HttpClient] GetMethod.getResponseBody().available() for download estimation?

Hi,

Is there any way to get an InputStream back from
GetMethod.getResponseBody() whose available() method returns a
reasonable estimation of the content-length of the requested resource?
I'm trying to use a ProgressMonitorInputStream to monitor a download,
but the available() method is returning something like 1K on a
multi-megabyte download.  The API docs state that available() should
return the amount of data that is available without blocking, so
perhaps this the amount of data that the underlying IO system has
buffered.  For what it's worth, I get the same results when I code the
thing by hand using a URLConnection.  BTW I did verify with a HEAD
request that the webserver was returning a valid content-length for
the URL I am using

Or perhaps there's a better way to get a download progress indicator?
Another idea is to do a HEAD request to get the content-length first,
then do my GetMethod, and override ProgressMonitorInputStream to
return the true content-length from available().  Thoughts?

Thanks.



-- 
George Armhold
Rutgers University
eLearning Grant, DCIS


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


Re: [HttpClient] GetMethod.getResponseBody().available() for download estimation?

Posted by George Armhold <ar...@cs.rutgers.edu>.
Michael Becke wrote:
> Hi George,
> 
> Please file an enhancement request in Bugzilla.  This change could most 
> likely be made in the post 2.0 code.  As a side note, the content length 
> (if present) can be read from the response headers.

Done.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=27366

Thanks!

-- 
George Armhold
Rutgers University
eLearning Grant, DCIS


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


Re: [HttpClient] GetMethod.getResponseBody().available() for download estimation?

Posted by Michael Becke <be...@u.washington.edu>.
Hi George,

Please file an enhancement request in Bugzilla.  This change could most 
likely be made in the post 2.0 code.  As a side note, the content 
length (if present) can be read from the response headers.

Mike

On Mar 2, 2004, at 7:20 AM, George Armhold wrote:

> Well I've been able to get a ProgressMonitorInputStream working with
> GetMethod.  I can now get a ProgressMonitor to pop up for my
> downloads.  I've had to wrap the InputStream returned from
> getResponseBodyAsStream() with a FilterInputStream that returns the
> contentLength for available().  I also had to extend GetMethod to make
> getResponseContentLength() public.  Why is it protected??  This is
> kind of ugly, but it works.
>
> /**
>  * supply a hard-coded value for available() method.
>  */
> class FixedInputStream extends FilterInputStream {
>   private int contentLength;
>
>   public FixedInputStream(InputStream is,
> 			  int contentLength) {
>     super(is);
>     this.contentLength = contentLength;
>   }
>
>   public int available() throws IOException {
>     return contentLength;
>   }
> }
>
> /**
>  * expose protected method getResponseContentLength.
>  */
> class FixedGetMethod extends GetMethod {
>
>   public FixedGetMethod(String url) {
>     super(url);
>   }
>
>   public int getResponseContentLength() {
>     return super.getResponseContentLength();
>   }
> }
>
>
>
> -- 
> George Armhold
> Rutgers University
> eLearning Grant, DCIS
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


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


Re: [HttpClient] GetMethod.getResponseBody().available() for download estimation?

Posted by George Armhold <ar...@cs.rutgers.edu>.
Well I've been able to get a ProgressMonitorInputStream working with
GetMethod.  I can now get a ProgressMonitor to pop up for my
downloads.  I've had to wrap the InputStream returned from
getResponseBodyAsStream() with a FilterInputStream that returns the
contentLength for available().  I also had to extend GetMethod to make
getResponseContentLength() public.  Why is it protected??  This is
kind of ugly, but it works.

/**
  * supply a hard-coded value for available() method.
  */
class FixedInputStream extends FilterInputStream {
   private int contentLength;

   public FixedInputStream(InputStream is,
			  int contentLength) {
     super(is);
     this.contentLength = contentLength;
   }

   public int available() throws IOException {
     return contentLength;
   }
}

/**
  * expose protected method getResponseContentLength.
  */
class FixedGetMethod extends GetMethod {

   public FixedGetMethod(String url) {
     super(url);
   }

   public int getResponseContentLength() {
     return super.getResponseContentLength();
   }
}



-- 
George Armhold
Rutgers University
eLearning Grant, DCIS


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