Re: [PR] Added a new DownloadProgress class to Get.java to display a progress bar. [ant]

mhdirkse (via GitHub) <[email protected]>
Newsgroups gmane.comp.jakarta.ant.devel
Message-ID <PR_kwDOATT2FM6qe5ru-88fb328e-ee55-451c-99db-8a4cf94e6017@gitbox.apache.org>
mhdirkse commented on code in PR #224:
URL: https://github.com/apache/ant/pull/224#discussion_r2417172528


##########
src/main/org/apache/tools/ant/taskdefs/Get.java:
##########
@@ -654,8 +662,109 @@ public void endDownload() {
         }
     }
 
-    private class GetThread extends Thread {
+    public class ProgressBarProgress implements DownloadProgress {
+        private long contentLength;
+        private long downloadedBytes;
+        private long lastDownloadedBytes;
+        private final PrintStream out;
+        private long lastTime;
+        private int previousLineLength = 0;
+
+        public ProgressBarProgress(PrintStream out) {
+            this.out = out;
+        }
+
+        public void setContentLength(long contentLength) {
+            this.contentLength = contentLength;
+        }
+
+        @Override
+        public void beginDownload() {
+            downloadedBytes = 0;
+            lastDownloadedBytes = 0;
+            lastTime = System.nanoTime();
+        }
+
+        @Override
+        public void onTick() {
+            int downloadedBytesPercentage = (int) ((downloadedBytes * 100) / contentLength);
+            printProgressBar(downloadedBytesPercentage, calculateDownloadSpeed());
+        }
+
+        public double calculateDownloadSpeed() {
+            long bytesSinceLast = downloadedBytes - lastDownloadedBytes;
+            long elapsedTime = System.nanoTime() - lastTime;
+            return (elapsedTime > 0)
+                ? (bytesSinceLast / (elapsedTime / 1_000_000_000.0))
+                : 0.0;
+        }
+
+        public void printProgressBar(int downloadedBytesPercentage, double downloadSpeed) {
+            int size = 50;

Review Comment:
   Ah, sorry. Should not have written that in Dutch! I asked here whether "const" could be added to the definition of variable `size`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.