From 37c138aa34ef4db284b3c3d3ad8b3e7ccb891b19 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Thu, 22 Jun 2017 21:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/command/AbsCmd.java | 4 +-- .../com/arialyy/aria/core/command/AddCmd.java | 6 ++-- .../arialyy/aria/core/command/CancelCmd.java | 6 ++-- .../aria/core/command/HighestPriorityCmd.java | 4 +-- .../aria/core/command/ResumeAllCmd.java | 2 +- .../arialyy/aria/core/command/StartCmd.java | 7 ++--- .../arialyy/aria/core/command/StopCmd.java | 6 ++-- .../aria/core/download/DownloadUtil.java | 2 +- .../aria/core/download/SingleThreadTask.java | 26 +++++++++++----- .../arialyy/aria/core/inf/AbsTaskEntity.java | 2 +- .../com/arialyy/aria/core/inf/IEntity.java | 2 +- .../java/com/arialyy/aria/core/inf/ITask.java | 4 +-- .../arialyy/aria/core/queue/AbsTaskQueue.java | 8 ++--- .../aria/core/queue/DownloadTaskQueue.java | 9 ++++-- .../arialyy/aria/core/queue/ITaskQueue.java | 30 +++++++++---------- .../aria/core/queue/pool/CachePool.java | 2 +- .../aria/core/queue/pool/ExecutePool.java | 12 ++++---- .../arialyy/aria/core/queue/pool/IPool.java | 8 ++--- .../aria/core/scheduler/AbsSchedulers.java | 2 +- .../arialyy/simple/base/BaseApplication.java | 9 ++++++ .../simple/download/SingleTaskActivity.java | 3 +- 21 files changed, 88 insertions(+), 66 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java index a90d006e..8dc620c8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java @@ -32,7 +32,7 @@ import com.arialyy.aria.util.CommonUtil; */ public abstract class AbsCmd implements ICmd { ITaskQueue mQueue; - T mEntity; + T mTaskEntity; String TAG; String mTargetName; /** @@ -52,7 +52,7 @@ public abstract class AbsCmd implements ICmd { canExeCmd = CheckUtil.checkCmdEntity(entity, !(this instanceof CancelCmd) || !(this instanceof StopCmd)); mTargetName = targetName; - mEntity = entity; + mTaskEntity = entity; TAG = CommonUtil.getClassName(this); if (entity instanceof DownloadTaskEntity) { mQueue = DownloadTaskQueue.getInstance(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java index fe6d481b..4177dc1d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java @@ -33,10 +33,10 @@ class AddCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mEntity.getEntity()); + ITask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { - mEntity.getEntity().setState(IEntity.STATE_WAIT); - mQueue.createTask(mTargetName, mEntity); + mTaskEntity.getEntity().setState(IEntity.STATE_WAIT); + mQueue.createTask(mTargetName, mTaskEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java index ad2aecd8..fd51ad14 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java @@ -31,15 +31,15 @@ class CancelCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mEntity.getEntity()); + ITask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { - task = mQueue.createTask(mTargetName, mEntity); + task = mQueue.createTask(mTargetName, mTaskEntity); } if (task != null) { if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } - mQueue.cancelTask(task); + mQueue.removeTask(task); } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java index 74999611..57dc35bb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java @@ -39,9 +39,9 @@ final class HighestPriorityCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mEntity.getEntity()); + ITask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { - task = mQueue.createTask(mTargetName, mEntity); + task = mQueue.createTask(mTargetName, mTaskEntity); } if (task != null) { if (!TextUtils.isEmpty(mTargetName)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java index c7110077..cb87dee7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java @@ -27,7 +27,7 @@ final class ResumeAllCmd extends AbsCmd { List allEntity = DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + ""); for (DownloadEntity entity : allEntity) { - int exeNum = mQueue.getExeTaskNum(); + int exeNum = mQueue.getExePoolSize(); if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { ITask task = createTask(entity); mQueue.startTask(task); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java index 284a5df8..a9a3d180 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.command; import android.text.TextUtils; -import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.QueueMod; import com.arialyy.aria.core.inf.ITask; @@ -35,9 +34,9 @@ class StartCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mEntity.getEntity()); + ITask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { - task = mQueue.createTask(mTargetName, mEntity); + task = mQueue.createTask(mTargetName, mTaskEntity); } if (task != null) { if (!TextUtils.isEmpty(mTargetName)) { @@ -56,7 +55,7 @@ class StartCmd extends AbsCmd { if (mod.equals(QueueMod.NOW.getTag())) { mQueue.startTask(task); }else if (mod.equals(QueueMod.WAIT.getTag())){ - if (mQueue.getExeTaskNum() < maxTaskNum){ + if (mQueue.getExePoolSize() < maxTaskNum){ mQueue.startTask(task); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java index 1e1fb295..8712d6cc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java @@ -34,10 +34,10 @@ class StopCmd extends AbsCmd { @Override public void executeCmd() { if (!canExeCmd) return; - ITask task = mQueue.getTask(mEntity.getEntity()); + ITask task = mQueue.getTask(mTaskEntity.getEntity()); if (task == null) { - if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) { - task = mQueue.createTask(mTargetName, mEntity); + if (mTaskEntity.getEntity().getState() == IEntity.STATE_RUNNING) { + task = mQueue.createTask(mTargetName, mTaskEntity); mQueue.stopTask(task); } else { Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java index 183954e8..d82975b1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java @@ -21,7 +21,6 @@ import android.util.Log; import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.util.BufferedRandomAccessFile; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.io.IOException; @@ -371,6 +370,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { new BufferedRandomAccessFile(new File(mDownloadFile.getPath()), "rwd", 8192); //设置文件长度 file.setLength(fileLength); + file.close(); mListener.onPostPre(fileLength); //分配每条线程的下载区间 pro = CommonUtil.loadConfig(mConfigFile); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java index b6233744..5d3cd840 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java @@ -76,8 +76,9 @@ final class SingleThreadTask implements Runnable { } @Override public void run() { - HttpURLConnection conn; - InputStream is; + HttpURLConnection conn = null; + InputStream is = null; + BufferedRandomAccessFile file = null; try { URL url = new URL(mConfigEntity.DOWNLOAD_URL); conn = ConnectionHelp.handleConnection(url); @@ -102,8 +103,7 @@ final class SingleThreadTask implements Runnable { conn.setReadTimeout(CONSTANCE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数 is = conn.getInputStream(); //创建可设置位置的文件 - BufferedRandomAccessFile file = - new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize); + file = new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize); //设置每条线程写入文件的位置 file.seek(mConfigEntity.START_LOCATION); byte[] buffer = new byte[mBufSize]; @@ -121,10 +121,6 @@ final class SingleThreadTask implements Runnable { file.write(buffer, 0, len); progress(len); } - file.close(); - //close 为阻塞的,需要使用线程池来处理 - is.close(); - conn.disconnect(); if (CONSTANCE.isCancel) { return; } @@ -161,6 +157,20 @@ final class SingleThreadTask implements Runnable { failDownload(mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】", e); } catch (Exception e) { failDownload(mChildCurrentLocation, "获取流失败", e); + } finally { + try { + if (file != null) { + file.close(); + } + if (is != null) { + is.close(); + } + if (conn != null) { + conn.disconnect(); + } + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index eefa1f3a..4f82ad27 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -35,7 +35,7 @@ public abstract class AbsTaskEntity { public RequestEnum requestEnum = RequestEnum.GET; /** - * 重定向后,新url的key + * 重定向后,从链接中获取新url所需要的key */ public String redirectUrlKey = "location"; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java index 6b60f1bb..fe529b49 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java @@ -39,7 +39,7 @@ public interface IEntity { */ @Ignore public static final int STATE_STOP = 2; /** - * 未开始状态 + * 等待状态 */ @Ignore public static final int STATE_WAIT = 3; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java index c8b4efa2..27b4af9b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java @@ -44,14 +44,14 @@ public interface ITask { public String getKey(); /** - * 是否真正执行 + * 任务是否正在执行 * * @return true,正在执行; */ public boolean isRunning(); /** - * 获取工具实体 + * 获取信息实体 */ public ENTITY getEntity(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index eb133913..67f55038 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -78,16 +78,16 @@ abstract class AbsTaskQueue tasks = mExecutePool.getAllTask(); Set keys = tasks.keySet(); - for (String key : keys){ + for (String key : keys) { DownloadTask task = tasks.get(key); task.setMaxSpeed(maxSpeed); } @@ -116,7 +119,7 @@ public class DownloadTaskQueue } } } - mExecutePool.setDownloadNum(downloadNum); + mExecutePool.setMaxNum(downloadNum); if (diff >= 1) { for (int i = 0; i < diff; i++) { DownloadTask nextTask = getNextTask(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index 538fc919..f6fd93d5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -67,11 +67,18 @@ public interface ITaskQueue implements IPool { } /** - * 获取所有正在执行的任务 + * 获取被缓存的任务 */ public Map getAllTask() { return mCacheMap; diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 4f9691af..6892d208 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -83,19 +83,19 @@ public class ExecutePool implements IPool { } /** - * 设置执行任务数 + * 设置执行队列最大任务数 * - * @param downloadNum 下载数 + * @param maxNum 下载数 */ - public void setDownloadNum(int downloadNum) { + public void setMaxNum(int maxNum) { try { - ArrayBlockingQueue temp = new ArrayBlockingQueue<>(downloadNum); + ArrayBlockingQueue temp = new ArrayBlockingQueue<>(maxNum); TASK task; while ((task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS)) != null) { temp.offer(task); } mExecuteQueue = temp; - mSize = downloadNum; + mSize = maxNum; } catch (InterruptedException e) { e.printStackTrace(); } @@ -104,7 +104,7 @@ public class ExecutePool implements IPool { /** * 添加新任务 * - * @param newTask 新下载任务 + * @param newTask 新任务 */ private boolean putNewTask(TASK newTask) { String url = newTask.getKey(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java index 53378e9c..970c22b9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java @@ -52,17 +52,17 @@ public interface IPool { public boolean removeTask(T task); /** - * 通过下载链接移除下载任务 + * 通过key除下载任务 * - * @param downloadUrl 下载链接 + * @param key 下载链接 * @return true:移除成功 */ - public boolean removeTask(String downloadUrl); + public boolean removeTask(String key); /** * 池子大小 * - * @return 返回缓存池或者当前任务池大小 + * @return 返回缓存池或者执行池大小 */ public int size(); } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index f53bf4e7..4aa14f66 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -140,7 +140,7 @@ public abstract class AbsSchedulers= Build.VERSION_CODES.GINGERBREAD) { + StrictMode.setThreadPolicy( + new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); + StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); + } } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index e96fd2c2..96e8e48c 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -62,7 +62,8 @@ public class SingleTaskActivity extends BaseActivity { private static final String DOWNLOAD_URL = //"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; - "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + "http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; //"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; //"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt";