-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXXCNFileDownloader.m
More file actions
228 lines (166 loc) · 5.51 KB
/
XXCNFileDownloader.m
File metadata and controls
228 lines (166 loc) · 5.51 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// XXCNFileDownLoader.m
// XXCNDownLoadManager
//
// Created by wang yan on 14-2-12.
// Copyright (c) 2014年 sogou. All rights reserved.
//
#import "XXCNFileDownloader.h"
const NSTimeInterval kDefaultTimeoutInterval=30.0;
@implementation XXCNFileDownloader
{
//下载的文件的url地址字符串
NSString *_urlStr;
//下载文件的NSURLConnection
NSURLConnection *_connection;
//本次是否为断点续传
BOOL _isResume;
//本下载进程是否已经在进行中(进行中和还在等待中的进程,取消操作需要使用不同的方法)
BOOL _operationStarted;
BOOL finished;
BOOL executing;
BOOL cancelled;
}
-(id)initWidthURLStr:(NSString *)urlstr
{
self=[super init];
if (self) {
finished=NO;
executing=NO;
cancelled=NO;
_urlStr=urlstr;
_expectedDataLength=0;
_receivedDataLength=0;
}
return self;
}
-(void)startDownload
{
[self start];
}
#pragma mark --override NSOPeration
//NSOperation的子类,如果并发操作必须重写此方法
-(void)start
{
NSMutableURLRequest *fileRequest =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:_urlStr]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:kDefaultTimeoutInterval];
if (_receivedDataLength!=0) {
_isResume=YES;
NSString *range = [NSString stringWithFormat:@"bytes=%lld-", _receivedDataLength];
[fileRequest setValue:range forHTTPHeaderField:@"Range"];
}
_connection=[[NSURLConnection alloc]initWithRequest:fileRequest delegate:self startImmediately:NO];
if (_connection) {
[_connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
if ([self isCancelled]){
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
} else {
[self willChangeValueForKey:@"isExecuting"];
executing = YES;
[_connection start];
_operationStarted=YES;
[self didChangeValueForKey:@"isExecuting"];
}
}
NSLog(@"start:%@",_tag);
}
//是否并发操作
-(BOOL)isConcurrent
{
return YES;
}
//线程是否操释放掉,不重写这个方法,线程内存无法释放
- (BOOL) isFinished{
return finished;
}
//线程是否还在执行操作
- (BOOL) isExecuting{
return executing;
}
//线程是否被取消,不重写这个方法,线程操作无法被取消
-(BOOL)isCancelled
{
return cancelled;
}
#pragma mark --NSURLConnectionDelegate
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"XXCNFileDownloader didFailWithError");
if (self.delegate && [self.delegate respondsToSelector:@selector(XXCNFileDownloader:loadFailWithError:)]) {
[self.delegate XXCNFileDownloader:self loadFailWithError:error];
}
[self finishOperation];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
_receivedDataLength+=[data length];
float progressValue=(float)_receivedDataLength/_expectedDataLength*100;
//NSLog(@"didReceiveData:%llu--%.2f",_receivedDataLength,progressValue);
if (self.delegate && [self.delegate respondsToSelector:@selector(XXCNFileDownloader:loadWithProgress:)]) {
[self.delegate XXCNFileDownloader:self loadWithProgress:progressValue];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//本次应该下载的数据大小
_expectedDataLength = [response expectedContentLength];
//如果是断点续传,文件的总大小应该是已经下载的大小+本次应该下载的大小
if (_isResume) {
_expectedDataLength+=_receivedDataLength;
}
NSLog(@"didReceiveResponse:%llu",_expectedDataLength);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading");
if (self.delegate && [self.delegate respondsToSelector:@selector(XXCNFileDownloaderLoadComplete:)]) {
[self.delegate XXCNFileDownloaderLoadComplete:self];
}
[self finishOperation];
}
#pragma --mark 自定义的方法
//结束进程
-(void)finishOperation
{
if (!_operationStarted) {
return;
}
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
[_connection cancel];
_connection=nil;
finished = YES;
executing = NO;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
//取消线程任务(对已经完成的线程任务无效)
-(void)cancelOperation
{
[self pauseOperation];
}
-(void)pauseOperation
{
if ([self isFinished]) {
return;
}
if (!_operationStarted) {
[self willChangeValueForKey:@"isCancelled"];
[_connection cancel];
_connection=nil;
cancelled=YES;
[self didChangeValueForKey:@"isCancelled"];
}else
{
[self finishOperation];
}
if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(XXCNFileDownloaderPaused:)]) {
[self.delegate XXCNFileDownloaderPaused:self];
}
}
@end