From adc8139d09e823def62d0773763353f7dd2e238b Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@QQ.com> Date: Mon, 18 Apr 2016 09:22:12 +0800 Subject: [PATCH 1/2] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b7049f81..4329e817 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # DownloadUtil -这是android 文件下载工具类,实现了多线程断点续传功能 +这是android 文件下载工具类,实现了多线程断点续传功能
+如果你觉得我的代码对你有帮助,请麻烦你在右上角给我一个star.^_^ #下载 [![Download](https://api.bintray.com/packages/arialyy/maven/MTDownloadUtil/images/download.svg)](https://bintray.com/arialyy/maven/MTDownloadUtil/_latestVersion)
From 2276003dfb9595f249b9d4363c1775fdd6623b6d Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Tue, 10 May 2016 16:38:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/downloadutil/DownLoadUtil.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java index 45674bb8..4b2df16d 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java @@ -37,7 +37,7 @@ public class DownLoadUtil { private boolean isStop = false; private boolean isCancel = false; private static final int TIME_OUT = 5000; //超时时间 - boolean newTask = true; + boolean isNewTask = true; private int mCancelNum = 0; private int mStopNum = 0; @@ -92,17 +92,16 @@ public class DownLoadUtil { final File configFile = new File(context.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties"); try { if (!configFile.exists()) { //记录文件被删除,则重新下载 - newTask = true; + isNewTask = true; Util.createFile(configFile.getPath()); } else { - newTask = false; + isNewTask = !dFile.exists(); } } catch (Exception e) { e.printStackTrace(); mListener.onFail(); return; } - newTask = !dFile.exists(); new Thread(new Runnable() { @Override public void run() { @@ -133,8 +132,24 @@ public class DownLoadUtil { //分配每条线程的下载区间 Properties pro = null; pro = Util.loadConfig(configFile); + if (pro.isEmpty()) { + isNewTask = true; + } else { + for (int i = 0; i < THREAD_NUM; i++) { + if(pro.getProperty(dFile.getName() + "_record_" + i) == null){ + isNewTask = true; + break; + } + } + } + int blockSize = fileLength / THREAD_NUM; SparseArray tasks = new SparseArray<>(); + int[] recordL = new int[THREAD_NUM]; + int rl = 0; + for (int i = 0; i < THREAD_NUM; i++) { + recordL[i] = -1; + } for (int i = 0; i < THREAD_NUM; i++) { long startL = i * blockSize, endL = (i + 1) * blockSize; Object state = pro.getProperty(dFile.getName() + "_state_" + i); @@ -142,6 +157,8 @@ public class DownLoadUtil { mCurrentLocation += endL - startL; Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++"); mCompleteThreadNum++; + mStopNum++; + mCancelNum++; if (mCompleteThreadNum == THREAD_NUM) { if (configFile.exists()) { configFile.delete(); @@ -155,12 +172,18 @@ public class DownLoadUtil { } //分配下载位置 Object record = pro.getProperty(dFile.getName() + "_record_" + i); - if (!newTask && record != null && Long.parseLong(record + "") > 0) { //如果有记录,则恢复下载 + if (!isNewTask && record != null && Long.parseLong(record + "") > 0) { //如果有记录,则恢复下载 Long r = Long.parseLong(record + ""); mCurrentLocation += r - startL; Log.d(TAG, "++++++++++ 线程_" + i + "_恢复下载 ++++++++++"); mListener.onChildResume(r); startL = r; + recordL[rl] = i; + rl++; + } + if (isNewTask) { + recordL[rl] = i; + rl++; } if (i == (THREAD_NUM - 1)) { endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度 @@ -174,8 +197,9 @@ public class DownLoadUtil { } else { mListener.onStart(mCurrentLocation); } - for (int i = 0, count = tasks.size(); i < count; i++) { - Thread task = tasks.get(i); + for (int l : recordL) { + if (l == -1) continue; + Thread task = tasks.get(l); if (task != null) { task.start(); }