com.duowan.mobile.netroid.toolbox
Class FileDownloader

java.lang.Object
  extended by com.duowan.mobile.netroid.toolbox.FileDownloader

public class FileDownloader
extends java.lang.Object

This class provided continuous transmission on the breakpoint download task management utilities. As long as the site support(enable 'Content-Range' Header), we'd retrieve last download temporary file when we start to download, fetch the temporary file length as current Range begin point, then the last downloaded data effective. Sadly is when the server-side didn't support download by Range, we'll delete the temporary file and start download from beginning. Usage: like ImageLoader, the best way to use this class is create by RequestQueue and stand Singleton, just get the only one instance to do everything in anywhere. To start a new download request, invoke the add(String, String, com.duowan.mobile.netroid.Listener) to pass in the task parameters, it will deploy to the Task Queue and execute as soon as possible. Note: For the multithreading and bandwidth limit reason, we normally start two parallel tasks to download data, don't over three, and remember keep one idle thread to perform common http request at least.


Nested Class Summary
 class FileDownloader.DownloadController
          This class included all such as PAUSE, RESUME, DISCARD to manipulating download task, it created by add(String, String, com.duowan.mobile.netroid.Listener), offer three params to constructing FileDownloadRequest then perform http downloading, you can check the download status whenever you want to know.
 
Constructor Summary
FileDownloader(RequestQueue queue, int parallelTaskCount)
          Construct Downloader and init the Task Queue.
 
Method Summary
 FileDownloader.DownloadController add(java.lang.String storeFilePath, java.lang.String url, Listener<java.lang.Void> listener)
          Create a new download request, this request might not run immediately because the parallel task limitation, you can check the status by the FileDownloader.DownloadController which you got after invoke this method.
 void clearAll()
          Clear all tasks, make the Task Queue empty.
 FileDownloader.DownloadController get(java.lang.String storeFilePath, java.lang.String url)
          Scanning the Task Queue, fetch a FileDownloader.DownloadController who match the two parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileDownloader

public FileDownloader(RequestQueue queue,
                      int parallelTaskCount)
Construct Downloader and init the Task Queue.

Parameters:
queue - The RequestQueue for dispatching Download task.
parallelTaskCount - Allows parallel task count, don't forget the value must less than ThreadPoolSize of the RequestQueue.
Method Detail

add

public FileDownloader.DownloadController add(java.lang.String storeFilePath,
                                             java.lang.String url,
                                             Listener<java.lang.Void> listener)
Create a new download request, this request might not run immediately because the parallel task limitation, you can check the status by the FileDownloader.DownloadController which you got after invoke this method. Note: don't perform this method twice or more with same parameters, because we didn't check for duplicate tasks, it rely on developer done. Note: this method should invoke in the main thread.

Parameters:
storeFilePath - Once download successed, we'll find it by the store file path.
url - The download url.
listener - The event callback by status;
Returns:
The task controller allows pause or resume or discard operation.

get

public FileDownloader.DownloadController get(java.lang.String storeFilePath,
                                             java.lang.String url)
Scanning the Task Queue, fetch a FileDownloader.DownloadController who match the two parameters.

Parameters:
storeFilePath - The storeFilePath to compare.
url - The url to compare.
Returns:
The matched FileDownloader.DownloadController.

clearAll

public void clearAll()
Clear all tasks, make the Task Queue empty.