forked from gnustep/libs-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSURLSessionTaskBody.m
111 lines (94 loc) · 2.05 KB
/
GSURLSessionTaskBody.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
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
#import "GSURLSessionTaskBody.h"
#import "Foundation/NSData.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSStream.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSValue.h"
@implementation GSURLSessionTaskBody
- (instancetype) init
{
if (nil != (self = [super init]))
{
_type = GSURLSessionTaskBodyTypeNone;
}
return self;
}
- (instancetype) initWithData: (NSData*)data
{
if (nil != (self = [super init]))
{
_type = GSURLSessionTaskBodyTypeData;
ASSIGN(_data, data);
}
return self;
}
- (instancetype) initWithFileURL: (NSURL*)fileURL
{
if (nil != (self = [super init]))
{
_type = GSURLSessionTaskBodyTypeFile;
ASSIGN(_fileURL, fileURL);
}
return self;
}
- (instancetype) initWithInputStream: (NSInputStream*)inputStream
{
if (nil != (self = [super init]))
{
_type = GSURLSessionTaskBodyTypeStream;
ASSIGN(_inputStream, inputStream);
}
return self;
}
- (void) dealloc
{
DESTROY(_data);
DESTROY(_fileURL);
DESTROY(_inputStream);
[super dealloc];
}
- (GSURLSessionTaskBodyType) type
{
return _type;
}
- (NSData*) data
{
return _data;
}
- (NSURL*) fileURL
{
return _fileURL;
}
- (NSInputStream*) inputStream
{
return _inputStream;
}
- (NSNumber*) getBodyLengthWithError: (NSError**)error
{
switch (_type)
{
case GSURLSessionTaskBodyTypeNone:
return [NSNumber numberWithInt: 0];
case GSURLSessionTaskBodyTypeData:
return [NSNumber numberWithUnsignedInteger: [_data length]];
case GSURLSessionTaskBodyTypeFile:
{
NSDictionary *attributes;
attributes = [[NSFileManager defaultManager]
attributesOfItemAtPath: [_fileURL path]
error: error];
if (!error)
{
NSNumber *size = [attributes objectForKey: NSFileSize];
return size;
}
else
{
return nil;
}
}
case GSURLSessionTaskBodyTypeStream:
return nil;
}
}
@end