webwork/src/main/webwork/multipart ProgressMonitor.java,1.2,1.3

[email protected] Thu, 16 Jun 2005 05:18:06 -0700
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/main/webwork/multipart
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29230/src/main/webwork/multipart

Modified Files:
	ProgressMonitor.java 
Log Message:
Added support for getting bitrate and remaining time


Index: ProgressMonitor.java
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/multipart/ProgressMonitor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- ProgressMonitor.java	17 Feb 2004 01:40:41 -0000	1.2
+++ ProgressMonitor.java	16 Jun 2005 12:17:59 -0000	1.3
@@ -7,13 +7,23 @@
  *         Date: Dec 20, 2003
  *         Time: 11:06:59 PM
  */
-class ProgressMonitor implements Serializable
+public class ProgressMonitor implements Serializable
 {
+  private static final long serialVersionUID = 5344649008664921629L;
+  private static final long MIN_SAMPLE_TIME = 150l;
+  private static final int HISTORY_SIZE = 20;
   private String fileName;
   private int read;
   private long size;
   private boolean completed;
-
+  private long startTime;
+  //array of datapoint[0] = bytes in this chunk, datapoint[1] = data in this chunk
+  private int[][] history = new int[HISTORY_SIZE][2];
+  private boolean historyFull = false;
+  private int historyIndex = 0;
+  private long lastHistoryTime;
+  private long lastHistoryTotal;
+  
   public String getFileName()
   {
     return fileName;
@@ -22,6 +32,8 @@
   public void setFileName(String fileName)
   {
     this.fileName = fileName;
+    startTime = System.currentTimeMillis();
+    lastHistoryTime = startTime;
   }
 
   public int getRead()
@@ -31,9 +43,73 @@
 
   public void setRead(int read)
   {
+    long now = System.currentTimeMillis();
+    long timeSinceLastRead = now - lastHistoryTime;
+    if(timeSinceLastRead > MIN_SAMPLE_TIME)
+    {
+      long diff;
+      if(this.read == 0)
+      {
+        diff = read;
+      }
+      else
+      {
+        diff = read - lastHistoryTotal;
+      }
+      history[historyIndex][0] = (int)diff;
+      history[historyIndex][1] = (int)timeSinceLastRead;
+      historyIndex++;
+      if(historyIndex == HISTORY_SIZE)
+      {
+        historyFull = true;
+        historyIndex = 0;
+      }
+      lastHistoryTime = now;
+      lastHistoryTotal = read;
+    }
     this.read = read;
   }
 
+  public long getStartTime()
+  {
+    return startTime;
+  }
+
+  public int getRemainingSeconds()
+  {
+    int bitrate = getBitRate();
+    if(bitrate == 0)
+    {
+      return 0;
+    }
+    return (int)((size - read) / bitrate);
+  }
+  /**
+   * Get the number of bytes per second for this upload.
+   * If the upload has just started or is finished, the rate will be 0
+   * @return
+   */
+  public int getBitRate()
+  {
+    if(completed || startTime == 0)
+    {
+      return 0;
+    }
+    int totalBytes = 0;
+    int totalTime = 0;
+    int end = history.length;
+    if(!historyFull) end = historyIndex;
+    for(int i = 0; i < end; i++)
+    {
+      int[] values = history[i];
+      totalBytes+=values[0];
+      totalTime+=values[1];
+    }
+    if(totalTime == 0) return 0;
+    double timeInSeconds = totalTime / (double)1000;
+    return (int)(totalBytes / timeInSeconds);
+  }
+  
   public long getRemaining()
   {
     return size - read;



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click