From 164b6f9008169181d44b3c8f8e33fcb3f2a980cc Mon Sep 17 00:00:00 2001 From: kappa1 Date: Thu, 22 May 2008 00:04:11 +0000 Subject: [PATCH] added support for displaying the current download speed --- .../org/lwjgl/util/applet/AppletLoader.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/java/org/lwjgl/util/applet/AppletLoader.java b/src/java/org/lwjgl/util/applet/AppletLoader.java index eba3eb5a..b6bd80b6 100644 --- a/src/java/org/lwjgl/util/applet/AppletLoader.java +++ b/src/java/org/lwjgl/util/applet/AppletLoader.java @@ -744,12 +744,34 @@ public class AppletLoader extends Applet implements Runnable, AppletStub { int bufferSize; + long downloadStartTime = System.currentTimeMillis(); + int downloadedAmount = 0; + String downloadSpeedMessage = ""; + while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1) { debug_sleep(10); fos.write(buffer, 0, bufferSize); currentSizeDownload += bufferSize; percentage = initialPercentage + ((currentSizeDownload * 45) / totalSizeDownload); subtaskMessage = "Retrieving: " + currentFile + " " + ((currentSizeDownload * 100) / totalSizeDownload) + "%"; + + downloadedAmount += bufferSize; + long timeLapse = System.currentTimeMillis() - downloadStartTime; + // update only if a second or more has passed + if (timeLapse >= 1000) { + // get kb/s, nice that bytes/millis is same as kilobytes/seconds + float downloadSpeed = (float) bufferSize / timeLapse; + // round to two decimal places + downloadSpeed = ((int)(downloadSpeed*100))/100f; + // set current speed message + downloadSpeedMessage = " @ " + downloadSpeed + " KB/sec"; + // reset downloaded amount + downloadedAmount = 0; + // reset start time + downloadStartTime = System.currentTimeMillis(); + } + + subtaskMessage += downloadSpeedMessage; } inputstream.close();