先附上流程圖 git
/**
*
* @param request 請求實體參數Entity
* @param tag 下載地址
* @param callBack 返回給調用的CollBack
*/
public void download(DownloadRequest request, String tag, CallBack callBack) {
final String key = createKey(tag);
if (check(key)) {
// 請求的響應 須要狀態傳遞類 以及對應的回調
DownloadResponse response = new DownloadResponseImpl(mDelivery, callBack);
// 下載器 須要線程池 數據庫管理者 對應的url key值 以後回調給本身
Downloader downloader = new DownloaderImpl(request, response,
mExecutorService, mDBManager, key, mConfig, this);
mDownloaderMap.put(key, downloader);
//開始下載
downloader.start();
}
}
複製代碼
DownloadResponseImpl 下載響應須要把自己的下載事件回調給調用者,因爲下載是在子線程裏面的,因此專門搞了一個下載狀態的傳遞類github
DownLoaderImpl 下載器 須要的參數就比較多了,請求實體,對應的下載響應,線程池,數據庫管理器,url的hash值,對應的配置,還有下載的回調數據庫
加入進LinkedHashMap 作一個有序的存儲多線程
以後調用下載器的start方法dom
@Override
public void start() {
//修改成Started狀態
mStatus = DownloadStatus.STATUS_STARTED;
//CallBack 回調給調用者
mResponse.onStarted();
// 鏈接獲取是否支持多線程下載
connect();
}
/**
* 執行鏈接任務
*/
private void connect() {
mConnectTask = new ConnectTaskImpl(mRequest.getUri(), this);
mExecutor.execute(mConnectTask);
}
複製代碼
在正式下載以前須要肯定後臺是否支持斷點下載,因此纔有先執行這個ConnectTaskImpl 鏈接任務ide
@Override
public void run() {
// 設置爲後臺線程
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//修改鏈接中狀態
mStatus = DownloadStatus.STATUS_CONNECTING;
//回調給調用者
mOnConnectListener.onConnecting();
try {
//執行鏈接方法
executeConnection();
} catch (DownloadException e) {
handleDownloadException(e);
}
}
/**
*
* @throws DownloadException
*/
private void executeConnection() throws DownloadException {
mStartTime = System.currentTimeMillis();
HttpURLConnection httpConnection = null;
final URL url;
try {
url = new URL(mUri);
} catch (MalformedURLException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Bad url.", e);
}
try {
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(Constants.HTTP.CONNECT_TIME_OUT);
httpConnection.setReadTimeout(Constants.HTTP.READ_TIME_OUT);
httpConnection.setRequestMethod(Constants.HTTP.GET);
httpConnection.setRequestProperty("Range", "bytes=" + 0 + "-");
final int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//後臺不支持斷點下載,啓用單線程下載
parseResponse(httpConnection, false);
} else if (responseCode == HttpURLConnection.HTTP_PARTIAL) {
//後臺支持斷點下載,啓用多線程下載
parseResponse(httpConnection, true);
} else {
throw new DownloadException(DownloadStatus.STATUS_FAILED,
"UnSupported response code:" + responseCode);
}
} catch (ProtocolException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Protocol error", e);
} catch (IOException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "IO error", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
private void parseResponse(HttpURLConnection httpConnection, boolean isAcceptRanges)
throws DownloadException {
final long length;
//header獲取length
String contentLength = httpConnection.getHeaderField("Content-Length");
if (TextUtils.isEmpty(contentLength) || contentLength.equals("0") || contentLength
.equals("-1")) {
//判斷後臺給你length,爲null 0,-1,從鏈接中獲取
length = httpConnection.getContentLength();
} else {
//直接轉化
length = Long.parseLong(contentLength);
}
if (length <= 0) {
//拋出異常數據
throw new DownloadException(DownloadStatus.STATUS_FAILED, "length <= 0");
}
//判斷是否取消和暫停
checkCanceledOrPaused();
//Successful
mStatus = DownloadStatus.STATUS_CONNECTED;
//獲取時間差
final long timeDelta = System.currentTimeMillis() - mStartTime;
//回調給調用者
mOnConnectListener.onConnected(timeDelta, length, isAcceptRanges);
}
private void checkCanceledOrPaused() throws DownloadException {
if (isCanceled()) {
// cancel
throw new DownloadException(DownloadStatus.STATUS_CANCELED, "Connection Canceled!");
} else if (isPaused()) {
// paused
throw new DownloadException(DownloadStatus.STATUS_PAUSED, "Connection Paused!");
}
}
//統一執行對應的異常信息
private void handleDownloadException(DownloadException e) {
switch (e.getErrorCode()) {
case DownloadStatus.STATUS_FAILED:
synchronized (mOnConnectListener) {
mStatus = DownloadStatus.STATUS_FAILED;
mOnConnectListener.onConnectFailed(e);
}
break;
case DownloadStatus.STATUS_PAUSED:
synchronized (mOnConnectListener) {
mStatus = DownloadStatus.STATUS_PAUSED;
mOnConnectListener.onConnectPaused();
}
break;
case DownloadStatus.STATUS_CANCELED:
synchronized (mOnConnectListener) {
mStatus = DownloadStatus.STATUS_CANCELED;
mOnConnectListener.onConnectCanceled();
}
break;
default:
throw new IllegalArgumentException("Unknown state");
}
}
複製代碼
HttpURLConnection.HTTP_OK 不支持斷點下載 使用單線程下載this
HttpURLConnection.HTTP_PARTIAL 支持斷點下載 使用多線程下載url
若是成功就會回調到OnConnectListener.onConnected(timeDelta, length, isAcceptRanges)方法中spa
回到下載器查看onConnected方法線程
@Override
public void onConnected(long time, long length, boolean isAcceptRanges) {
if (mConnectTask.isCanceled()) {
//鏈接取消
onConnectCanceled();
} else {
mStatus = DownloadStatus.STATUS_CONNECTED;
//回調給你響應鏈接成功狀態
mResponse.onConnected(time, length, isAcceptRanges);
mDownloadInfo.setAcceptRanges(isAcceptRanges);
mDownloadInfo.setLength(length);
//真正開始下載
download(length, isAcceptRanges);
}
}
@Override
public void onConnectCanceled() {
deleteFromDB();
deleteFile();
mStatus = DownloadStatus.STATUS_CANCELED;
mResponse.onConnectCanceled();
onDestroy();
}
@Override
public void onDestroy() {
// trigger the onDestroy callback tell download manager
mListener.onDestroyed(mTag, this);
}
複製代碼
根據狀態來處理,isCanceled() 刪除數據庫裏面的數據,刪除文件,更改成取消狀態狀態
未取消,進去下載download
/**
* 下載開始
* @param length 設置下載的長度
* @param acceptRanges 是否支持斷點下載
*/
private void download(long length, boolean acceptRanges) {
mStatus = DownloadStatus.STATUS_PROGRESS;
initDownloadTasks(length, acceptRanges);
//開始下載任務
for (DownloadTask downloadTask : mDownloadTasks) {
mExecutor.execute(downloadTask);
}
}
/**
* 初始化下載任務
* @param length
* @param acceptRanges
*/
private void initDownloadTasks(long length, boolean acceptRanges) {
mDownloadTasks.clear();
if (acceptRanges) {
List<ThreadInfo> threadInfos = getMultiThreadInfos(length);
// init finished
int finished = 0;
for (ThreadInfo threadInfo : threadInfos) {
finished += threadInfo.getFinished();
}
mDownloadInfo.setFinished(finished);
for (ThreadInfo info : threadInfos) {
//開始多線程下載
mDownloadTasks.add(new MultiDownloadTask(mDownloadInfo, info, mDBManager, this));
}
} else {
//單線程下載不須要保存進度信息
ThreadInfo info = getSingleThreadInfo();
mDownloadTasks.add(new SingleDownloadTask(mDownloadInfo, info, this));
}
}
//TODO
private List<ThreadInfo> getMultiThreadInfos(long length) {
// init threadInfo from db
final List<ThreadInfo> threadInfos = mDBManager.getThreadInfos(mTag);
if (threadInfos.isEmpty()) {
final int threadNum = mConfig.getThreadNum();
for (int i = 0; i < threadNum; i++) {
// calculate average
final long average = length / threadNum;
final long start = average * i;
final long end;
if (i == threadNum - 1) {
end = length;
} else {
end = start + average - 1;
}
ThreadInfo threadInfo = new ThreadInfo(i, mTag, mRequest.getUri(), start, end, 0);
threadInfos.add(threadInfo);
}
}
return threadInfos;
}
//單線程數據
private ThreadInfo getSingleThreadInfo() {
ThreadInfo threadInfo = new ThreadInfo(0, mTag, mRequest.getUri(), 0);
return threadInfo;
}
複製代碼
根據connected返回的數據判斷是否支持斷點下載,支持acceptRanges 就調用getMultiThreadInfos來組裝多線程下載數據,多線程須要初始化下載的進度信息,二單線程getSingleThreadInfo本身組裝一個簡單的就能夠了
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// 插入數據庫
insertIntoDB(mThreadInfo);
try {
mStatus = DownloadStatus.STATUS_PROGRESS;
executeDownload();
//根據回調對象,加鎖
synchronized (mOnDownloadListener) {
//沒出異常就表明下載完成了
mStatus = DownloadStatus.STATUS_COMPLETED;
mOnDownloadListener.onDownloadCompleted();
}
} catch (DownloadException e) {
handleDownloadException(e);
}
}
/**
* 開始下載數據
*/
private void executeDownload() throws DownloadException {
final URL url;
try {
url = new URL(mThreadInfo.getUri());
} catch (MalformedURLException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Bad url.", e);
}
HttpURLConnection httpConnection = null;
try {
//設置http鏈接信息
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(HTTP.CONNECT_TIME_OUT);
httpConnection.setReadTimeout(HTTP.READ_TIME_OUT);
httpConnection.setRequestMethod(HTTP.GET);
//設置header數據,斷點下載設置關鍵
setHttpHeader(getHttpHeaders(mThreadInfo), httpConnection);
final int responseCode = httpConnection.getResponseCode();
if (responseCode == getResponseCode()) {
//下載數據
transferData(httpConnection);
} else {
throw new DownloadException(DownloadStatus.STATUS_FAILED,
"UnSupported response code:" + responseCode);
}
} catch (ProtocolException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Protocol error", e);
} catch (IOException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "IO error", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
/**
* 設置header數據
*
* @param headers header元數據
*/
private void setHttpHeader(Map<String, String> headers, URLConnection connection) {
if (headers != null) {
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
}
}
/**
* 下載數據
*/
private void transferData(HttpURLConnection httpConnection) throws DownloadException {
InputStream inputStream = null;
RandomAccessFile raf = null;
try {
try {
inputStream = httpConnection.getInputStream();
} catch (IOException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "http get inputStream error", e);
}
//獲取下載的偏移量
final long offset = mThreadInfo.getStart() + mThreadInfo.getFinished();
try {
//設置偏移量
raf = getFile(mDownloadInfo.getDir(), mDownloadInfo.getName(), offset);
} catch (IOException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "File error", e);
}
//開始寫入數據
transferData(inputStream, raf);
} finally {
try {
IOCloseUtils.close(inputStream);
IOCloseUtils.close(raf);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 寫入數據
*/
private void transferData(InputStream inputStream, RandomAccessFile raf)
throws DownloadException {
final byte[] buffer = new byte[1024 * 8];
while (true) {
checkPausedOrCanceled();
int len = -1;
try {
len = inputStream.read(buffer);
if (len == -1) {
break;
}
raf.write(buffer, 0, len);
//設置下載的信息
mThreadInfo.setFinished(mThreadInfo.getFinished() + len);
synchronized (mOnDownloadListener) {
mDownloadInfo.setFinished(mDownloadInfo.getFinished() + len);
//回調進度
mOnDownloadListener
.onDownloadProgress(mDownloadInfo.getFinished(), mDownloadInfo.getLength());
}
} catch (IOException e) {
//更新數據庫
updateDB(mThreadInfo);
throw new DownloadException(DownloadStatus.STATUS_FAILED, e);
}
}
}
複製代碼
斷點下載的關鍵是在header頭信息裏面添加已經下載length ,下載數據也是從下載的length點開始寫入數據,寫入數據,每一個線程在對應的片斷裏面下載對應的數據,後期使用RandomAccessFile組裝起來,合成一個文件
@Override
protected Map<String, String> getHttpHeaders(ThreadInfo info) {
Map<String, String> headers = new HashMap<String, String>();
//計算開始和結束的位置
long start = info.getStart() + info.getFinished();
long end = info.getEnd();
headers.put("Range", "bytes=" + start + "-" + end);
return headers;
}
@Override
protected RandomAccessFile getFile(File dir, String name, long offset) throws IOException {
File file = new File(dir, name);
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//設置偏移量
raf.seek(offset);
return raf;
}
複製代碼
@Override
protected Map<String, String> getHttpHeaders(ThreadInfo info) {
// simply return null
return null;
}
@Override
protected RandomAccessFile getFile(File dir, String name, long offset) throws IOException {
File file = new File(dir, name);
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(0);
return raf;
}
複製代碼
單線程下載不須要偏移量
具體能夠查看github.com/lizubing199…