com.duowan.mobile.netroid.request
Class FileDownloadRequest

java.lang.Object
  extended by com.duowan.mobile.netroid.Request<java.lang.Void>
      extended by com.duowan.mobile.netroid.request.FileDownloadRequest
All Implemented Interfaces:
java.lang.Comparable<Request<java.lang.Void>>

public class FileDownloadRequest
extends Request<java.lang.Void>

Its purpose is provide a big file download impmenetation, suport continuous transmission on the breakpoint download if server-side enable 'Content-Range' Header. for example: execute a request and submit header like this : Range=bytes=1000- (1000 means the begin point of the file). response return a header like this Content-Range=bytes 1000-1895834/1895835, that's continuous transmission, also return Accept-Ranges=bytes tell us the server-side supported range transmission. This request will stay longer in the thread which dependent your download file size, that will fill up your thread poll as soon as possible if you launch many request, if all threads is busy, the high priority request such as StringRequest might waiting long time, so don't use it alone. we highly recommend you to use it with the FileDownloader, FileDownloader maintain a download task queue, let's set the maximum parallel request count, the rest will await. By the way, this request priority was Priority#LOW, higher request will jump ahead it.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.duowan.mobile.netroid.Request
Request.Method, Request.Priority
 
Constructor Summary
FileDownloadRequest(java.lang.String storeFilePath, java.lang.String url)
           
 
Method Summary
 Request.Priority getPriority()
          Returns the Request.Priority of this request; Request.Priority.NORMAL by default.
 byte[] handleResponse(org.apache.http.HttpResponse response, Delivery delivery)
          In this method, we got the Content-Length, with the TemporaryFile length, we can calculate the actually size of the whole file, if TemporaryFile not exists, we'll take the store file length then compare to actually size, and if equals, we consider this download was already done.
protected  Response<java.lang.Void> parseNetworkResponse(NetworkResponse response)
          Ignore the response content, just rename the TemporaryFile to StoreFile.
 void prepare()
          Init or reset the Range header, ensure the begin position always be the temporary file size.
 void setCacheExpireTime(java.util.concurrent.TimeUnit timeUnit, int amount)
          Never use cache in this case.
 
Methods inherited from class com.duowan.mobile.netroid.Request
addHeader, addMarker, cancel, compareTo, deliverCancel, deliverDownloadProgress, deliverError, deliverFinish, deliverNetworking, deliverPreExecute, deliverRetry, deliverSuccess, deliverUsedCache, finish, getBody, getBodyContentType, getCacheExpireTime, getCacheKey, getHeaders, getMethod, getParams, getParamsEncoding, getRetryPolicy, getSequence, getTag, getTimeoutMs, getUrl, hasHadResponseDelivered, isCanceled, isForceUpdate, markDelivered, parseNetworkError, perform, removeHeader, setForceUpdate, setListener, setRequestQueue, setRetryPolicy, setSequence, setTag, shouldCache, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileDownloadRequest

public FileDownloadRequest(java.lang.String storeFilePath,
                           java.lang.String url)
Method Detail

prepare

public void prepare()
Init or reset the Range header, ensure the begin position always be the temporary file size.

Overrides:
prepare in class Request<java.lang.Void>

parseNetworkResponse

protected Response<java.lang.Void> parseNetworkResponse(NetworkResponse response)
Ignore the response content, just rename the TemporaryFile to StoreFile.

Specified by:
parseNetworkResponse in class Request<java.lang.Void>
Parameters:
response - Response from the network
Returns:
The parsed response, or null in the case of an error

handleResponse

public byte[] handleResponse(org.apache.http.HttpResponse response,
                             Delivery delivery)
                      throws java.io.IOException,
                             ServerError
In this method, we got the Content-Length, with the TemporaryFile length, we can calculate the actually size of the whole file, if TemporaryFile not exists, we'll take the store file length then compare to actually size, and if equals, we consider this download was already done. We used RandomAccessFile to continue download, when download success, the TemporaryFile will be rename to StoreFile.

Overrides:
handleResponse in class Request<java.lang.Void>
Throws:
java.io.IOException
ServerError

getPriority

public Request.Priority getPriority()
Description copied from class: Request
Returns the Request.Priority of this request; Request.Priority.NORMAL by default.

Overrides:
getPriority in class Request<java.lang.Void>

setCacheExpireTime

public void setCacheExpireTime(java.util.concurrent.TimeUnit timeUnit,
                               int amount)
Never use cache in this case.

Overrides:
setCacheExpireTime in class Request<java.lang.Void>
Parameters:
timeUnit - what unit for the amount value
amount - how much unit should calculate