Skip to content

Commit

Permalink
优化AbsThreadTask代码, 新增文件长度处理功能 https://github.com/AriaLyy/Aria/issues/393
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaLyy committed May 9, 2019
1 parent 0c3e16c commit 0dbc515
Show file tree
Hide file tree
Showing 33 changed files with 2,740 additions and 2,663 deletions.
Expand Up @@ -16,6 +16,7 @@
package com.arialyy.aria.core.common;

import android.content.Context;
import android.os.Looper;
import android.util.SparseArray;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.BaseDListener;
Expand Down
257 changes: 135 additions & 122 deletions Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java

Large diffs are not rendered by default.

Expand Up @@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.common;

import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.exception.BaseException;

public interface OnFileInfoCallback {
Expand All @@ -30,5 +31,5 @@ public interface OnFileInfoCallback {
*
* @param e 错误信息
*/
void onFail(String url, BaseException e, boolean needRetry);
void onFail(AbsEntity entity, BaseException e, boolean needRetry);
}
Expand Up @@ -374,7 +374,7 @@ protected void handleFile(String remotePath, FTPFile ftpFile) {

private void failDownload(BaseException e, boolean needRetry) {
if (mCallback != null) {
mCallback.onFail(mEntity.getKey(), e, needRetry);
mCallback.onFail(mEntity, e, needRetry);
}
}

Expand Down
Expand Up @@ -57,7 +57,7 @@ protected AbsFtpThreadTask(StateConstance constance, IEventListener listener,
*/
protected FTPClient createClient() {
FTPClient client = null;
final FtpUrlEntity urlEntity = mTaskWrapper.asFtp().getUrlEntity();
final FtpUrlEntity urlEntity = getTaskWrapper().asFtp().getUrlEntity();
if (urlEntity.validAddr == null) {
try {
InetAddress[] ips = InetAddress.getAllByName(urlEntity.hostName);
Expand Down Expand Up @@ -106,8 +106,8 @@ protected FTPClient createClient() {
// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码
charSet = "UTF-8";
if (reply != FTPReply.COMMAND_IS_SUPERFLUOUS) {
if (!TextUtils.isEmpty(mTaskWrapper.asFtp().getCharSet())) {
charSet = mTaskWrapper.asFtp().getCharSet();
if (!TextUtils.isEmpty(getTaskWrapper().asFtp().getCharSet())) {
charSet = getTaskWrapper().asFtp().getCharSet();
}
}
client.setControlEncoding(charSet);
Expand All @@ -116,7 +116,7 @@ protected FTPClient createClient() {
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(5000);
if (mTaskWrapper.asFtp().getUrlEntity().isFtps) {
if (getTaskWrapper().asFtp().getUrlEntity().isFtps) {
((FTPSClient) client).execPROT("P");
}
} catch (java.io.IOException e) {
Expand Down Expand Up @@ -150,7 +150,7 @@ private FTPClient newInstanceClient(FtpUrlEntity urlEntity) {
private FTPClient connect(FTPClient client, InetAddress[] ips, int index, int port) {
try {
client.connect(ips[index], port);
mTaskWrapper.asFtp().getUrlEntity().validAddr = ips[index];
getTaskWrapper().asFtp().getUrlEntity().validAddr = ips[index];
return client;
} catch (java.io.IOException e) {
try {
Expand Down
207 changes: 103 additions & 104 deletions Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java
@@ -1,104 +1,103 @@
/*
* 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.common.http;

import android.text.TextUtils;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadGroupTarget;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.ITargetHandler;
import com.arialyy.aria.util.ALog;
import java.util.HashMap;
import java.util.Map;

/**
* HTTP参数委托
* @param <TARGET>
*/
class HttpDelegate<TARGET extends AbsTarget> implements ITargetHandler {
private static final String TAG = "PostDelegate";
TARGET mTarget;

HttpDelegate(TARGET target) {
mTarget = target;
}

public TARGET setParams(Map<String, String> params) {
mTarget.getTaskWrapper().asHttp().setParams(params);
if (mTarget instanceof DownloadGroupTarget) {
for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) {
subTask.asHttp().setParams(params);
}
}
return mTarget;
}

public TARGET setParam(String key, String value) {
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
ALog.d(TAG, "key 或value 为空");
return mTarget;
}
Map<String, String> params = mTarget.getTaskWrapper().asHttp().getParams();
if (params == null) {
params = new HashMap<>();
mTarget.getTaskWrapper().asHttp().setParams(params);
}
params.put(key, value);
if (mTarget instanceof DownloadGroupTarget) {
for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) {
subTask.asHttp().setParams(params);
}
}
return mTarget;
}

@Override public void add() {
mTarget.add();
}

@Override public void start() {
mTarget.start();
}

@Override public void stop() {
mTarget.stop();
}

@Override public void resume() {
mTarget.resume();
}

@Override public void cancel() {
mTarget.cancel();
}

@Override public void save() {
mTarget.save();
}

@Override public void cancel(boolean removeFile) {
mTarget.cancel(removeFile);
}

@Override public void reTry() {
mTarget.reTry();
}

@Override public void reStart() {
mTarget.reStart();
}
}
/*
* 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.common.http;

import android.text.TextUtils;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadGroupTarget;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.ITargetHandler;
import com.arialyy.aria.util.ALog;
import java.util.HashMap;
import java.util.Map;

/**
* HTTP参数委托
*/
class HttpDelegate<TARGET extends AbsTarget> implements ITargetHandler {
private static final String TAG = "PostDelegate";
TARGET mTarget;

HttpDelegate(TARGET target) {
mTarget = target;
}

public TARGET setParams(Map<String, String> params) {
mTarget.getTaskWrapper().asHttp().setParams(params);
if (mTarget instanceof DownloadGroupTarget) {
for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) {
subTask.asHttp().setParams(params);
}
}
return mTarget;
}

public TARGET setParam(String key, String value) {
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
ALog.d(TAG, "key 或value 为空");
return mTarget;
}
Map<String, String> params = mTarget.getTaskWrapper().asHttp().getParams();
if (params == null) {
params = new HashMap<>();
mTarget.getTaskWrapper().asHttp().setParams(params);
}
params.put(key, value);
if (mTarget instanceof DownloadGroupTarget) {
for (DTaskWrapper subTask : ((DGTaskWrapper) mTarget.getTaskWrapper()).getSubTaskWrapper()) {
subTask.asHttp().setParams(params);
}
}
return mTarget;
}

@Override public void add() {
mTarget.add();
}

@Override public void start() {
mTarget.start();
}

@Override public void stop() {
mTarget.stop();
}

@Override public void resume() {
mTarget.resume();
}

@Override public void cancel() {
mTarget.cancel();
}

@Override public void save() {
mTarget.save();
}

@Override public void cancel(boolean removeFile) {
mTarget.cancel(removeFile);
}

@Override public void reTry() {
mTarget.reTry();
}

@Override public void reStart() {
mTarget.reStart();
}
}
Expand Up @@ -17,10 +17,20 @@

import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.inf.AbsHttpFileLenAdapter;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IHttpFileLenAdapter;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.Proxy;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -80,7 +90,31 @@ public TARGET addHeaders(@NonNull Map<String, String> headers) {
return mTarget;
}

public void addHeader(AbsTaskWrapper taskWrapper, String key, String value) {
public TARGET setFileLenAdapter(AbsHttpFileLenAdapter adapter) {
if (adapter == null) {
throw new IllegalArgumentException("adapter为空");
}
try {
adapter.clone();
mTarget.getTaskWrapper().asHttp().setFileLenAdapter((IHttpFileLenAdapter) adapter.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
//// 以下代码有问题,匿名内部类不能序列化
//String objPath = String.format("%s/obj_temp/%s", AriaManager.APP.getFilesDir().getPath(),
// adapter.hashCode());
//CommonUtil.writeObjToFile(objPath, adapter);
//IHttpFileLenAdapter newAdapter = (IHttpFileLenAdapter) CommonUtil.readObjFromFile(objPath);
//File temp = new File(objPath);
//if (temp.exists()) {
// temp.delete();
//}
//mTarget.getTaskWrapper().asHttp().setFileLenAdapter(newAdapter);

return mTarget;
}

private void addHeader(AbsTaskWrapper taskWrapper, String key, String value) {
HttpTaskDelegate taskDelegate = taskWrapper.asHttp();
if (taskDelegate.getHeaders().get(key) == null) {
taskDelegate.getHeaders().put(key, value);
Expand All @@ -89,7 +123,7 @@ public void addHeader(AbsTaskWrapper taskWrapper, String key, String value) {
}
}

public void addHeaders(AbsTaskWrapper taskWrapper, Map<String, String> headers) {
private void addHeaders(AbsTaskWrapper taskWrapper, Map<String, String> headers) {
HttpTaskDelegate taskDelegate = taskWrapper.asHttp();
/*
两个map比较逻辑
Expand Down

0 comments on commit 0dbc515

Please sign in to comment.