From fa08263805afa2827254a8d6cc8a8442448f4301 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Tue, 15 May 2018 20:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8B=E8=BD=BD=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/ConfigHelper.java | 11 +++++++++- .../arialyy/aria/core/common/AbsFileer.java | 4 +++- .../arialyy/aria/core/common/TaskRecord.java | 6 ++++++ .../core/download/downloader/Downloader.java | 12 +++++++---- .../download/downloader/HttpThreadTask.java | 12 ++++++++++- app/src/main/assets/aria_config.xml | 6 ++++++ .../arialyy/simple/test/AnyRunActivity.java | 4 ++-- .../com/arialyy/aria/core/Configuration.java | 20 +++++++++++++++++++ 8 files changed, 66 insertions(+), 9 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 7f1a3d1a..233c94e4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -59,6 +59,9 @@ class ConfigHelper extends DefaultHandler { String value = attributes.getValue("value"); switch (qName) { + case "useVirtualFile": + loadUseVirtualFile(value); + break; case "threadNum": loadThreadNum(value); break; @@ -114,6 +117,12 @@ class ConfigHelper extends DefaultHandler { } } + private void loadUseVirtualFile(String value) { + if (isDownloadConfig) { + mDownloadConfig.useVirtualFile = checkBoolean(value) ? Boolean.valueOf(value) : false; + } + } + private void loadNotNetRetry(String value) { if (isDownloadConfig) { mDownloadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false; @@ -223,7 +232,7 @@ class ConfigHelper extends DefaultHandler { mDownloadConfig.buffSize = buffSize; } - if (isUploadConfig){ + if (isUploadConfig) { mUploadConfig.buffSize = buffSize; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index e23a133a..3651e096 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -73,7 +73,7 @@ public abstract class AbsFileer(); mRecord.isGroupRecord = mTaskEntity.getEntity().isGroupChild(); + mRecord.isUseVirtualFile = + AriaManager.getInstance(AriaManager.APP).getDownloadConfig().isUseVirtualFile(); if (mRecord.isGroupRecord) { if (mTaskEntity.getEntity() instanceof DownloadEntity) { mRecord.dGroupName = ((DownloadEntity) mTaskEntity.getEntity()).getGroupName(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java index ea928de3..597388b6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java @@ -73,4 +73,10 @@ public class TaskRecord extends DbEntity { @Ignore @Deprecated public String uGroupName; + + /** + * 是否是使用虚拟文件下载的 + * {@code true}是,{@code false}不是 + */ + public boolean isUseVirtualFile = false; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 3eb63678..4c9a2ec1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -40,13 +40,17 @@ class Downloader extends AbsFileer { Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) { super(listener, taskEntity); mTempFile = new File(mEntity.getDownloadPath()); - setUpdateInterval( - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getUpdateInterval()); + AriaManager manager = AriaManager.getInstance(AriaManager.APP); + setUpdateInterval(manager.getDownloadConfig().getUpdateInterval()); } @Override protected int setNewTaskThreadNum() { return - mEntity.getFileSize() <= SUB_LEN || mTaskEntity.getRequestType() == AbsTaskEntity.D_FTP_DIR + // 小于1m的文件或是任务组的子任务、使用虚拟文件,线程数都是1 + mEntity.getFileSize() <= SUB_LEN + || mTaskEntity.getRequestType() == AbsTaskEntity.D_FTP_DIR + || mTaskEntity.getRequestType() == AbsTaskEntity.DG_HTTP + || mRecord.isUseVirtualFile ? 1 : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); } @@ -57,7 +61,7 @@ class Downloader extends AbsFileer { try { file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); //设置文件长度 - file.setLength(mEntity.getFileSize()); + file.setLength(mRecord.isUseVirtualFile ? 1 : mEntity.getFileSize()); return true; } catch (IOException e) { failDownload("下载失败【downloadUrl:" diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index ce2366f5..7b5ca5d0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -28,7 +28,6 @@ import com.arialyy.aria.util.CommonUtil; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -39,6 +38,11 @@ import java.net.URL; */ final class HttpThreadTask extends AbsThreadTask { private final String TAG = "HttpThreadTask"; + /** + * 2M的动态长度 + */ + private final int LEN_INTERVAL = 1024 * 1024 * 2; + private boolean useVirtualFile = false; HttpThreadTask(StateConstance constance, IDownloadListener listener, SubThreadConfig downloadInfo) { @@ -48,6 +52,7 @@ final class HttpThreadTask extends AbsThreadTask 0) { Thread.sleep(mSleepTime); } + if (useVirtualFile) { + file.setLength( + STATE.CURRENT_LOCATION + LEN_INTERVAL < mEntity.getFileSize() ? STATE.CURRENT_LOCATION + + LEN_INTERVAL : mEntity.getFileSize()); + } file.write(buffer, 0, len); progress(len); } diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 2d469d67..bc0e1feb 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -11,6 +11,12 @@ + + + diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index 311f64e6..d6d3e104 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -18,8 +18,8 @@ public class AnyRunActivity extends BaseActivity { AnyRunnModule module; String[] urls; int index = 0; - //String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; - String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; + String URL = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; + //String URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //String URL = "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @Override protected int setLayoutId() { diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 56c7eebd..0500322f 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -374,6 +374,26 @@ class Configuration { */ int maxSpeed = 0; + /** + * 是否使用虚拟文件,使用虚拟文件初始化时将不占用磁盘空间,下载多少byte,占多少空间,效果见chrome的下载。 + * 注意: + * 1、使用该功能,将自动关闭多线程下载; + * 2、对于已经采用了多线程的任务,依然采用原来的下载方式; + * 3、原本参数是true,任务没下载完成,就参数改为false,那么没下载完成的任务还是会按照参数修改前的方式下载,只有新任务才会根据参数调用不同的下载方式 + * {@code true}使用 + */ + boolean useVirtualFile = true; + + public DownloadConfig setuseVirtualFile(boolean useVirtualFile) { + this.useVirtualFile = useVirtualFile; + saveKey("useVirtualFile", String.valueOf(useVirtualFile)); + return this; + } + + public boolean isUseVirtualFile() { + return useVirtualFile; + } + public DownloadConfig setMaxTaskNum(int maxTaskNum) { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum;