|
|
@ -109,6 +109,28 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
startFlow(); |
|
|
|
startFlow(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 重置任务状态 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void resetState() { |
|
|
|
|
|
|
|
closeTimer(); |
|
|
|
|
|
|
|
mCompleteThreadNum = 0; |
|
|
|
|
|
|
|
mTotalThreadNum = 0; |
|
|
|
|
|
|
|
mStartThreadNum = 0; |
|
|
|
|
|
|
|
if (mFixedThreadPool != null) { |
|
|
|
|
|
|
|
mFixedThreadPool.shutdown(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (mTask != null && !mTask.isEmpty()) { |
|
|
|
|
|
|
|
for (int i = 0; i < mTask.size(); i++) { |
|
|
|
|
|
|
|
AbsThreadTask task = mTask.get(i); |
|
|
|
|
|
|
|
if (task != null && !task.isRunning()) { |
|
|
|
|
|
|
|
task.breakTask(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mTask.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 开始流程 |
|
|
|
* 开始流程 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -116,8 +138,10 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
if (isBreak()) { |
|
|
|
if (isBreak()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
resetState(); |
|
|
|
mConstance.resetState(); |
|
|
|
mConstance.resetState(); |
|
|
|
checkRecord(); |
|
|
|
checkRecord(); |
|
|
|
|
|
|
|
mConstance.isRunning = true; |
|
|
|
mConstance.TASK_RECORD = mRecord; |
|
|
|
mConstance.TASK_RECORD = mRecord; |
|
|
|
if (mListener instanceof IDownloadListener) { |
|
|
|
if (mListener instanceof IDownloadListener) { |
|
|
|
((IDownloadListener) mListener).onPostPre(mEntity.getFileSize()); |
|
|
|
((IDownloadListener) mListener).onPostPre(mEntity.getFileSize()); |
|
|
@ -281,13 +305,75 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
if (mRecord.threadRecords == null || mRecord.threadRecords.isEmpty()) { |
|
|
|
if (mRecord.threadRecords == null || mRecord.threadRecords.isEmpty()) { |
|
|
|
initRecord(true); |
|
|
|
initRecord(true); |
|
|
|
} else if (mRecord.isBlock) { |
|
|
|
} else if (mRecord.isBlock) { |
|
|
|
|
|
|
|
handleBlockRecord(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
handleNormalRecord(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 处理普通任务的记录 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void handleNormalRecord() { |
|
|
|
|
|
|
|
File file = new File(mRecord.filePath); |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); |
|
|
|
|
|
|
|
mRecord.deleteData(); |
|
|
|
|
|
|
|
initRecord(true); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mRecord.isOpenDynamicFile) { |
|
|
|
|
|
|
|
ThreadRecord tr = mRecord.threadRecords.get(0); |
|
|
|
|
|
|
|
if (tr == null) { |
|
|
|
|
|
|
|
mTaskEntity.setNewTask(true); |
|
|
|
|
|
|
|
mTotalThreadNum = 1; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (file.length() > mEntity.getFileSize()) { |
|
|
|
|
|
|
|
ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath())); |
|
|
|
|
|
|
|
file.delete(); |
|
|
|
|
|
|
|
tr.startLocation = 0; |
|
|
|
|
|
|
|
tr.isComplete = false; |
|
|
|
|
|
|
|
tr.endLocation = mEntity.getFileSize(); |
|
|
|
|
|
|
|
mStartThreadNum++; |
|
|
|
|
|
|
|
} else if (file.length() == mEntity.getFileSize()) { |
|
|
|
|
|
|
|
tr.isComplete = true; |
|
|
|
|
|
|
|
mCompleteThreadNum++; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (file.length() != tr.startLocation) { |
|
|
|
|
|
|
|
ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); |
|
|
|
|
|
|
|
tr.startLocation = file.length(); |
|
|
|
|
|
|
|
tr.isComplete = false; |
|
|
|
|
|
|
|
mStartThreadNum++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
for (ThreadRecord tr : mRecord.threadRecords) { |
|
|
|
|
|
|
|
if (tr.isComplete) { |
|
|
|
|
|
|
|
mCompleteThreadNum++; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mStartThreadNum++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mTotalThreadNum = mRecord.threadRecords.size(); |
|
|
|
|
|
|
|
mTaskEntity.setNewTask(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 处理分块任务的记录 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void handleBlockRecord() { |
|
|
|
final int threadNum = mRecord.threadRecords.size(); |
|
|
|
final int threadNum = mRecord.threadRecords.size(); |
|
|
|
final long blockLen = mEntity.getFileSize() / threadNum; |
|
|
|
final long blockLen = mEntity.getFileSize() / threadNum; |
|
|
|
int i = 0; |
|
|
|
int i = 0; |
|
|
|
for (ThreadRecord tr : mRecord.threadRecords) { |
|
|
|
for (ThreadRecord tr : mRecord.threadRecords) { |
|
|
|
File temp = new File(String.format(SUB_PATH, mRecord.filePath, tr.threadId)); |
|
|
|
File temp = new File(String.format(SUB_PATH, mRecord.filePath, tr.threadId)); |
|
|
|
if (!temp.exists()) { |
|
|
|
if (!temp.exists()) { |
|
|
|
ALog.w(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); |
|
|
|
ALog.i(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); |
|
|
|
tr.isComplete = false; |
|
|
|
tr.isComplete = false; |
|
|
|
tr.startLocation = -1; |
|
|
|
tr.startLocation = -1; |
|
|
|
mStartThreadNum++; |
|
|
|
mStartThreadNum++; |
|
|
@ -296,7 +382,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
mCompleteThreadNum++; |
|
|
|
mCompleteThreadNum++; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
long realLocation = i * blockLen + temp.length(); |
|
|
|
long realLocation = i * blockLen + temp.length(); |
|
|
|
ALog.w(TAG, String.format( |
|
|
|
ALog.i(TAG, String.format( |
|
|
|
"startLocation = %s; endLocation = %s; block = %s; tempLen = %s; i = %s", |
|
|
|
"startLocation = %s; endLocation = %s; block = %s; tempLen = %s; i = %s", |
|
|
|
tr.startLocation, tr.endLocation, blockLen, temp.length(), i)); |
|
|
|
tr.startLocation, tr.endLocation, blockLen, temp.length(), i)); |
|
|
|
if (tr.endLocation == realLocation) { |
|
|
|
if (tr.endLocation == realLocation) { |
|
|
@ -307,10 +393,10 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
tr.isComplete = false; |
|
|
|
tr.isComplete = false; |
|
|
|
if (realLocation < tr.startLocation) { |
|
|
|
if (realLocation < tr.startLocation) { |
|
|
|
ALog.w(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); |
|
|
|
ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); |
|
|
|
tr.startLocation = realLocation; |
|
|
|
tr.startLocation = realLocation; |
|
|
|
} else if (realLocation > tr.endLocation) { |
|
|
|
} else if (realLocation > tr.endLocation) { |
|
|
|
ALog.w(TAG, String.format("分块【%s】错误,将重新开始该分块", temp.getPath())); |
|
|
|
ALog.i(TAG, String.format("分块【%s】错误,将重新开始该分块", temp.getPath())); |
|
|
|
temp.delete(); |
|
|
|
temp.delete(); |
|
|
|
tr.startLocation = i * blockLen; |
|
|
|
tr.startLocation = i * blockLen; |
|
|
|
} |
|
|
|
} |
|
|
@ -322,26 +408,6 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
} |
|
|
|
} |
|
|
|
mTotalThreadNum = mRecord.threadRecords.size(); |
|
|
|
mTotalThreadNum = mRecord.threadRecords.size(); |
|
|
|
mTaskEntity.setNewTask(false); |
|
|
|
mTaskEntity.setNewTask(false); |
|
|
|
} else { |
|
|
|
|
|
|
|
File file = new File(mRecord.filePath); |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); |
|
|
|
|
|
|
|
mRecord.deleteData(); |
|
|
|
|
|
|
|
initRecord(true); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (ThreadRecord tr : mRecord.threadRecords) { |
|
|
|
|
|
|
|
if (tr.isComplete) { |
|
|
|
|
|
|
|
mCompleteThreadNum++; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mStartThreadNum++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mTotalThreadNum = mRecord.threadRecords.size(); |
|
|
|
|
|
|
|
mTaskEntity.setNewTask(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -463,7 +529,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
private AbsThreadTask createSingThreadTask(int i, long startL, long endL, long fileLength, |
|
|
|
private AbsThreadTask createSingThreadTask(int i, long startL, long endL, long fileLength, |
|
|
|
ThreadRecord record) { |
|
|
|
ThreadRecord record) { |
|
|
|
SubThreadConfig<TASK_ENTITY> config = new SubThreadConfig<>(); |
|
|
|
SubThreadConfig<TASK_ENTITY> config = new SubThreadConfig<>(); |
|
|
|
config.FILE_SIZE = fileLength; |
|
|
|
config.TOTAL_FILE_SIZE = fileLength; |
|
|
|
config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); |
|
|
|
config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); |
|
|
|
config.TEMP_FILE = |
|
|
|
config.TEMP_FILE = |
|
|
|
mRecord.isBlock ? new File(String.format(SUB_PATH, mTempFile.getPath(), i)) : mTempFile; |
|
|
|
mRecord.isBlock ? new File(String.format(SUB_PATH, mTempFile.getPath(), i)) : mTempFile; |
|
|
@ -566,34 +632,36 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 重试线程任务,只有线程创建成功才能重试 |
|
|
|
* 重试任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void retryThreadTask() { |
|
|
|
public void retryTask() { |
|
|
|
if (isBreak()) { |
|
|
|
ALog.w(TAG, String.format("任务【%s】开始重试", mEntity.getFileName())); |
|
|
|
return; |
|
|
|
startFlow(); |
|
|
|
} |
|
|
|
//if (isBreak()) {
|
|
|
|
|
|
|
|
// return;
|
|
|
|
if (mTask == null || mTask.size() == 0) { |
|
|
|
//}
|
|
|
|
ALog.w(TAG, "没有线程任务"); |
|
|
|
//if (mTask == null || mTask.size() == 0) {
|
|
|
|
return; |
|
|
|
// ALog.w(TAG, "没有线程任务");
|
|
|
|
} |
|
|
|
// return;
|
|
|
|
Set<Integer> keys = mTask.keySet(); |
|
|
|
//}
|
|
|
|
for (Integer key : keys) { |
|
|
|
//Set<Integer> keys = mTask.keySet();
|
|
|
|
AbsThreadTask task = mTask.get(key); |
|
|
|
//for (Integer key : keys) {
|
|
|
|
if (task != null && !task.isThreadComplete()) { |
|
|
|
// AbsThreadTask task = mTask.get(key);
|
|
|
|
task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation; |
|
|
|
// if (task != null && !task.isThreadComplete()) {
|
|
|
|
mConstance.resetState(); |
|
|
|
//
|
|
|
|
startTimer(); |
|
|
|
// task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation;
|
|
|
|
ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(), |
|
|
|
// mConstance.resetState();
|
|
|
|
key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION)); |
|
|
|
// startTimer();
|
|
|
|
if (!mFixedThreadPool.isShutdown()) { |
|
|
|
// ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(),
|
|
|
|
mFixedThreadPool.execute(task); |
|
|
|
// key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION));
|
|
|
|
} else { |
|
|
|
// if (!mFixedThreadPool.isShutdown()) {
|
|
|
|
ALog.w(TAG, "线程池已关闭"); |
|
|
|
// mFixedThreadPool.execute(task);
|
|
|
|
mListener.onFail(true); |
|
|
|
// } else {
|
|
|
|
} |
|
|
|
// ALog.w(TAG, "线程池已关闭");
|
|
|
|
} |
|
|
|
// mListener.onFail(true);
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -608,17 +676,17 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handleNoSupportBP() { |
|
|
|
private void handleNoSupportBP() { |
|
|
|
SubThreadConfig<TASK_ENTITY> config = new SubThreadConfig<>(); |
|
|
|
SubThreadConfig<TASK_ENTITY> config = new SubThreadConfig<>(); |
|
|
|
config.FILE_SIZE = mEntity.getFileSize(); |
|
|
|
config.TOTAL_FILE_SIZE = mEntity.getFileSize(); |
|
|
|
config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); |
|
|
|
config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); |
|
|
|
config.TEMP_FILE = mTempFile; |
|
|
|
config.TEMP_FILE = mTempFile; |
|
|
|
config.THREAD_ID = 0; |
|
|
|
config.THREAD_ID = 0; |
|
|
|
config.START_LOCATION = 0; |
|
|
|
config.START_LOCATION = 0; |
|
|
|
config.END_LOCATION = config.FILE_SIZE; |
|
|
|
config.END_LOCATION = config.TOTAL_FILE_SIZE; |
|
|
|
config.SUPPORT_BP = mTaskEntity.isSupportBP(); |
|
|
|
config.SUPPORT_BP = mTaskEntity.isSupportBP(); |
|
|
|
config.TASK_ENTITY = mTaskEntity; |
|
|
|
config.TASK_ENTITY = mTaskEntity; |
|
|
|
ThreadRecord record = new ThreadRecord(); |
|
|
|
ThreadRecord record = new ThreadRecord(); |
|
|
|
record.startLocation = 0; |
|
|
|
record.startLocation = 0; |
|
|
|
record.endLocation = config.FILE_SIZE; |
|
|
|
record.endLocation = config.TOTAL_FILE_SIZE; |
|
|
|
record.key = mTempFile.getPath(); |
|
|
|
record.key = mTempFile.getPath(); |
|
|
|
config.THREAD_RECORD = record; |
|
|
|
config.THREAD_RECORD = record; |
|
|
|
AbsThreadTask task = selectThreadTask(config); |
|
|
|
AbsThreadTask task = selectThreadTask(config); |
|
|
|