0

I've written a program in Java to use chunking to download large files over http. Everything is working fine but I'd like to add a progress bar which I can't do unless I get the total length of the file. Is there any way to do this?

Joe
  • 73
  • 1
  • 10

1 Answers1

0

If the server provides a Content-Length header field, then that's easy. If it doesn't, then you're out of luck.

Reasonable server implementations provide Content-Length when the content is static and has a known size. If the content is dynamically generated, then it's impossible to know the length a priori, at least without making two passes through the data.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Content-Length appears to give me the size for just the current chunk. Is that possible? – Joe Jun 08 '11 at 20:50
  • @Joe: `Content-Length` represents the length of the entire content. If that's the same length as the first chunk, then that means there's only one chunk. – C. K. Young Jun 08 '11 at 20:51
  • Alright, that's not what I'm seeing so there's a bug in here somewhere. Time to go hunting! – Joe Jun 08 '11 at 20:53
  • Ok I've played around a little longer and I see that content length is definitely returning the length of the chunk. – Joe Jun 08 '11 at 21:22
  • The whole point of using chunked transfer encoding is that the server doesn't know beforehand how large the data is that it's returning, so it can't give you an overall content length. For example, the server could be compressing the result on the fly, and it won't know the compressed size until it sends back the entire response. You can't do a determinate progress bar in this case. You could do an indeterminate progress bar to indicate that something is happening. – Jesse Barnum Jul 02 '11 at 05:31