Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 592 forks source link

Add Download Progress Monitor #336

Closed LywLover closed 4 years ago

LywLover commented 4 years ago

Is your feature request related to a problem? Please describe. I need download large files and provide some time of progress bar to a user

Describe the solution you'd like Add Download Progress Monitor like uploadMonitor :)

ryber commented 4 years ago

When you download them what "asResult" method are you using? asFile()? or something else?

LywLover commented 4 years ago

I using asFile()

ryber commented 4 years ago

I've got a version of this working locally. It needs a little cleanup. I'll probably release something this weekend.

However, just a warning. The length of the body is not entirely determinable. We will use the following in order:

  1. The Content-Length header if present (it is not required strictly)
  2. The available bytes from the input stream which is maybe a horrible thing to try and use. I'm going to do some tests with different downloads to see how accurate it is. If it's not accurate at all I might just say it's Content-Length or nothing.

Both of these are impacted by streaming connections. If the server just keeps feeding bytes without telling me the length then Unirest is just going to keep reading until it's gone. We will hit up the monitor on the buffer but there is a chance the total size will be zero. This might be ok for a spinner, but not great for a bar or estimated time of completion.

ryber commented 4 years ago

(if you or anyone else has some magical way of knowing the length of an input stream without reading it I'm more than open to suggestions)

ryber commented 4 years ago

complete in 3.6.00