java在while循环下载过程中如何计算Mbit / s [英] java howto calculate Mbit/s during while loop download
问题描述
我有这个而
循环
下载文件
< $(pre>
while((val = bis.read(buffer,0,1024))> 0){
out.write(buffer,0,val);
fileSize - = val;
if(fileSize< 1024){
val =(int)fileSize;
}
试图找出如何显示Mbit / s像许多速度网站一样。 (链接)
http://www.speedtest.net/
。
想要将图像留在而
循环中,但我有
看到的例子使用分钟线程发射,但我dunno ..
我不知道数据量,或数据总是1024.
thats关于我认为的问题
任何帮助是grate?
我可能会错过订单一般的想法如下:
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 have this while
loop
downloading a file
while ((val = bis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, val);
fileSize -= val;
if (fileSize < 1024) {
val = (int) fileSize;
}
Trying to figure out how to show Mbit/s like many speed sites do.(link)
http://www.speedtest.net/
.
Would like the mesurment to stay inside the while
loop but i have
seen examples using on-minute-threads firing but i dunno..
I dont know the amount of data, or the data is always 1024.
thats on of the problems i think
Any help is grate?
I might be wrong by an order of magnitude or two, by the general idea is as follows:
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)
}
Note that this calculates average speed from the beginning, not the current velocity.
这篇关于java在while循环下载过程中如何计算Mbit / s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!