Java how to show friendly formatted output about bandwidth at download time

Want to show well-formatted output about bandwidth speed at boot time

I have this calculation below thanks to @Tomasz Nurkiewicz, and it shows mega * bytes * per second when I upload the 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) } 

I wish it were so. I get mega * bytes * per second from the calculation, and from this I will enter a if statement to select KByte / s, MBit / s (not sure) or just like the speed of a regular FTP client.

 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 that I am inserting into the if? Statement.

hope you understand what I'm trying to do

0
source share
2 answers

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):

+2
source

I really don’t understand what exactly you want, it is confusing that you have mega ** bytes per second on your switch.

As you seem to know, the switch statement must have either an enumeration or an int, and that the current number is not.

If you want to automatically switch from Kbit / s to Mbit / s as the number increases, I think you want to use the if statement with a threshold.

If you want to accept the parameter that the user sets as a preference, you just need to pass this parameter (either an enumeration or int) to this function so that it can process the response in the required format.

If you do not want to do any of this, I find your question a bit confusing.

+1
source

All Articles