com.isti.util
Class FileDownloader

java.lang.Object
  extended by java.lang.Thread
      extended by com.isti.util.IstiThread
          extended by com.isti.util.IstiNotifyThread
              extended by com.isti.util.FileDownloader
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
DownloadFileTest

public class FileDownloader
extends IstiNotifyThread

Class FileDownloader downloads a file with optional dialogs.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class com.isti.util.IstiThread
DEFAULT_TERMINATE_WAIT_TIME, terminateFlag
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
FileDownloader(java.lang.String fileNameStr, java.io.File outputDir, CallBackStringParam callBackObj, IstiDialogInterface dialogObj, ProgressIndicatorInterface progressBarObj)
          Creates a file download thread.
FileDownloader(java.lang.String fileNameStr, java.io.File outputDir, CallBackStringParam callBackObj, IstiDialogInterface dialogObj, ProgressIndicatorInterface progressBarObj, boolean unzipDownloadedFileFlag)
          Creates a file download thread.
 
Method Summary
protected  void backupOutputFile()
          Backup the output file.
protected  java.lang.String checkConnection(java.net.URLConnection urlCon)
          Checks the connection.
protected  java.lang.String checkHttpURLConnection(java.net.HttpURLConnection htpUrlCon)
          Checks the HTTP connection.
protected  boolean checkLastModified()
          Checks the last modified time.
protected  boolean checkLastModified(long inputLastModified, long outputLastModified)
          Checks the last modified time.
protected  void closeStreams()
          Closes the streams.
protected  java.io.File createOutputFileObject(java.net.URL fileUrl)
          Create the output file object.
protected  java.net.URL createURL()
          Create the file URL.
protected  void doThreadFinished(java.lang.String errMsgStr)
          Performs thread-finished operations.
protected  java.io.File getBackupFile(java.io.File outputFile)
          Gets the backup file for the specified output file.
 IstiDialogInterface getDialogObj()
          Get the dialog object.
 java.lang.String getFileNameStr()
          Get the file name string.
 java.io.File getOutputFileObj()
          Returns the file object for the last file downloaded (or attempted).
 ProgressIndicatorInterface getProgressBarObj()
          Get the progress bar object.
protected  void initProgressBar()
          Initialize the progress bar.
 boolean isCloseWhenDone()
          Determines if dialog is closed when the download is done.
 boolean isDownloadDone()
          Returns the completion status of the download attempt.
 void requestDialogFocus()
          Perfoms a 'requestFocus()' on the currently-displayed dialog (if any).
 void run()
          Runs the thread.
 void setBackupFile(boolean b)
          Determines if the output file should be backed up if it already exists.
 void setCallBackObj(CallBackStringParam callBackObj)
          Sets the call-back object to use used.
 void setCheckConnection(boolean b)
          Determines if the connection should be checked.
 void setCheckLastModified(boolean b)
          Determines if the last modified time should be checked.
 void setCloseWhenDone(boolean b)
          Sets if the dialog is closed when the download is done.
 void setDeleteFileAfterUnzip(boolean b)
          Determines if the file should be deleted after unzip.
protected  void setDownloadDone(java.lang.String errMsgStr)
          Sets the download done.
 void setFileNameStr(java.lang.String fileNameStr)
          Set the file name string.
 void terminate()
          Deprecated. Should use the terminateThread method instead.
 void terminateThread(java.lang.String msgStr)
          Terminates this download thread.
protected  void threadFinished(java.lang.String errMsgStr)
          Performs thread-finished operations (if terminate flag not set).
protected  void transferStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Transfers data from the input stream to the output stream.
protected  void unzipZipFile()
          Unzip the file.
 
Methods inherited from class com.isti.util.IstiNotifyThread
sleep
 
Methods inherited from class com.isti.util.IstiThread
clearThreadWaitNotifyFlag, isTerminated, nextThreadNum, notifyThread, start, terminate, waitForNotify, waitForNotify, waitForTerminate, waitForTerminate, wasStarted
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileDownloader

public FileDownloader(java.lang.String fileNameStr,
                      java.io.File outputDir,
                      CallBackStringParam callBackObj,
                      IstiDialogInterface dialogObj,
                      ProgressIndicatorInterface progressBarObj,
                      boolean unzipDownloadedFileFlag)
Creates a file download thread. It downloads the specified file to the output directory.

Parameters:
fileNameStr - the installer file name string.
outputDir - the output directory.
callBackObj - if not null then a call-back object whose method is called (after the download is complete) with a string parameter containing null if successful or an error message if an error occurred.
dialogObj - the dialog object or null for none.
progressBarObj - the progress bar object or null for none.
unzipDownloadedFileFlag - true to unzip file after download (if it is a ZIP file); false to not unzip file.

FileDownloader

public FileDownloader(java.lang.String fileNameStr,
                      java.io.File outputDir,
                      CallBackStringParam callBackObj,
                      IstiDialogInterface dialogObj,
                      ProgressIndicatorInterface progressBarObj)
Creates a file download thread. It downloads the specified file to the output directory and if the file is a ZIP file it unzips the file.

Parameters:
fileNameStr - the installer file name string.
outputDir - the output directory.
callBackObj - if not null then a call-back object whose method is called (after the download is complete) with a string parameter containing null if successful or an error message if an error occurred.
dialogObj - the dialog object or null for none.
progressBarObj - the progress bar object or null for none.
Method Detail

setBackupFile

public void setBackupFile(boolean b)
Determines if the output file should be backed up if it already exists. The default is false.

Parameters:
b - true if the output file should be backed up if it already exists.

setCallBackObj

public void setCallBackObj(CallBackStringParam callBackObj)
Sets the call-back object to use used.

Parameters:
callBackObj - if not null then a call-back object whose method is called (after the download is complete) with a string parameter containing null if successful or an error message if an error occurred.

setCheckConnection

public void setCheckConnection(boolean b)
Determines if the connection should be checked. The default is true.

Parameters:
b - true if the connection should be checked.

setCheckLastModified

public void setCheckLastModified(boolean b)
Determines if the last modified time should be checked. The default is false.

Parameters:
b - true if the last modified time should be checked.

setCloseWhenDone

public void setCloseWhenDone(boolean b)
Sets if the dialog is closed when the download is done.

Parameters:
b - true if dialog is closed when the download is done, false otherwise.

setDeleteFileAfterUnzip

public void setDeleteFileAfterUnzip(boolean b)
Determines if the file should be deleted after unzip. The default is true.

Parameters:
b - true if the file should be deleted after unzip.

backupOutputFile

protected void backupOutputFile()
Backup the output file.


checkConnection

protected java.lang.String checkConnection(java.net.URLConnection urlCon)
                                    throws java.io.IOException
Checks the connection.

Parameters:
urlCon - the URL connection.
Returns:
the error message or null if none.
Throws:
java.io.IOException - if an I/O error has occurred.

checkHttpURLConnection

protected java.lang.String checkHttpURLConnection(java.net.HttpURLConnection htpUrlCon)
                                           throws java.io.IOException
Checks the HTTP connection.

Parameters:
htpUrlCon - the HTTP URL connection.
Returns:
the error message or null if none.
Throws:
java.io.IOException - if an I/O error has occurred.

checkLastModified

protected boolean checkLastModified()
Checks the last modified time.

Returns:
true if the download should be terminated.

checkLastModified

protected boolean checkLastModified(long inputLastModified,
                                    long outputLastModified)
Checks the last modified time.

Parameters:
inputLastModified - the input last modified time.
outputLastModified - the output last modified time.
Returns:
true if the download should be terminated.

createURL

protected java.net.URL createURL()
                          throws java.net.MalformedURLException
Create the file URL.

Returns:
the file URL.
Throws:
java.net.MalformedURLException - if the string specifies an unknown protocol.

createOutputFileObject

protected java.io.File createOutputFileObject(java.net.URL fileUrl)
Create the output file object.

Parameters:
fileUrl - the file URL.
Returns:
the output file object.

getBackupFile

protected java.io.File getBackupFile(java.io.File outputFile)
Gets the backup file for the specified output file.

Parameters:
outputFile - the output file.
Returns:
the backup file.

getDialogObj

public IstiDialogInterface getDialogObj()
Get the dialog object.

Returns:
the dialog object or null for none.

getFileNameStr

public java.lang.String getFileNameStr()
Get the file name string.

Returns:
the file name string

getProgressBarObj

public ProgressIndicatorInterface getProgressBarObj()
Get the progress bar object.

Returns:
the the progress bar object or null for none.

initProgressBar

protected void initProgressBar()
Initialize the progress bar.


setFileNameStr

public void setFileNameStr(java.lang.String fileNameStr)
Set the file name string.

Parameters:
fileNameStr - the file name string.

unzipZipFile

protected void unzipZipFile()
                     throws java.io.IOException,
                            java.util.zip.ZipException
Unzip the file.

Throws:
java.util.zip.ZipException - if a ZIP error has occurred.
java.io.IOException - if an I/O error has occurred.

run

public void run()
Runs the thread.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setDownloadDone

protected void setDownloadDone(java.lang.String errMsgStr)
Sets the download done.

Parameters:
errMsgStr - error message to be entered into the call-back object, or null for none.

closeStreams

protected void closeStreams()
Closes the streams.


transferStream

protected void transferStream(java.io.InputStream inputStream,
                              java.io.OutputStream outputStream)
                       throws java.io.IOException
Transfers data from the input stream to the output stream.

Parameters:
inputStream - the input stream.
outputStream - the output stream.
Throws:
java.io.IOException - if an I/O error occurs.

threadFinished

protected void threadFinished(java.lang.String errMsgStr)
Performs thread-finished operations (if terminate flag not set).

Parameters:
errMsgStr - error message to be entered into the call-back object, or null for none.

doThreadFinished

protected void doThreadFinished(java.lang.String errMsgStr)
Performs thread-finished operations.

Parameters:
errMsgStr - error message to be entered into the call-back object, or null for none.

terminate

public void terminate()
Deprecated. Should use the terminateThread method instead.

Terminates this thread if the thread is not terminated and alive.

Overrides:
terminate in class IstiNotifyThread
See Also:
terminateThread

terminateThread

public void terminateThread(java.lang.String msgStr)
Terminates this download thread.

Parameters:
msgStr - message string given to call-back object.

requestDialogFocus

public void requestDialogFocus()
Perfoms a 'requestFocus()' on the currently-displayed dialog (if any).


isCloseWhenDone

public boolean isCloseWhenDone()
Determines if dialog is closed when the download is done.

Returns:
true if dialog is closed when the download is done, false otherwise.

isDownloadDone

public boolean isDownloadDone()
Returns the completion status of the download attempt.

Returns:
true if the download attempt has finish; false if not.

getOutputFileObj

public java.io.File getOutputFileObj()
Returns the file object for the last file downloaded (or attempted).

Returns:
The file object for the last file downloaded, or null if none.