-
Notifications
You must be signed in to change notification settings - Fork 2
/
DownloadCenter.xcsm
213 lines (182 loc) · 6.84 KB
/
DownloadCenter.xcsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//xlang Source, Name:DownloadCenter.xcsm
//Date: Wed Dec 15:23:25 2019
class DownloadCenter{
static Map<String, Downloader> dltasks = new Map<String, Downloader> ();
static bool cancelDownload = false;
public static void createDownload(String key, String url, String local, long totalsize, DownloadListener ls, Object tag){
new Downloader(key, url, local, totalsize, ls, tag);
}
public static int getTaskProgress(String key){
synchronized(dltasks){
try{
Downloader dl = dltasks.get(key);
if (dl != nilptr){
return dl.progress;
}
}catch(Exception e){
}
}
return -1;
}
public @NotNilptr static List<String> getKeys(){
List<String> keys = new List<String>();
synchronized(dltasks){
Map.Iterator<String, Downloader> iter = dltasks.iterator();
while (iter.hasNext()) {
keys.add(iter.getKey());
iter.next();
}
}
return keys;
}
public static bool hasTask(){
return dltasks.size() != 0;
}
public static Downloader getTask(String key){
try{
synchronized(dltasks){
return dltasks.get(key);
}
}catch(Exception e){
}
return nilptr;
}
public static class Downloader : Thread{
public DownloadListener lis ;
public Object _tag;
public bool bcancel = false;
public String url, localfile;
public long dltotalsize;
public int progress = 0;
public DownloadStatus state = DownloadStatus.DL_UNKNOW;
public String key;
public Downloader(String _key, String _url, String _local, long _totalsize, DownloadListener dl, Object tag){
localfile = _local;
key = _key;
url = _url;
dltotalsize = _totalsize;
lis = dl;
_tag = tag;
start();
}
public void run()override{
String dlfile = localfile;
if (dlfile == nilptr){
return ;
}
synchronized(dltasks){
dltasks.put(key, this);
}
long totallen = 0;
FileOutputStream fos;
try{
HttpRequest req = new HttpRequest();
fos = new FileOutputStream(dlfile);
String _url = url;
state = DownloadStatus.DL_BEGIN;
progress = 0;
updateProgress();
String proxy_type = Setting.get("proxy_type");
String proxy_host = Setting.get("proxy_host");
String proxy_port = Setting.get("proxy_port");
String proxy_user = Setting.get("proxy_user");
String proxy_pwd = Setting.get("proxy_pwd");
if (proxy_type.length() != 0 && proxy_host.length() != 0 && proxy_port.length() != 0)
{
int proxytype = HttpRequest.PROXY_HTTP;
switch(proxy_type){
case "HTTP1.0":
proxytype = HttpRequest.PROXY_HTTP_10;
break;
case "SOCKS4":
proxytype = HttpRequest.PROXY_SOCKS4;
break;
case "SOCKS5":
proxytype = HttpRequest.PROXY_SOCKS5;
break;
case "SOCKS4A":
proxytype = HttpRequest.PROXY_SOCKS4A;
break;
case "SOCK5_HOSTNAME":
proxytype = HttpRequest.PROXY_HTTP_SOCKS5_HOSTNAME;
break;
default :
proxytype = HttpRequest.PROXY_HTTP;
break;
}
if (proxy_user.length() == 0){
proxy_user = nilptr;
}
if (proxy_pwd.length() == 0){
proxy_pwd = nilptr;
}
req.setProxy(proxy_host,proxy_port.parseInt(),proxytype,proxy_user,proxy_pwd);
}
if (_url != nilptr && req.get(_url, 0, _url.upper().startsWith("HTTPS://"))){
if (req.getResponseCode() >= 200){
if (dltotalsize <= 0){
dltotalsize = req.getLength();
}
byte [] data = new byte[4096];
state = DownloadStatus.DL_INPROCESSING;
while ((bcancel == false) && (req.available(true) > 0)){
int readlen = req.readData(data,0,data.length);
fos.write(data, 0, readlen);
totallen += readlen;
progress = (int)( (totallen * 100) / dltotalsize);
updateProgress();
if (cancelDownload){
break;
}
}
}
}
}catch(Exception e){
}finally{
if (fos != nilptr){
fos.close();
}
}
progress = totallen == dltotalsize ? 100 : 0;
if (!bcancel){
state = ( (totallen == dltotalsize) ? DownloadStatus.DL_DONE : DownloadStatus.DL_FAILED);
}else{
state = DownloadStatus.DL_CANCEL;
}
updateProgress();
synchronized(dltasks){
dltasks.remove(key);
}
}
public void updateProgress(){
if (lis != nilptr){
lis.onStatus(state, progress, _tag);
}
}
public void setCancel(bool bc){
bcancel = bc;
}
};
public static void stopAlldownload(){
cancelDownload = true;
}
public static bool hasDownload(){
return dltasks.size() > 0;
}
public static bool hasDownloadTask(String key){
synchronized(dltasks){
return dltasks.containsKey(key);
}
}
public static void cancelDownloadTask(String key){
try{
synchronized(dltasks){
Downloader dl = dltasks.get(key);
if (dl != nilptr){
dl.setCancel(true);
}
}
}catch(Exception e){
}
}
};