org.jets3t.service.io
Class BytesProgressWatcher

java.lang.Object
  extended by org.jets3t.service.io.BytesProgressWatcher

public class BytesProgressWatcher
extends java.lang.Object

Utility class that tracks the number of bytes transferred from a source, and uses this information to calculate transfer rates and estimate end times. The watcher stores the number of bytes that will be transferred, the number of bytes that have been transferred in the current session and the time this has taken, and the number of bytes and time taken overal (eg for transfers that have been restarted).

Author:
James Murty

Field Summary
static int SECONDS_OF_HISTORY
          The number of seconds worth of historical byte transfer information that will be stored and used to calculate the recent transfer rate.
 
Constructor Summary
BytesProgressWatcher(long bytesToTransfer)
          Construct a watcher for a transfer that will involve a given number of bytes.
 
Method Summary
static double calculateOverallBytesPerSecond(BytesProgressWatcher[] progressWatchers)
           
static long calculateRecentByteRatePerSecond(BytesProgressWatcher[] progressWatchers)
           
static long calculateRemainingTime(BytesProgressWatcher[] progressWatchers)
           
 long getBytesRemaining()
           
 long getBytesToTransfer()
           
 long getBytesTransferred()
           
protected  long getElapsedTimeMS()
           
protected  long getHistoricStartTimeMS()
           
 double getRecentByteRatePerSecond()
           
 long getRemainingTime()
           
protected  long getTotalBytesInAllTransfers()
           
protected  boolean isStarted()
           
 void resetWatcher()
          Resets the byte count and timer variables for a watcher.
static long sumBytesToTransfer(BytesProgressWatcher[] progressWatchers)
           
static long sumBytesTransferred(BytesProgressWatcher[] progressWatchers)
           
 void updateBytesTransferred(long byteCount)
          Notifies this watcher that bytes have been transferred.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SECONDS_OF_HISTORY

public static final int SECONDS_OF_HISTORY
The number of seconds worth of historical byte transfer information that will be stored and used to calculate the recent transfer rate.

See Also:
Constant Field Values
Constructor Detail

BytesProgressWatcher

public BytesProgressWatcher(long bytesToTransfer)
Construct a watcher for a transfer that will involve a given number of bytes.

Parameters:
bytesToTransfer - the number of bytes that will be transferred, eg the size of a file being uploaded.
Method Detail

getBytesToTransfer

public long getBytesToTransfer()
Returns:
the count of bytes that will be transferred by the object watched by this class.

resetWatcher

public void resetWatcher()
Resets the byte count and timer variables for a watcher. This method is called automatically when a transfer is started (ie the first bytes are registered in the method updateBytesTransferred(long)), or when a transfer is restarted (eg due to transmission errors).


updateBytesTransferred

public void updateBytesTransferred(long byteCount)
Notifies this watcher that bytes have been transferred.

Parameters:
byteCount - the number of bytes that have been transferred.

getBytesTransferred

public long getBytesTransferred()
Returns:
the number of bytes that have so far been transferred in the most recent transfer session.

getBytesRemaining

public long getBytesRemaining()
Returns:
the number of bytes that are remaining to be transferred.

getRemainingTime

public long getRemainingTime()
Returns:
an estimate of the time (in seconds) it will take for the transfer to completed, based on the number of bytes remaining to transfer and the overall bytes/second rate.

getRecentByteRatePerSecond

public double getRecentByteRatePerSecond()
Returns:
the byte rate (per second) based on the historical information for the last SECONDS_OF_HISTORY seconds before the current time.

getElapsedTimeMS

protected long getElapsedTimeMS()
Returns:
the number of milliseconds time elapsed for a transfer. The value returned is the time elapsed so far if the transfer is ongoing, the total time taken for the transfer if it is complete, or 0 if the transfer has not yet started.

getTotalBytesInAllTransfers

protected long getTotalBytesInAllTransfers()
Returns:
the number of bytes that have been transferred over all sessions, including any sessions that have been restarted.

isStarted

protected boolean isStarted()

getHistoricStartTimeMS

protected long getHistoricStartTimeMS()
Returns:
the time (in milliseconds) when the first bytes were transferred, regardless of how many times the transfer was reset.

sumBytesToTransfer

public static long sumBytesToTransfer(BytesProgressWatcher[] progressWatchers)
Parameters:
progressWatchers - all the watchers involved in the same byte transfer operation.
Returns:
the total number of bytes to transfer.

sumBytesTransferred

public static long sumBytesTransferred(BytesProgressWatcher[] progressWatchers)
Parameters:
progressWatchers - all the watchers involved in the same byte transfer operation.
Returns:
the total number of bytes already transferred.

calculateRemainingTime

public static long calculateRemainingTime(BytesProgressWatcher[] progressWatchers)
Parameters:
progressWatchers - all the watchers involved in the same byte transfer operation.
Returns:
an estimate of the time (in seconds) it will take for the transfer to completed, based on the number of bytes remaining to transfer and the overall bytes/second rate.

calculateOverallBytesPerSecond

public static double calculateOverallBytesPerSecond(BytesProgressWatcher[] progressWatchers)
Parameters:
progressWatchers - all the watchers involved in the same byte transfer operation.
Returns:
the overall rate of bytes/second over all transfers for all watchers.

calculateRecentByteRatePerSecond

public static long calculateRecentByteRatePerSecond(BytesProgressWatcher[] progressWatchers)
Parameters:
progressWatchers - all the watchers involved in the same byte transfer operation.
Returns:
the rate of bytes/second that has been achieved recently (ie within the last SECONDS_OF_HISTORY seconds).