ファイルをダウンロードしていますが、ダウンロード速度を KBps 単位で決定しようとしています。方程式を思いついたのですが、奇妙な結果が得られています。
try (BufferedInputStream in = new BufferedInputStream(url.openStream());
FileOutputStream out = new FileOutputStream(file)) {
byte[] buffer = new byte[4096];
int read = 0;
while (true) {
long start = System.nanoTime();
if ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
} else {
break;
}
int speed = (int) ((read * 1000000000.0) / ((System.nanoTime() - start) * 1024.0));
}
}
それは私に100から300,000の間のどこかを与えています。これを正しいダウンロード速度にするにはどうすればよいですか? ありがとう