Erik Erik - 3 months ago 12
Java Question

java how to show user friendly formatted output regarding bandwidth speed during download

Want to show nicely formatted output regarding bandwidth speed during download

I have this calculation below thanks to @Tomasz Nurkiewicz, and it show

mega*bytes* per second when i download a file.

long start = System.nanoTime();
long totalRead = 0;
final double NANOS_PER_SECOND = 1000000000.0;
final double BYTES_PER_MIB = 1024 * 1024;

while ((val = bis.read(buffer, 0, 1024)) > 0) {
//...
totalRead += val;
double speed = NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1)
}


Would like it to be like this. I get mega*bytes* per second from the
calculation and from that i enter a
if statement
to select on
KByte/s, MBit/s (not sure) or just like a normal FTP client show speed.

if( KByte/s something) {
System.out.print(your current speed xx KB/s);
}else if(MByte/s something){
System.out.print(your current speed xx MB/s);
}


My problem is what do i put in the if statement?.

hope you understand what i try to do

Answer

There is a FileUtils.byteCountToDisplaySize() method in Apache Commons IO:

System.out.println(
  "your current speed is " + FileUtils.byteCountToDisplaySize(12345) + "/s"
)

// your current speed is 12 KB/s

Also see (possible duplicates):

Comments