-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamService.m
75 lines (64 loc) · 2.39 KB
/
StreamService.m
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
//
// FileService.m
//
// Created by Jeremy Olmsted-Thompson on 1/20/12.
// Copyright (c) 2012 JOT. All rights reserved.
//
#import "StreamService.h"
@implementation StreamService
@synthesize outputStream = _outputStream;
@synthesize progressHandler = _progressHandler;
+(id)serviceWithOutputStream:(NSOutputStream*)stream {
return [[[self alloc] initWithOutputStream:stream] autorelease];
}
+(id)serviceWithOutputStream:(NSOutputStream*)stream progressHandler:(void (^)(long long, long long))progressHandler {
return [[[self alloc] initWithOutputStream:stream progressHandler:progressHandler] autorelease];
}
-(id)initWithOutputStream:(NSOutputStream*)stream {
return [self initWithOutputStream:stream progressHandler:nil];
}
-(id)initWithOutputStream:(NSOutputStream*)stream progressHandler:(void (^)(long long, long long))progressHandler {
if ((self = [super init])) {
self.outputStream = stream;
self.progressHandler = progressHandler;
}
return self;
}
-(void)dealloc {
[_outputStream release];
[_progressHandler release];
[super dealloc];
}
-(void)requestWithURL:(NSURL*)url
method:(NSString*)method
headers:(NSDictionary*)headers
bodyStream:(NSInputStream*)bodyStream
cachePolicy:(NSURLRequestCachePolicy)cachePolicy
timeoutInterval:(NSTimeInterval)timeoutInterval
receiveHandler:(void (^)(id, NSNumber*, NSDictionary*))receiveHandler
errorHandler:(void (^)(NSError*))errorHandler {
[_outputStream open];
_downloadedLength = 0;
[super requestWithURL:url
method:method
headers:headers
bodyStream:bodyStream
cachePolicy:cachePolicy
timeoutInterval:timeoutInterval
receiveHandler:^(id response, NSNumber *status, NSDictionary *headers) {
[_outputStream close];
receiveHandler(self.outputStream, status, headers);
}
errorHandler:^(NSError *error) {
[_outputStream close];
errorHandler(error);
}];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.outputStream write:[data bytes] maxLength:[data length]];
if (_progressHandler) {
_downloadedLength += [data length];
_progressHandler(_downloadedLength, self.expectedContentLength);
}
}
@end