优化ttbuilder

pull/617/head
laoyuyu 6 years ago
parent 36c226702d
commit 6f53235807
  1. 50
      Aria/src/main/java/com/arialyy/aria/core/common/FtpOption.java
  2. 4
      Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java
  3. 2
      DEV_LOG.md
  4. 4
      FtpComponent/src/main/java/com/arialyy/aria/ftp/AbsFtpInfoTask.java
  5. 4
      FtpComponent/src/main/java/com/arialyy/aria/ftp/BaseFtpThreadTaskAdapter.java
  6. 95
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoader.java
  7. 3
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java
  8. 37
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDRecordHandler.java
  9. 13
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDTTBuilderAdapter.java
  10. 3
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java
  11. 15
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java
  12. 41
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUTTBuilder.java
  13. 15
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java
  14. 10
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDTTBuilderAdapter.java
  15. 3
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java
  16. 15
      HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java
  17. 41
      HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUTTBuilder.java
  18. 15
      PublicComponent/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java
  19. 47
      PublicComponent/src/main/java/com/arialyy/aria/core/IdEntity.java
  20. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java
  21. 90
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalTTBuilderAdapter.java
  22. 53
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/NormalTTBuilder.java
  23. 17
      PublicComponent/src/main/java/com/arialyy/aria/exception/AriaLoginException.java
  24. 2
      PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java
  25. 124
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/AbsSFtpInfoTask.java
  26. 100
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/SFtpInfoThread.java
  27. 98
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/SFtpSessionManager.java
  28. 25
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/SFtpSubThreadCOnfig.java
  29. 49
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDInfoTask.java
  30. 163
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoader.java
  31. 40
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderAdapter.java
  32. 129
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java
  33. 72
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDTTBuilderAdapter.java
  34. 2
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDThreadTaskAdapter.java
  35. 2
      app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java
  36. 2
      app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java

@ -17,6 +17,7 @@ package com.arialyy.aria.core.common;
import android.text.TextUtils;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.IdEntity;
import com.arialyy.aria.core.ProtocolType;
import com.arialyy.aria.core.processor.IFtpUploadInterceptor;
import com.arialyy.aria.util.ALog;
@ -36,7 +37,7 @@ public class FtpOption extends BaseOption {
private String charSet, userName, password, account;
private boolean isNeedLogin = false;
private FtpUrlEntity urlEntity;
private String protocol, keyAlias, storePass, storePath;
private String protocol;
private boolean isImplicit = true;
private IFtpUploadInterceptor uploadInterceptor;
private int connMode = FtpConnectionMode.DATA_CONNECTION_MODE_PASV;
@ -51,6 +52,7 @@ public class FtpOption extends BaseOption {
private String serverTimeZoneId = null;
private String systemKey = FTPServerIdentifier.SYST_UNIX;
//---------------- ftp client 配置信息 end
private IdEntity idEntity = new IdEntity();
public FtpOption() {
super();
@ -98,7 +100,7 @@ public class FtpOption extends BaseOption {
}
/**
* 设置证书别名
* 设置私钥证书别名
*
* @param keyAlias 别名
*/
@ -107,12 +109,12 @@ public class FtpOption extends BaseOption {
ALog.e(TAG, "设置证书别名失败,证书别名为空");
return this;
}
this.keyAlias = keyAlias;
idEntity.keyAlias = keyAlias;
return this;
}
/**
* 设置证书密码
* 设置私钥证书密码
*
* @param storePass 私钥密码
*/
@ -121,12 +123,12 @@ public class FtpOption extends BaseOption {
ALog.e(TAG, "设置证书密码失败,证书密码为空");
return this;
}
this.storePass = storePass;
idEntity.prvPass = storePass;
return this;
}
/**
* 设置证书路径
* 设置私钥证书路径
*
* @param storePath 证书路径
*/
@ -135,7 +137,35 @@ public class FtpOption extends BaseOption {
ALog.e(TAG, "设置证书路径失败,证书路径为空");
return this;
}
this.storePath = storePath;
idEntity.storePath = storePath;
return this;
}
/**
* 设置私钥证书
*
* @param prvKey 证书内容
*/
public FtpOption setPrvKey(String prvKey) {
if (TextUtils.isEmpty(prvKey)) {
ALog.e(TAG, "设置私钥证书失败,证书内容为空");
return this;
}
idEntity.prvKey = prvKey;
return this;
}
/**
* 设置公钥证书
*
* @param pubKey 公钥证书内容
*/
public FtpOption setPubKey(String pubKey) {
if (TextUtils.isEmpty(pubKey)) {
ALog.e(TAG, "设置公钥失败,证书内容为空");
return this;
}
idEntity.pubKey = pubKey;
return this;
}
@ -296,12 +326,10 @@ public class FtpOption extends BaseOption {
urlEntity.user = userName;
urlEntity.password = password;
urlEntity.account = account;
if (!TextUtils.isEmpty(storePath)) {
if (!TextUtils.isEmpty(idEntity.storePath) || !TextUtils.isEmpty(idEntity.prvKey)) {
urlEntity.isFtps = true;
urlEntity.protocol = protocol;
urlEntity.keyAlias = keyAlias;
urlEntity.storePass = storePass;
urlEntity.storePath = storePath;
urlEntity.idEntity = idEntity;
urlEntity.isImplicit = isImplicit;
}
}

@ -90,11 +90,11 @@ public class CheckFtpDirEntityUtil implements ICheckEntityUtil {
(FtpUrlEntity) mWrapper.getOptionParams().getParam(IOptionConstant.ftpUrlEntity);
assert urlEntity != null;
if (urlEntity.isFtps) {
if (TextUtils.isEmpty(urlEntity.storePath)) {
if (TextUtils.isEmpty(urlEntity.idEntity.storePath)) {
ALog.e(TAG, "证书路径为空");
return false;
}
if (TextUtils.isEmpty(urlEntity.keyAlias)) {
if (TextUtils.isEmpty(urlEntity.idEntity.keyAlias)) {
ALog.e(TAG, "证书别名为空");
return false;
}

@ -1,4 +1,6 @@
## 开发日志
+ v_3.8.4
- fix bug https://github.com/AriaLyy/Aria/issues/599
+ v_3.8.3 (2020/1/9)
- fix bug https://github.com/AriaLyy/Aria/issues/573
- android P适配 https://github.com/AriaLyy/Aria/issues/581

@ -226,8 +226,8 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
FTPClient temp;
if (urlEntity.isFtps) {
FTPSClient sClient;
SSLContext sslContext = SSLContextUtil.getSSLContext(urlEntity.keyAlias, urlEntity.storePath,
urlEntity.protocol);
SSLContext sslContext = SSLContextUtil.getSSLContext(
urlEntity.idEntity.keyAlias, urlEntity.idEntity.storePath, urlEntity.protocol);
if (sslContext == null) {
sClient = new FTPSClient(urlEntity.protocol, urlEntity.isImplicit);
} else {

@ -147,8 +147,8 @@ public abstract class BaseFtpThreadTaskAdapter extends AbsThreadTaskAdapter {
FTPClient temp;
if (urlEntity.isFtps) {
FTPSClient sClient;
SSLContext sslContext = SSLContextUtil.getSSLContext(urlEntity.keyAlias, urlEntity.storePath,
urlEntity.protocol);
SSLContext sslContext = SSLContextUtil.getSSLContext(
urlEntity.idEntity.keyAlias, urlEntity.idEntity.storePath, urlEntity.protocol);
if (sslContext == null) {
sClient = new FTPSClient(urlEntity.protocol, urlEntity.isImplicit);
} else {

@ -0,0 +1,95 @@
///*
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.arialyy.aria.ftp.download;
//
//import com.arialyy.aria.core.TaskRecord;
//import com.arialyy.aria.core.common.SubThreadConfig;
//import com.arialyy.aria.core.listener.IEventListener;
//import com.arialyy.aria.core.loader.NormalTTBuilder;
//import com.arialyy.aria.core.loader.IRecordHandler;
//import com.arialyy.aria.core.loader.IThreadTaskBuilder;
//import com.arialyy.aria.core.loader.NormalLoader;
//import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
//import com.arialyy.aria.util.ALog;
//import com.arialyy.aria.util.FileUtil;
//import java.io.File;
//import java.lang.reflect.InvocationHandler;
//import java.lang.reflect.Method;
//import java.lang.reflect.Proxy;
//
//final class FtpDLoader extends NormalLoader {
// FtpDLoader(AbsTaskWrapper wrapper, IEventListener listener) {
// super(wrapper, listener);
// }
//
// @Override public void addComponent(IThreadTaskBuilder builder) {
// mTTBuilder = (IThreadTaskBuilder) Proxy.newProxyInstance(getClass().getClassLoader(),
// NormalTTBuilder.class.getInterfaces(), new InvocationHandler() {
// NormalTTBuilder target = new NormalTTBuilder(mTaskWrapper);
// @Override public Object invoke(Object proxy, Method method, Object[] args)
// throws Exception {
// if (method.getDeclaringClass() == Object.class) {
// return method.invoke(this, args);
// }
//
// //if (method.isDefault()) {
// // Constructor<MethodHandles.Lookup>
// // constructor =
// // MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
// // constructor.setAccessible(true);
// // return constructor.newInstance(AbsNormalTTBuilder.class, -1 /* trusted */)
// // .unreflectSpecial(method, AbsNormalTTBuilder.class)
// // .bindTo(proxy)
// // .invokeWithArguments(args);
// //}
//
// String methodName = method.getName();
// switch (methodName) {
//
// case "handleNewTask":
// return handleNewTask((TaskRecord) args[0], (int) args[1]);
// case "getAdapter":
// return new FtpDThreadTaskAdapter((SubThreadConfig) args[0]);
// }
//
// return method.invoke(target, args);
// }
// });
// }
//
// /**
// * 处理新任务
// */
// private boolean handleNewTask(TaskRecord record, int totalThreadNum) {
// File temp = new File(getEntity().getFilePath());
// if (!record.isBlock) {
// if (temp.exists()) {
// FileUtil.deleteFile(temp);
// }
// //CommonUtil.createFile(mTempFile.getPath());
// } else {
// for (int i = 0; i < totalThreadNum; i++) {
// File blockFile =
// new File(String.format(IRecordHandler.SUB_PATH, temp.getPath(), i));
// if (blockFile.exists()) {
// ALog.d(TAG, String.format("分块【%s】已经存在,将删除该分块", i));
// FileUtil.deleteFile(blockFile);
// }
// }
// }
// return true;
// }
//}

@ -21,6 +21,7 @@ import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption;
@ -45,7 +46,7 @@ public final class FtpDLoaderUtil extends AbsNormalLoaderUtil {
structure.addComponent(new FtpDRecordHandler((DTaskWrapper) getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new FtpDFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new FtpDTTBuilder(getTaskWrapper()));
.addComponent(new NormalTTBuilder(getTaskWrapper(), new FtpDTTBuilderAdapter()));
structure.accept(getLoader());
return structure;
}

@ -21,9 +21,7 @@ import com.arialyy.aria.core.common.RecordHandler;
import com.arialyy.aria.core.common.RecordHelper;
import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.util.RecordUtil;
import java.util.ArrayList;
@ -31,9 +29,9 @@ import java.util.ArrayList;
* @Author lyy
* @Date 2019-09-19
*/
final class FtpDRecordHandler extends RecordHandler {
public final class FtpDRecordHandler extends RecordHandler {
FtpDRecordHandler(DTaskWrapper wrapper) {
public FtpDRecordHandler(DTaskWrapper wrapper) {
super(wrapper);
}
@ -76,35 +74,18 @@ final class FtpDRecordHandler extends RecordHandler {
record.filePath = getEntity().getFilePath();
record.threadRecords = new ArrayList<>();
record.threadNum = threadNum;
int requestType = getWrapper().getRequestType();
if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR) {
record.isBlock = Configuration.getInstance().downloadCfg.isUseBlock();
} else {
record.isBlock = false;
}
record.isBlock = Configuration.getInstance().downloadCfg.isUseBlock();
record.taskType = getWrapper().getEntity().getTaskType();
record.isGroupRecord = getEntity().isGroupChild();
if (record.isGroupRecord) {
if (getEntity() instanceof DownloadEntity) {
record.dGroupHash = ((DownloadEntity) getEntity()).getGroupHash();
}
}
record.isGroupRecord = false;
return record;
}
@Override public int initTaskThreadNum() {
int requestType = getWrapper().getRequestType();
if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR) {
int threadNum = Configuration.getInstance().downloadCfg.getThreadNum();
return getFileSize() <= IRecordHandler.SUB_LEN
|| getEntity().isGroupChild()
|| threadNum == 1
? 1
: threadNum;
} else {
return 1;
}
int threadNum = Configuration.getInstance().downloadCfg.getThreadNum();
return getFileSize() <= IRecordHandler.SUB_LEN
|| threadNum == 1
? 1
: threadNum;
}
}

@ -17,23 +17,14 @@ package com.arialyy.aria.ftp.download;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilder;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.FileUtil;
import java.io.File;
/**
* @Author lyy
* @Date 2019-09-19
*/
final class FtpDTTBuilder extends AbsNormalTTBuilder {
FtpDTTBuilder(AbsTaskWrapper wrapper) {
super(wrapper);
}
final class FtpDTTBuilderAdapter extends AbsNormalTTBuilderAdapter {
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new FtpDThreadTaskAdapter(config);

@ -20,6 +20,7 @@ import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.group.AbsSubDLoadUtil;
import com.arialyy.aria.core.group.SubRecordHandler;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.SubLoader;
/**
@ -48,7 +49,7 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil {
@Override protected LoaderStructure buildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new SubRecordHandler(getWrapper()))
.addComponent(new FtpDTTBuilder(getWrapper()))
.addComponent(new NormalTTBuilder(getWrapper(), new FtpDTTBuilderAdapter()))
.addComponent(new FtpDFileInfoTask(getWrapper()));
structure.accept(getLoader());
return structure;

@ -15,11 +15,16 @@
*/
package com.arialyy.aria.ftp.upload;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption;
@ -44,7 +49,15 @@ public final class FtpULoaderUtil extends AbsNormalLoaderUtil {
structure.addComponent(new FtpURecordHandler((UTaskWrapper) getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new FtpUFileInfoTask((UTaskWrapper) getTaskWrapper()))
.addComponent(new FtpUTTBuilder(getTaskWrapper()));
.addComponent(new NormalTTBuilder(getTaskWrapper(), new AbsNormalTTBuilderAdapter() {
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new FtpUThreadTaskAdapter(config);
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
return true;
}
}));
structure.accept(getLoader());
return structure;
}

@ -1,41 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.ftp.upload;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilder;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
/**
* @Author lyy
* @Date 2019-09-19
*/
final class FtpUTTBuilder extends AbsNormalTTBuilder {
FtpUTTBuilder(AbsTaskWrapper wrapper) {
super(wrapper);
}
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new FtpUThreadTaskAdapter(config);
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
return true;
}
}

@ -15,16 +15,27 @@
*/
package com.arialyy.aria.http.download;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpRecordHandler;
import com.arialyy.aria.http.HttpTaskOption;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.FileUtil;
import java.io.File;
import java.io.IOException;
/**
* @Author lyy
@ -45,8 +56,10 @@ public final class HttpDLoaderUtil extends AbsNormalLoaderUtil {
structure.addComponent(new HttpRecordHandler(getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new HttpDFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new HttpDTTBuilder(getTaskWrapper()));
.addComponent(new NormalTTBuilder(getTaskWrapper(), new HttpDTTBuilderAdapter()));
structure.accept(getLoader());
return structure;
}
}

@ -2,20 +2,16 @@ package com.arialyy.aria.http.download;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilder;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.FileUtil;
import java.io.File;
import java.io.IOException;
final class HttpDTTBuilder extends AbsNormalTTBuilder {
HttpDTTBuilder(AbsTaskWrapper wrapper) {
super(wrapper);
}
final class HttpDTTBuilderAdapter extends AbsNormalTTBuilderAdapter {
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new HttpDThreadTaskAdapter(config);
@ -59,4 +55,4 @@ final class HttpDTTBuilder extends AbsNormalTTBuilder {
}
return false;
}
}
}

@ -20,6 +20,7 @@ import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.group.AbsSubDLoadUtil;
import com.arialyy.aria.core.group.SubRecordHandler;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.SubLoader;
/**
@ -48,7 +49,7 @@ final class HttpSubDLoaderUtil extends AbsSubDLoadUtil {
@Override protected LoaderStructure buildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new SubRecordHandler(getWrapper()))
.addComponent(new HttpDTTBuilder(getWrapper()))
.addComponent(new NormalTTBuilder(getWrapper(), new HttpDTTBuilderAdapter()))
.addComponent(new HttpDFileInfoTask(getWrapper()));
structure.accept(getLoader());
return structure;

@ -15,11 +15,16 @@
*/
package com.arialyy.aria.http.upload;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpRecordHandler;
@ -44,7 +49,15 @@ public final class HttpULoaderUtil extends AbsNormalLoaderUtil {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new HttpRecordHandler(getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new HttpUTTBuilder(getTaskWrapper()));
.addComponent(new NormalTTBuilder(getTaskWrapper(), new AbsNormalTTBuilderAdapter() {
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new HttpUThreadTaskAdapter(config);
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
return true;
}
}));
structure.accept(getLoader());
return structure;
}

@ -1,41 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.http.upload;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilder;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
/**
* @Author lyy
* @Date 2019-09-19
*/
final class HttpUTTBuilder extends AbsNormalTTBuilder {
HttpUTTBuilder(AbsTaskWrapper wrapper) {
super(wrapper);
}
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new HttpUThreadTaskAdapter(config);
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
return true;
}
}

@ -78,16 +78,6 @@ public class FtpUrlEntity implements Cloneable {
*/
public InetAddress validAddr;
/**
* 证书路径
*/
public String storePath;
/**
* 证书密码
*/
public String storePass;
/**
* 连接协议
* {@link ProtocolType}
@ -99,10 +89,7 @@ public class FtpUrlEntity implements Cloneable {
*/
public boolean isImplicit = true;
/**
* 私钥别名
*/
public String keyAlias;
public IdEntity idEntity;
@Override public FtpUrlEntity clone() {
FtpUrlEntity entity = null;

@ -0,0 +1,47 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
/**
* 证书信息
*/
public class IdEntity {
/**
* 私钥证书内容非路径
*/
public String prvKey;
/**
* 私钥证书密码
*/
public String prvPass;
/**
* 公钥证书内容非路径
*/
public String pubKey;
/**
* 私钥证书路径
*/
public String storePath;
/**
* 私钥别名
*/
public String keyAlias;
}

@ -48,6 +48,8 @@ public class SubThreadConfig {
public int threadType = TYPE_HTTP;
// 进度更新间隔,单位:毫秒
public long updateInterval = 1000;
// 扩展数据
public Object obj;
/**
* 转换线程任务类型

@ -0,0 +1,90 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.loader;
import android.os.Handler;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
public abstract class AbsNormalTTBuilderAdapter {
protected String TAG = CommonUtil.getClassName(this);
protected AbsTaskWrapper wrapper;
private File tempFile;
public AbsNormalTTBuilderAdapter() {
}
protected void setWrapper(AbsTaskWrapper wrapper) {
this.wrapper = wrapper;
tempFile = new File(((AbsNormalEntity) wrapper.getEntity()).getFilePath());
}
/**
* 创建线程任务适配器
*/
public abstract IThreadTaskAdapter getAdapter(SubThreadConfig config);
/**
* 处理新任务
*
* @param record 任务记录
* @param totalThreadNum 任务的线程总数
* @return {@code true}创建新任务成功
*/
public abstract boolean handleNewTask(TaskRecord record, int totalThreadNum);
/**
* SubThreadConfig 模版如果不使用该方法创建配置则默认使用{@link #createNormalSubThreadConfig(Handler, ThreadRecord,
* boolean, int)}创建配置
*/
protected SubThreadConfig getSubThreadConfig(Handler stateHandler, ThreadRecord threadRecord,
boolean isBlock, int startNum) {
return createNormalSubThreadConfig(stateHandler, threadRecord, isBlock, startNum);
}
private SubThreadConfig createNormalSubThreadConfig(Handler stateHandler,
ThreadRecord threadRecord,
boolean isBlock, int startNum) {
SubThreadConfig config = new SubThreadConfig();
config.url = getEntity().isRedirect() ? getEntity().getRedirectUrl() : getEntity().getUrl();
config.tempFile =
isBlock ? new File(
String.format(IRecordHandler.SUB_PATH, tempFile.getPath(), threadRecord.threadId))
: tempFile;
config.isBlock = isBlock;
config.startThreadNum = startNum;
config.taskWrapper = wrapper;
config.record = threadRecord;
config.stateHandler = stateHandler;
config.threadType = SubThreadConfig.getThreadType(wrapper.getRequestType());
config.updateInterval = SubThreadConfig.getUpdateInterval(wrapper.getRequestType());
return config;
}
protected AbsNormalEntity getEntity() {
return (AbsNormalEntity) wrapper.getEntity();
}
protected File getTempFile() {
return tempFile;
}
}

@ -10,61 +10,46 @@ import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.task.ThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
public final class NormalTTBuilder implements IThreadTaskBuilder {
protected String TAG = CommonUtil.getClassName(this);
private Handler mStateHandler;
private AbsTaskWrapper mWrapper;
private TaskRecord mRecord;
private int mTotalThreadNum;
private File mTempFile;
private int mStartThreadNum;
private AbsNormalTTBuilderAdapter mAdapter;
public AbsNormalTTBuilder(AbsTaskWrapper wrapper) {
public NormalTTBuilder(AbsTaskWrapper wrapper, AbsNormalTTBuilderAdapter adapter) {
if (wrapper instanceof DGTaskWrapper) {
throw new AssertionError("NormalTTBuilder 不适用于组合任务");
}
mWrapper = wrapper;
mTempFile = new File(((AbsNormalEntity) wrapper.getEntity()).getFilePath());
}
protected File getTempFile() {
return mTempFile;
mAdapter = adapter;
mAdapter.setWrapper(wrapper);
}
protected AbsNormalEntity getEntity() {
return (AbsNormalEntity) mWrapper.getEntity();
}
/**
* 创建线程任务适配器
*/
public abstract IThreadTaskAdapter getAdapter(SubThreadConfig config);
/**
* 处理新任务
*
* @param record 任务记录
* @param totalThreadNum 任务的线程总数
* @return {@code true}创建新任务成功
*/
public abstract boolean handleNewTask(TaskRecord record, int totalThreadNum);
public AbsNormalTTBuilderAdapter getAdapter() {
return mAdapter;
}
/**
* 创建线程任务
*/
private IThreadTask createThreadTask(SubThreadConfig config) {
ThreadTask task = new ThreadTask(config);
task.setAdapter(getAdapter(config));
task.setAdapter(mAdapter.getAdapter(config));
return task;
}
@ -75,20 +60,8 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
* @param startNum 启动的线程数
*/
private IThreadTask createSingThreadTask(ThreadRecord record, int startNum) {
SubThreadConfig config = new SubThreadConfig();
config.url = getEntity().isRedirect() ? getEntity().getRedirectUrl() : getEntity().getUrl();
config.tempFile =
mRecord.isBlock ? new File(
String.format(IRecordHandler.SUB_PATH, mTempFile.getPath(), record.threadId))
: mTempFile;
config.isBlock = mRecord.isBlock;
config.startThreadNum = startNum;
config.taskWrapper = mWrapper;
config.record = record;
config.stateHandler = mStateHandler;
config.threadType = SubThreadConfig.getThreadType(mWrapper.getRequestType());
config.updateInterval = SubThreadConfig.getUpdateInterval(mWrapper.getRequestType());
return createThreadTask(config);
return createThreadTask(
mAdapter.getSubThreadConfig(mStateHandler, record, mRecord.isBlock, startNum));
}
/**
@ -117,7 +90,7 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
List<IThreadTask> threadTasks = new ArrayList<>(mTotalThreadNum);
mRecord.fileLength = fileLength;
if (mWrapper.isNewTask() && !handleNewTask(mRecord, mTotalThreadNum)) {
if (mWrapper.isNewTask() && !mAdapter.handleNewTask(mRecord, mTotalThreadNum)) {
ALog.e(TAG, "初始化线程任务失败");
return null;
}
@ -138,7 +111,7 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
Message msg = mStateHandler.obtainMessage();
msg.what = IThreadStateManager.STATE_COMPLETE;
Bundle b = msg.getData();
if (b == null){
if (b == null) {
b = new Bundle();
}
b.putString(IThreadStateManager.DATA_THREAD_NAME,

@ -13,19 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
package com.arialyy.aria.exception;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.sftp.BaseInfoThreadAdapter;
import java.util.Vector;
public class AriaLoginException extends BaseException{
private static final String ARIA_NET_EXCEPTION = "Aria Login Exception:";
final class DSFtpInfoThreadAdapter extends BaseInfoThreadAdapter<DTaskWrapper> {
DSFtpInfoThreadAdapter(DTaskWrapper taskWrapper) {
super(taskWrapper);
public AriaLoginException(String tag, String message) {
super(tag, String.format("%s%s", ARIA_NET_EXCEPTION, message));
}
@Override protected boolean handlerFile(Vector vector) {
return false;
public AriaLoginException(String tag, String message, Exception e){
super(tag, message, e);
}
}

@ -179,6 +179,7 @@ public class RecordUtil {
} else if (record.isBlock) { // 删除分块文件
removeBlockFile(record);
}
removeTargetFile(targetFile);
} else if (removeFile) { // 处理任务完成情况
if (recordIsM3U8(record.taskType)) {
removeTsCache(targetFile, record.bandWidth);
@ -249,6 +250,7 @@ public class RecordUtil {
} else if (record.isBlock) { // 删除分块文件
removeBlockFile(record);
}
removeTargetFile(targetFile);
} else if (removeFile) { // 处理任务完成情况
if (recordIsM3U8(record.taskType)) {
removeTsCache(targetFile, record.bandWidth);

@ -0,0 +1,124 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp;
import android.text.TextUtils;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.IdEntity;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.ILoaderVisitor;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
* 进行登录获取session获取文件信息
*/
public abstract class AbsSFtpInfoTask<WP extends AbsTaskWrapper> implements IInfoTask {
protected String TAG = CommonUtil.getClassName(this);
protected Callback callback;
private WP wrapper;
private FtpTaskOption option;
public AbsSFtpInfoTask(WP wp) {
this.wrapper = wp;
this.option = (FtpTaskOption) wrapper.getTaskOption();
}
protected abstract void getFileInfo(Session session)
throws JSchException, UnsupportedEncodingException, SftpException;
@Override public void run() {
try {
FtpUrlEntity entity = option.getUrlEntity();
String key = CommonUtil.getStrMd5(entity.hostName + entity.port + entity.user);
Session session = SFtpSessionManager.getInstance().getSession(key);
if (session == null) {
session = login(entity);
}
getFileInfo(session);
} catch (JSchException e) {
e.printStackTrace();
fail(false, null);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fail(false, null);
} catch (SftpException e) {
e.printStackTrace();
fail(false, null);
}
}
private Session login(FtpUrlEntity entity) throws JSchException, UnsupportedEncodingException {
JSch jSch = new JSch();
IdEntity idEntity = entity.idEntity;
if (idEntity.prvKey != null) {
if (idEntity.pubKey == null) {
jSch.addIdentity(idEntity.prvKey,
entity.password == null ? null : entity.password.getBytes("UTF-8"));
} else {
jSch.addIdentity(idEntity.prvKey, idEntity.pubKey,
entity.password == null ? null : entity.password.getBytes("UTF-8"));
}
}
Session session;
if (TextUtils.isEmpty(entity.user)) {
session = jSch.getSession(entity.url, entity.hostName, Integer.parseInt(entity.port));
} else {
session = jSch.getSession(entity.hostName);
}
if (!TextUtils.isEmpty(entity.password)) {
session.setPassword(entity.password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);// 为Session对象设置properties
session.setTimeout(3000);// 设置超时
session.setIdentityRepository(jSch.getIdentityRepository());
session.connect();
return session;
}
protected FtpTaskOption getOption() {
return option;
}
protected WP getWrapper() {
return wrapper;
}
protected void fail(boolean needRetry, BaseException e) {
callback.onFail(getWrapper().getEntity(), e, needRetry);
}
@Override public void setCallback(Callback callback) {
this.callback = callback;
}
@Override public void accept(ILoaderVisitor visitor) {
visitor.addComponent(this);
}
}

@ -1,100 +0,0 @@
///*
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.arialyy.aria.sftp;
//
//import com.arialyy.aria.core.AriaConfig;
//import com.arialyy.aria.core.FtpUrlEntity;
//import com.arialyy.aria.core.common.AbsNormalEntity;
//import com.arialyy.aria.core.inf.OnFileInfoCallback;
//import com.arialyy.aria.core.upload.UploadEntity;
//import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
//import com.arialyy.aria.ftp.FtpTaskOption;
//import com.arialyy.aria.util.ALog;
//import com.arialyy.aria.util.CommonUtil;
//import com.jcraft.jsch.ChannelSftp;
//import com.jcraft.jsch.JSchException;
//import com.jcraft.jsch.SftpException;
//import java.util.Vector;
//
///**
// * https://cloud.tencent.com/developer/article/1354612
// *
// * @author lyy
// */
//public class SFtpInfoThread<ENTITY extends AbsNormalEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
// implements Runnable {
//
// private final String TAG = CommonUtil.getClassName(getClass());
// protected ENTITY mEntity;
// protected TASK_WRAPPER mTaskWrapper;
// protected FtpTaskOption mTaskOption;
// private int mConnectTimeOut;
// protected OnFileInfoCallback mCallback;
// protected long mSize = 0;
// protected String charSet = "UTF-8";
// private boolean isUpload = false;
// private SFtpUtil mSFtpUtil;
// private BaseInfoThreadAdapter mAdapter;
//
// public SFtpInfoThread(SFtpUtil ftpUtil, TASK_WRAPPER taskWrapper,
// OnFileInfoCallback callback) {
// mSFtpUtil = ftpUtil;
// mTaskWrapper = taskWrapper;
// mEntity = taskWrapper.getEntity();
// mTaskOption = (FtpTaskOption) taskWrapper.getTaskOption();
// mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut();
// mCallback = callback;
// if (mEntity instanceof UploadEntity) {
// isUpload = true;
// }
// }
//
// public void setAdapter(BaseInfoThreadAdapter adapter) {
// mAdapter = adapter;
// }
//
// @Override public void run() {
// if (mAdapter == null) {
// ALog.e(TAG, "adapter为空");
// return;
// }
// try {
// ChannelSftp channelSftp =
// (ChannelSftp) mSFtpUtil.getSession().openChannel(SFtpUtil.CMD_TYPE_SFTP);
// Vector files = channelSftp.ls(getUrlEntity().remotePath);
//
// if (files.isEmpty()) {
// ALog.e(TAG, String.format("路径【%s】没有文件", getUrlEntity().remotePath));
// mCallback.onFail(mEntity, null, false);
// return;
// }
//
// if (!mAdapter.handlerFile(files)) {
// ALog.e(TAG, "文件处理失败");
// mCallback.onFail(mEntity, null, false);
// return;
// }
// } catch (JSchException ex) {
// ex.printStackTrace();
// } catch (SftpException e) {
// e.printStackTrace();
// }
// }
//
// private FtpUrlEntity getUrlEntity() {
// return mTaskOption.getUrlEntity();
// }
//}

@ -0,0 +1,98 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp;
import android.text.TextUtils;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.Session;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* SFTP session 管理器
* 1管理session
* 2定时清除无效的session
*/
public class SFtpSessionManager {
private String TAG = CommonUtil.getClassName(this);
private static volatile SFtpSessionManager INSTANCE = null;
private Map<String, Session> sessionDeque = new HashMap<>();
public synchronized static SFtpSessionManager getInstance() {
if (INSTANCE == null) {
synchronized (SFtpSessionManager.class) {
INSTANCE = new SFtpSessionManager();
}
}
return INSTANCE;
}
private SFtpSessionManager() {
}
/**
* 获取session获取完成session后检查map中的所有session移除所有失效的session
*
* @param key md5(host + port + userName )
* @return 如果session不可用返回null
*/
public Session getSession(String key) {
if (TextUtils.isEmpty(key)) {
ALog.e(TAG, "获取session失败,key为空");
return null;
}
Session session = sessionDeque.get(key);
if (session == null) {
ALog.w(TAG, "获取session失败,key:" + key);
}
cleanIdleSession();
return session;
}
/**
* 添加session
*/
public void addSession(Session session) {
if (session == null) {
ALog.e(TAG, "添加session到管理器失败,session 为空");
return;
}
String key =
CommonUtil.getStrMd5(session.getHost() + session.getPort() + session.getUserName());
sessionDeque.put(key, session);
}
/**
* 移除所有闲置的session闲置条件
* 1session 为空
* 2session未连接
*/
private void cleanIdleSession() {
if (sessionDeque.size() > 0) {
Iterator<Map.Entry<String, Session>> i = sessionDeque.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, Session> entry = i.next();
Session session = entry.getValue();
if (session == null || !session.isConnected()) {
i.remove();
}
}
}
}
}

@ -15,28 +15,7 @@
*/
package com.arialyy.aria.sftp;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import java.util.Vector;
import com.arialyy.aria.core.common.SubThreadConfig;
/**
* sftp文件信息适配器
*/
public abstract class BaseInfoThreadAdapter<WRAPPER extends AbsTaskWrapper> {
private WRAPPER mWrapper;
public BaseInfoThreadAdapter(WRAPPER taskWrapper) {
mWrapper = taskWrapper;
}
public WRAPPER getWrapper() {
return mWrapper;
}
/**
* 处理文件
*
* @return true 处理文件成功false 处理文件失败结束任务
*/
protected abstract boolean handlerFile(Vector vector);
public class SFtpSubThreadCOnfig extends SubThreadConfig {
}

@ -0,0 +1,49 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.sftp.AbsSFtpInfoTask;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import java.io.UnsupportedEncodingException;
/**
* 进行登录获取session获取文件信息
*/
final class SFtpDInfoTask extends AbsSFtpInfoTask<DTaskWrapper> {
SFtpDInfoTask(DTaskWrapper wrapper) {
super(wrapper);
}
@Override protected void getFileInfo(Session session) throws JSchException,
UnsupportedEncodingException, SftpException {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
SftpATTRS attr = channel.stat(
CommonUtil.convertFtpChar(getOption().getCharSet(), getWrapper().getEntity().getUrl()));
getWrapper().getEntity().setFileSize(attr.getSize());
CompleteInfo info = new CompleteInfo();
info.code = 200;
info.obj = channel;
callback.onSucceed(getWrapper().getKey(), info);
}
}

@ -0,0 +1,163 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.common.AbsEntity;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.event.EventMsgUtil;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.listener.IDLoadListener;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.loader.IThreadTaskBuilder;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.FileUtil;
import com.jcraft.jsch.ChannelSftp;
import java.io.File;
final class SFtpDLoader extends AbsNormalLoader {
private int startThreadNum; //启动的线程数
private boolean isComplete = false;
private Looper looper;
private ChannelSftp channelSftp;
SFtpDLoader(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
mTempFile = new File(getEntity().getFilePath());
EventMsgUtil.getDefault().register(this);
setUpdateInterval(wrapper.getConfig().getUpdateInterval());
}
private AbsNormalEntity getEntity() {
return (AbsNormalEntity) mTaskWrapper.getEntity();
}
@Override public long getFileSize() {
return getEntity().getFileSize();
}
/**
* 设置最大下载/上传速度AbsFtpInfoThread
*
* @param maxSpeed 单位为kb
*/
protected void setMaxSpeed(int maxSpeed) {
for (IThreadTask threadTask : getTaskList()) {
if (threadTask != null && startThreadNum > 0) {
threadTask.setMaxSpeed(maxSpeed / startThreadNum);
}
}
}
@Override public void onDestroy() {
super.onDestroy();
EventMsgUtil.getDefault().unRegister(this);
}
/**
* 启动单线程任务
*/
@Override
public void handleTask(Looper looper) {
if (isBreak() || isComplete) {
return;
}
this.looper = looper;
mInfoTask.run();
}
private void startThreadTask() {
if (getListener() instanceof IDLoadListener) {
((IDLoadListener) getListener()).onPostPre(getEntity().getFileSize());
}
File file = new File(getEntity().getFilePath());
if (file.getParentFile() != null && !file.getParentFile().exists()) {
FileUtil.createDir(file.getPath());
}
// 处理记录、初始化状态管理器
mRecord = mRecordHandler.getRecord(getFileSize());
mStateManager.setLooper(mRecord, looper);
// 创建线程任务
SFtpDTTBuilderAdapter ttBuild =
(SFtpDTTBuilderAdapter) ((NormalTTBuilder) mTTBuilder).getAdapter();
ttBuild.setChannel(channelSftp);
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
new Handler(looper, mStateManager.getHandlerCallback())));
startThreadNum = mTTBuilder.getCreatedThreadNum();
mStateManager.updateCurrentProgress(getEntity().getCurrentProgress());
if (mStateManager.getCurrentProgress() > 0) {
getListener().onResume(mStateManager.getCurrentProgress());
} else {
getListener().onStart(mStateManager.getCurrentProgress());
}
// 启动线程任务
for (IThreadTask threadTask : getTaskList()) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask);
}
// 启动定时器
startTimer();
}
@Override public long getCurrentProgress() {
return isRunning() ? mStateManager.getCurrentProgress() : getEntity().getCurrentProgress();
}
@Override public void addComponent(IRecordHandler recordHandler) {
mRecordHandler = recordHandler;
if (recordHandler.checkTaskCompleted()) {
mRecord.deleteData();
isComplete = true;
getListener().onComplete();
}
}
@Override public void addComponent(IInfoTask infoTask) {
mInfoTask = infoTask;
infoTask.setCallback(new IInfoTask.Callback() {
@Override public void onSucceed(String key, CompleteInfo info) {
channelSftp = (ChannelSftp) info.obj;
startThreadTask();
}
@Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
getListener().onFail(needRetry, e);
}
});
}
@Override public void addComponent(IThreadStateManager threadState) {
mStateManager = threadState;
}
@Override public void addComponent(IThreadTaskBuilder builder) {
mTTBuilder = builder;
}
}

@ -1,40 +0,0 @@
///*
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.arialyy.aria.sftp.download;
//
//import com.arialyy.aria.core.common.SubThreadConfig;
//import com.arialyy.aria.core.task.IThreadTask;
//import com.arialyy.aria.core.task.ThreadTask;
//import com.arialyy.aria.core.wrapper.ITaskWrapper;
//import com.arialyy.aria.ftp.download.FtpDTTBuilder;
//
///**
// * sftp下载适配器
// *
// * @author lyy
// */
//final class SFtpDLoaderAdapter extends FtpDTTBuilder {
// SFtpDLoaderAdapter(ITaskWrapper wrapper) {
// super(wrapper);
// }
//
// @Override public IThreadTask createThreadTask(SubThreadConfig config) {
// ThreadTask threadTask = new ThreadTask(config);
// SFtpDThreadTaskAdapter adapter = new SFtpDThreadTaskAdapter(config);
// threadTask.setAdapter(adapter);
// return threadTask;
// }
//}

@ -1,75 +1,54 @@
///*
// * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//package com.arialyy.aria.sftp.download;
//
//import com.arialyy.aria.core.common.AbsEntity;
//import com.arialyy.aria.core.common.CompleteInfo;
//import com.arialyy.aria.core.download.DTaskWrapper;
//import com.arialyy.aria.core.inf.OnFileInfoCallback;
//import com.arialyy.aria.core.listener.IEventListener;
//import com.arialyy.aria.core.loader.AbsNormalLoader;
//import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
//import com.arialyy.aria.core.loader.NormalLoader;
//import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
//import com.arialyy.aria.exception.BaseException;
//import com.arialyy.aria.ftp.FtpTaskOption;
//import com.arialyy.aria.sftp.SFtpInfoThread;
//import com.arialyy.aria.sftp.SFtpUtil;
//
///**
// * sftp下载工具
// *
// * @author lyy
// */
//public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
//
// private SFtpUtil mSftpUtil;
//
// protected SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
// super(wrapper, listener);
// wrapper.generateTaskOption(FtpTaskOption.class);
// FtpTaskOption option = (FtpTaskOption) wrapper.getTaskOption();
// mSftpUtil = new SFtpUtil.Builder()
// .setIp(option.getUrlEntity().hostName)
// .setPort(Integer.parseInt(option.getUrlEntity().port))
// .setUserName(option.getUrlEntity().user)
// .setPassword(option.getUrlEntity().password)
// .build();
// }
//
// @Override protected AbsNormalLoader createLoader() {
// NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper());
// loader.setAdapter(new SFtpDLoaderAdapter(getTaskWrapper()));
// return loader;
// }
//
// @Override protected Runnable createInfoThread() {
// DSFtpInfoThreadAdapter adapter = new DSFtpInfoThreadAdapter((DTaskWrapper) getTaskWrapper());
// SFtpInfoThread infoThread = new SFtpInfoThread<>(mSftpUtil, (DTaskWrapper) getTaskWrapper(),
// new OnFileInfoCallback() {
// @Override public void onComplete(String key, CompleteInfo info) {
//
// }
//
// @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
//
// }
// });
// infoThread.setAdapter(adapter);
//
// return infoThread;
// }
//}
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.ftp.download.FtpDRecordHandler;
/**
* sftp下载工具
*
* @author lyy
*/
public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
protected SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(FtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() {
return mLoader == null ? new SFtpDLoader(getTaskWrapper(), getListener()) : mLoader;
}
@Override public LoaderStructure BuildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new FtpDRecordHandler((DTaskWrapper) getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new SFtpDInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new NormalTTBuilder(getTaskWrapper(), new SFtpDTTBuilderAdapter()git));
structure.accept(getLoader());
return structure;
}
}

@ -0,0 +1,72 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
import android.os.Handler;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.FileUtil;
import com.jcraft.jsch.ChannelSftp;
import java.io.File;
class SFtpDTTBuilderAdapter extends AbsNormalTTBuilderAdapter {
private ChannelSftp channel;
SFtpDTTBuilderAdapter() {
}
void setChannel(ChannelSftp channel) {
this.channel = channel;
}
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new SFtpDThreadTaskAdapter(config);
}
@Override
protected SubThreadConfig getSubThreadConfig(Handler stateHandler, ThreadRecord threadRecord,
boolean isBlock, int startNum) {
SubThreadConfig config =
super.getSubThreadConfig(stateHandler, threadRecord, isBlock, startNum);
config.obj = channel;
return config;
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
if (!record.isBlock) {
if (getTempFile().exists()) {
FileUtil.deleteFile(getTempFile());
}
//CommonUtil.createFile(mTempFile.getPath());
} else {
for (int i = 0; i < totalThreadNum; i++) {
File blockFile =
new File(String.format(IRecordHandler.SUB_PATH, getTempFile().getPath(), i));
if (blockFile.exists()) {
ALog.d(TAG, String.format("分块【%s】已经存在,将删除该分块", i));
FileUtil.deleteFile(blockFile);
}
}
}
return true;
}
}

@ -23,7 +23,7 @@ import com.arialyy.aria.core.task.AbsThreadTaskAdapter;
*
* @author lyy
*/
public class SFtpDThreadTaskAdapter extends AbsThreadTaskAdapter {
final class SFtpDThreadTaskAdapter extends AbsThreadTaskAdapter {
SFtpDThreadTaskAdapter(SubThreadConfig config) {
super(config);

@ -279,7 +279,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
break;
case R.id.cancel:
Aria.download(this).load(mTaskId).cancel(true);
Aria.download(this).load(mTaskId).cancel();
break;
}
}

@ -288,7 +288,7 @@ public class M3U8VodDLoadActivity extends BaseActivity<ActivityM3u8VodBinding> {
}
break;
case R.id.cancel:
Aria.download(this).load(mTaskId).cancel(true);
Aria.download(this).load(mTaskId).cancel();
mTaskId = -1;
break;
}

Loading…
Cancel
Save