cz.vity.freerapid.core.tasks
Class DownloadTask

java.lang.Object
  extended by org.jdesktop.swingworker.SwingWorker<T,V>
      extended by org.jdesktop.application.Task<T,V>
          extended by cz.vity.freerapid.core.tasks.CoreTask<Void,Long>
              extended by cz.vity.freerapid.core.tasks.DownloadTask
All Implemented Interfaces:
HttpFileDownloadTask, Runnable, Future<Void>
Direct Known Subclasses:
DownloadNewPluginsTask, RunCheckTask

public class DownloadTask
extends CoreTask<Void,Long>
implements HttpFileDownloadTask

Author:
Vity

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jdesktop.application.Task
org.jdesktop.application.Task.BlockingScope, org.jdesktop.application.Task.InputBlocker
 
Nested classes/interfaces inherited from class org.jdesktop.swingworker.SwingWorker
org.jdesktop.swingworker.SwingWorker.StateValue
 
Field Summary
protected  HttpDownloadClient client
           
protected  DownloadFile downloadFile
           
protected  File outputFile
           
protected  ShareDownloadService service
           
protected  File storeFile
           
 
Fields inherited from class cz.vity.freerapid.core.tasks.CoreTask
inputBlocker
 
Constructor Summary
DownloadTask(org.jdesktop.application.Application application)
           
DownloadTask(org.jdesktop.application.Application application, HttpDownloadClient client, DownloadFile downloadFile, ShareDownloadService service)
           
 
Method Summary
protected  void cancelled()
           
protected  Void doInBackground()
           
protected  void error(Throwable cause)
           
protected  void failed(Throwable cause)
           
 HttpDownloadClient getClient()
          Client associated with current file - its HTTP connections are used to grab a file
 HttpFile getDownloadFile()
          Returns file that is given to be downloaded
protected  OutputStream getFileOutputStream(File f, long fileSize)
           
 DownloadTaskError getServiceError()
           
 int getYouHaveToSleepSecondsTime()
           
protected  int checkExists()
           
protected  void init()
           
protected  void initBackground()
           
protected  void initDownloadThread()
           
protected  void interrupted(InterruptedException e)
           
 boolean isTerminated()
          Checks whether current downloading process has been stopped
 void saveToFile(InputStream inputStream)
          Method that handles direct saving file onto physical disc.
protected  void setAverageSpeed(float avgSpeed)
           
protected  void setDownloaded(long counter)
           
protected  void setDownloadFile(DownloadFile downloadFile)
           
protected  void setFileErrorMessage(Throwable cause)
           
protected  void setSleep(int sleep)
           
protected  void setSpeed(long speedInBytes)
           
 void sleep(int seconds)
          Sets file download state to WAITING.
protected  void succeeded(Void result)
           
protected  void updateFileState(Throwable cause, boolean connectError)
           
protected  boolean useTemporaryFiles()
           
 
Methods inherited from class cz.vity.freerapid.core.tasks.CoreTask
getMainFrame, getTaskResourceMap, handleRuntimeException, postMessage, setTaskToForeground
 
Methods inherited from class org.jdesktop.application.Task
addTaskListener, done, finished, getApplication, getContext, getDescription, getExecutionDuration, getInputBlocker, getMessage, getMessageDuration, getResourceMap, getTaskListeners, getTaskService, getTitle, getUserCanCancel, isPending, isProgressPropertyValid, isStarted, message, process, removeTaskListener, resourceName, setDescription, setInputBlocker, setMessage, setProgress, setProgress, setProgress, setTitle, setUserCanCancel
 
Methods inherited from class org.jdesktop.swingworker.SwingWorker
addPropertyChangeListener, cancel, execute, firePropertyChange, get, get, getProgress, getPropertyChangeSupport, getState, isCancelled, isDone, publish, removePropertyChangeListener, run, setProgress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

client

protected HttpDownloadClient client

downloadFile

protected DownloadFile downloadFile

service

protected ShareDownloadService service

outputFile

protected File outputFile

storeFile

protected File storeFile
Constructor Detail

DownloadTask

public DownloadTask(org.jdesktop.application.Application application)

DownloadTask

public DownloadTask(org.jdesktop.application.Application application,
                    HttpDownloadClient client,
                    DownloadFile downloadFile,
                    ShareDownloadService service)
Method Detail

init

protected void init()

doInBackground

protected Void doInBackground()
                       throws Exception
Specified by:
doInBackground in class org.jdesktop.swingworker.SwingWorker<Void,Long>
Throws:
Exception

initDownloadThread

protected void initDownloadThread()

getFileOutputStream

protected OutputStream getFileOutputStream(File f,
                                           long fileSize)
                                    throws NotEnoughSpaceException,
                                           IOException
Throws:
NotEnoughSpaceException
IOException

initBackground

protected void initBackground()

isTerminated

public boolean isTerminated()
Description copied from interface: HttpFileDownloadTask
Checks whether current downloading process has been stopped

Specified by:
isTerminated in interface HttpFileDownloadTask
Returns:
true if downloading of the file was interrupted

saveToFile

public void saveToFile(InputStream inputStream)
                throws Exception
Description copied from interface: HttpFileDownloadTask
Method that handles direct saving file onto physical disc. file download state is set to COMPLETED automatically

Specified by:
saveToFile in interface HttpFileDownloadTask
Parameters:
inputStream - - Http response stream, which contains data to be saved on the disk - should not be null
Throws:
Exception - Error during writing or if inputStream is null

useTemporaryFiles

protected boolean useTemporaryFiles()

cancelled

protected void cancelled()
Overrides:
cancelled in class org.jdesktop.application.Task<Void,Long>

setSpeed

protected void setSpeed(long speedInBytes)

setSleep

protected void setSleep(int sleep)

setDownloaded

protected void setDownloaded(long counter)

setAverageSpeed

protected void setAverageSpeed(float avgSpeed)

failed

protected void failed(Throwable cause)
Overrides:
failed in class CoreTask<Void,Long>

updateFileState

protected void updateFileState(Throwable cause,
                               boolean connectError)

error

protected void error(Throwable cause)

setFileErrorMessage

protected void setFileErrorMessage(Throwable cause)

interrupted

protected void interrupted(InterruptedException e)
Overrides:
interrupted in class org.jdesktop.application.Task<Void,Long>

succeeded

protected void succeeded(Void result)
Overrides:
succeeded in class org.jdesktop.application.Task<Void,Long>

checkExists

protected int checkExists()
                   throws InvocationTargetException,
                          InterruptedException
Throws:
InvocationTargetException
InterruptedException

sleep

public void sleep(int seconds)
           throws InterruptedException
Description copied from interface: HttpFileDownloadTask
Sets file download state to WAITING. Stops download thread for given time.

Specified by:
sleep in interface HttpFileDownloadTask
Parameters:
seconds - time to sleep
Throws:
InterruptedException - if the thread was interrupted during sleeping

getDownloadFile

public HttpFile getDownloadFile()
Description copied from interface: HttpFileDownloadTask
Returns file that is given to be downloaded

Specified by:
getDownloadFile in interface HttpFileDownloadTask
Returns:
file for downloading

getClient

public HttpDownloadClient getClient()
Description copied from interface: HttpFileDownloadTask
Client associated with current file - its HTTP connections are used to grab a file

Specified by:
getClient in interface HttpFileDownloadTask
Returns:
actual instance of HttpDownloadClient

getServiceError

public DownloadTaskError getServiceError()

getYouHaveToSleepSecondsTime

public int getYouHaveToSleepSecondsTime()

setDownloadFile

protected void setDownloadFile(DownloadFile downloadFile)