Mapping Long values to Integer values ?

Hi I have a task I run in the back-end (a file download) and I'd like to display a progressbar. the progressbar however only accepts ints as max value and current value. now I was wondering, what would be the best way (and preferrably also the most performant way) to map the Long values to Int values.

atm I'm thinking of using a division by 4294967298 (plus or minus one) which is Long.MAX_VALUE divided by Integer.MAX_VALUE and would provide the most accurate mapping. however I think a better method is possible so I'm asking if anyone has any ideas.

[570 byte] By [boran_bloka] at [2007-10-2 7:39:21]
# 1
I guess you want to update the progress bar with the number of bytes you've downloaded so far and that the files size (in bytes) is bigger than Integer.MAX_VALUE? What about just displaying kb (size/1000) or mb (size/1000000)?
fossill1a at 2007-7-16 21:22:32 > top of Java-index,Other Topics,Algorithms...
# 2
because you're not sure what to take eh. filesizes vary from 0 to Long.Max_VALUE so dividing by 1000 or 1000000 would be conditional depending on size. i'd rather have a 1-1 mapping. (meaning one integer value corresponds to a set of long values)
boran_bloka at 2007-7-16 21:22:32 > top of Java-index,Other Topics,Algorithms...
# 3
I somehow underestimated the vastness of long. even 640 MB files when divided by that number result in a zero value when converted to int. :/ well another method will be found.
boran_bloka at 2007-7-16 21:22:32 > top of Java-index,Other Topics,Algorithms...
# 4

Well, and int is 32 bits and a long is 64. So the right way to do what you want is to shift the current counter right 32 bits (that is, lose the 32 least significant bits). However, if you do this, you will only increase the current counter by one for every 2^32 bytes - that is, every 4Gb. Not very friendly unless you often move files bigger than 4Gb!

I would do what the other reply said - if you know the size of your file, then just set the progress bar to show a percentage complete.

dannyyatesa at 2007-7-16 21:22:32 > top of Java-index,Other Topics,Algorithms...