views:

170

answers:

1

I have some code that does a bunch of HTTP GETs, POSTs, and PUTs using Commons HttpClient 3.1. I'd like to provide a current transfer speed indicator in my GUI, but was unable to find any methods for determining the transfer rate of a HttpMethod being processed.

I could easily just time the transfer and do some simple math after it was complete to determine what the speed was, but this provides a bad user experience during a long upload or download. Does anyone know how to determine the transfer rate of a Commons HttpClient request while it is still being processed?

+3  A: 

I haven't used HttpClient extensively, so there may be a simple hook. However, it appears that HttpConnection.getResponseInputStream() returns a simple InputStream.

To add the hook yourself, you'd need to override HttpConnectionManager and HttpConnection, to return a decorated stream that keeps track of the number of bytes read. You could then spin up a second thread to poll this stream and display the transfer rate, or (better) create the stream with a callback every N bytes (better because you don't have to care about concurrency, and you can also set N such that the callback is only invoked for large files).

kdgregory
Looks like I can probably combine this advice with the code from http://docstore.mik.ua/orelly/java-ent/dist/ch08_04.htm and make something work, I'll try it and report back.
Ian Levesque
Works. There's no better way that I can find, unfortunately. Good enough!
Ian Levesque