How to compress (lossly) longs into ints?

Upgraded to java 7, got a new problem:
java 7 introduced the URLConnection.getContentLengthLong() method, which i want to use (for those wonderful 2gb+ downloads)

Anyway, the problem is that jprogressbar and all the ancillary classes is only prepared to accept ints. This is not so critical considering it’s almost all view classes (taking care to be careful around the edges since we don’t want it to look “not started” or “done” when it’s not) - but the long needs to be compressed into a int

I was thinking of something like:
0L is int 0
1L … (totalL -1L) is int mapping(1L) … mapping(totalL -1L) where some number of contiguous mappings give the same int value
totalL is int mapping(total) that is unique (to make JProgressBar look full only at the end)

I’m terrible at maths. Help?

Unless you expect 2TB (!) files, you can divide all values by 1024 and cast them to ints.

something like:
int map(long i, long max){
if (max <= Integer.MAX_VALUE)//don’t compress without need
return (int) i;

int newI = (int) i / 1024;
if(newI == 0 && i != 0L) return 1;
if(newI == ((int)max/1024) && i != max) return ((int)max/1024)-1;
return newI;
}

?

hmmm screw this, 2gb+ downloads stay undetermined.

No, always divide by 1024. Who cares about sub-1K amounts.

Who needs to even map? You can simply multiply the percentage done by the range the JProgressBar goes :wink:

Good idea thanks.