添加下载动态增加文件长度的功能

pull/330/head
laoyuyu 7 years ago
parent c7fb5f2633
commit fa08263805
  1. 9
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  2. 4
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  3. 6
      Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java
  4. 12
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  6. 6
      app/src/main/assets/aria_config.xml
  7. 4
      app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java
  8. 20
      aria/src/main/java/com/arialyy/aria/core/Configuration.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;

@ -73,7 +73,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 进度刷新间隔
*/
private long mUpdateInterval = 1000;
private TaskRecord mRecord;
protected TaskRecord mRecord;
protected AbsFileer(IEventListener listener, TASK_ENTITY taskEntity) {
mListener = listener;
@ -336,6 +336,8 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
mRecord.filePath = mTaskEntity.getKey();
mRecord.threadRecords = new ArrayList<>();
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();

@ -73,4 +73,10 @@ public class TaskRecord extends DbEntity {
@Ignore
@Deprecated
public String uGroupName;
/**
* 是否是使用虚拟文件下载的
* {@code true}{@code false}不是
*/
public boolean isUseVirtualFile = false;
}

@ -40,13 +40,17 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
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<DownloadEntity, DownloadTaskEntity> {
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:"

@ -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<DownloadEntity, DownloadTaskEntity> {
private final String TAG = "HttpThreadTask";
/**
* 2M的动态长度
*/
private final int LEN_INTERVAL = 1024 * 1024 * 2;
private boolean useVirtualFile = false;
HttpThreadTask(StateConstance constance, IDownloadListener listener,
SubThreadConfig<DownloadTaskEntity> downloadInfo) {
@ -48,6 +52,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
mReadTimeOut = manager.getDownloadConfig().getIOTimeOut();
mBufSize = manager.getDownloadConfig().getBuffSize();
isNotNetRetry = manager.getDownloadConfig().isNotNetRetry();
useVirtualFile = STATE.TASK_RECORD.isUseVirtualFile;
setMaxSpeed(manager.getDownloadConfig().getMaxSpeed());
}
@ -143,6 +148,11 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
if (mSleepTime > 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);
}

@ -11,6 +11,12 @@
<!--注意,修改该配置文件中的属性会覆盖代码中所设置的属性-->
<download>
<!--是否使用虚拟文件,使用虚拟文件初始化时将不占用磁盘空间,下载多少byte,占多少空间,效果见chrome的下载。-->
<!--注意:
1、使用该功能,将自动关闭多线程下载;
2、对于已经采用了多线程的任务,依然采用原来的下载方式;
3、原本参数是true,任务没下载完成,就参数改为false,那么没下载完成的任务还是会按照参数修改前的方式下载;只有新任务才会根据参数调用不同的下载方式。-->
<useVirtualFile value="true"/>
<!--断网的时候是否重试,true:断网也重试;false:断网不重试,直接走失败的回调-->
<notNetRetry value="true"/>

@ -18,8 +18,8 @@ public class AnyRunActivity extends BaseActivity<ActivityTestBinding> {
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() {

@ -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;

Loading…
Cancel
Save