org.freeshell.zs.common
Class Downloader

java.lang.Object
  extended by org.freeshell.zs.common.Downloader
All Implemented Interfaces:
java.lang.Runnable

public class Downloader
extends java.lang.Object
implements java.lang.Runnable

Download a remote resource.


Constructor Summary
Downloader(java.net.URL url, java.lang.Object target)
          Constructor.
 
Method Summary
 void cancel()
          Cancel the download.
 int getDownloadedLength()
          Get the number of bytes downloaded.
 int getLength()
          Get the length of the remote resource.
 int getProgressPercent()
          Get the percentage describing the current progress.
 java.lang.String getProgressString()
          Get a string describing the current progress.
 boolean isCancelled()
          Is the download cancelled?
 boolean isCompleted()
          Is the download completed?
 boolean isProgressUpdated()
          Has there been an update in the progress? The progressUpdated flag is set to false before this method returns.
 boolean isRunning()
          Is the downloader running?
 boolean isStarted()
          Has the download started?
 void pause()
          Pause the download.
 void resume()
          Resume the download.
 void run()
          Start downloading the remote resource.
 void waitUntilCompleted()
          Wait until the download is completed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Downloader

public Downloader(java.net.URL url,
                  java.lang.Object target)
Constructor. The target object should not be accessed until after calling waitUntilCompleted().

Parameters:
url - URL of the remote resource to be downloaded
target - target object to be populated (File or StringBuilder object)
Method Detail

getLength

public int getLength()
Get the length of the remote resource.

Returns:
length of the remote resource, in number of bytes; -1 if unknown

getDownloadedLength

public int getDownloadedLength()
Get the number of bytes downloaded.

Returns:
number of bytes downloaded

getProgressString

public java.lang.String getProgressString()
Get a string describing the current progress.

Returns:
string describing the current progress

getProgressPercent

public int getProgressPercent()
Get the percentage describing the current progress.

Returns:
percentage describing the current progress; -1 if unknown

isProgressUpdated

public boolean isProgressUpdated()
Has there been an update in the progress? The progressUpdated flag is set to false before this method returns.

Returns:
true if there has been an update in the progress; false otherwise

pause

public void pause()
Pause the download.


resume

public void resume()
Resume the download.


cancel

public void cancel()
Cancel the download.


isStarted

public boolean isStarted()
Has the download started?

Returns:
true if download has started; false otherwise

isRunning

public boolean isRunning()
Is the downloader running?

Returns:
true if downloader is running; false otherwise

isCancelled

public boolean isCancelled()
Is the download cancelled?

Returns:
true if downloader is cancelled; false otherwise

isCompleted

public boolean isCompleted()
Is the download completed?

Returns:
true if download is completed; false otherwise

waitUntilCompleted

public void waitUntilCompleted()
                        throws java.lang.Exception
Wait until the download is completed. The target object should be accessed only after calling this method.

Throws:
java.lang.Exception - if an error has occurred during download, or if the download was cancelled

run

public void run()
Start downloading the remote resource. The target object should not be accessed until after calling waitUntilCompleted().

Specified by:
run in interface java.lang.Runnable