/
TiDataStream.m
248 lines (206 loc) Β· 9.78 KB
/
TiDataStream.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "TiDataStream.h"
@implementation TiDataStream
@synthesize data, mode;
#pragma mark Internals
-(id)init
{
if (self = [super init]) {
data = nil;
mode = TI_READ;
position = 0;
}
return self;
}
-(void)_destroy
{
RELEASE_TO_NIL(data);
[super _destroy];
}
#pragma mark I/O Stream implementation
-(int)readToBuffer:(TiBuffer *)toBuffer offset:(int)offset length:(int)length callback:(KrollCallback *)callback
{
if (data == nil) {
[self throwException:@"TiStreamException"
subreason:@"Attempt to read off of closed/nil data stream"
location:CODELOCATION];
}
// TODO: Codify in read() and write() when we have every method calling the wrappers... like it should.
if ([[toBuffer data] length] == 0 && length != 0) {
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(0),@"bytesProcessed", NUMINT(0),@"errorState",@"",@"errorDescription", nil];
[self _fireEventToListener:@"read" withObject:event listener:callback thisObject:nil];
}
return 0;
}
// TODO: Throw exception, or no-op? For now, assume NO-OP
if (position >= [data length]) {
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(-1),@"bytesProcessed",NUMINT(0),@"errorState",@"",@"errorDescription", nil];
[self _fireEventToListener:@"read" withObject:event listener:callback thisObject:nil];
}
return -1;
}
// TODO: This is a dumb convention. Go back and fix it.
if (length == 0) {
length = [data length];
[toBuffer setLength:NUMINT(length)];
}
const void* bytes = [data bytes];
void* toBytes = [[toBuffer data] mutableBytes];
int bytesToWrite = MIN([data length] - position, length);
memcpy(toBytes+offset, bytes+position, bytesToWrite);
position += bytesToWrite;
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(bytesToWrite),@"bytesProcessed",NUMINT(0),@"errorState",@"",@"errorDescription", nil];
[self _fireEventToListener:@"read" withObject:event listener:callback thisObject:nil];
}
return bytesToWrite;
}
// TODO: Need to extend the data if we're writing past its current bounds
-(int)writeFromBuffer:(TiBuffer *)fromBuffer offset:(int)offset length:(int)length callback:(KrollCallback *)callback
{
if (data == nil) {
[self throwException:@"TiStreamException"
subreason:@"Attempt to write from closed/nil data stream"
location:CODELOCATION];
}
// Sanity check for mutable data (just in case...)
if (![data isKindOfClass:[NSMutableData class]]) {
NSString* errorStr = [NSString stringWithFormat:@"[ERROR] Attempt to write to unwritable stream"];
DebugLog(errorStr);
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(-1),@"bytesProcessed",errorStr,@"errorDescription",NUMINT(-1),@"errorState", nil];
[self _fireEventToListener:@"write" withObject:event listener:callback thisObject:nil];
}
return -1;
}
// TODO: Codify in read() and write() when we have every method calling the wrappers... like it should.
if ([[fromBuffer data] length] == 0) {
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(0),@"bytesProcessed",NUMINT(0),@"errorState",@"",@"errorDescription", nil];
[self _fireEventToListener:@"write" withObject:event listener:callback thisObject:nil];
}
return 0;
}
// OK, even if we're working with NSData (and not NSMutableData) we have to cast away const here; we're going to assume that
// even with immutable data (i.e. blob) if the user has specified WRITE or APPEND, they're OK with digging their own grave.
NSMutableData* mutableData = (NSMutableData*)data;
if (mode & TI_WRITE) {
int overflow = length - ([data length] - position);
if (overflow > 0) {
[mutableData increaseLengthBy:overflow];
}
void* bytes = [mutableData mutableBytes];
const void* fromBytes = [[fromBuffer data] bytes];
memcpy(bytes+position, fromBytes+offset, length);
position += length;
}
else if (mode & TI_APPEND) {
[mutableData appendData:[[fromBuffer data] subdataWithRange:NSMakeRange(offset,length)]];
position = [data length];
}
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",NUMINT(length),@"bytesProcessed",NUMINT(0),@"errorState",@"",@"errorDescription",nil];
[self _fireEventToListener:@"write" withObject:event listener:callback thisObject:nil];
}
return length;
}
-(int)writeToStream:(id<TiStreamInternal>)output chunkSize:(int)size callback:(KrollCallback *)callback
{
if (data == nil) {
[self throwException:@"TiStreamException"
subreason:@"Attempt to write from closed/nil data stream"
location:CODELOCATION];
}
int length = [data length];
int totalBytes = 0;
while (position < length) {
TiBuffer* tempBuffer = [[[TiBuffer alloc] _initWithPageContext:[self executionContext]] autorelease];
NSRange subdataRange = NSMakeRange(position,MIN(size,length-position));
int bytesWritten = 0;
@try {
void* bytes = malloc(subdataRange.length);
if (bytes == NULL) {
[self throwException:TiExceptionMemoryFailure subreason:@"Failed to allocate for stream" location:CODELOCATION];
}
[data getBytes:bytes range:subdataRange];
[tempBuffer setData:[NSMutableData dataWithBytesNoCopy:bytes length:subdataRange.length freeWhenDone:YES]];
bytesWritten = [output writeFromBuffer:tempBuffer offset:0 length:subdataRange.length callback:nil];
}
@catch (NSException* e) {
// TODO: We'll need some kind of information about:
// 1. Error Code
// 2. # bytes produced as part of the write
// In the exception.
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"fromStream",output,@"toStream",NUMINT(totalBytes),@"bytesWritten",[e reason],@"errorDescription", NUMINT(-1),@"errorState",nil];
[self _fireEventToListener:@"writeToStream" withObject:event listener:callback thisObject:nil];
}
else {
@throw e;
}
}
if (bytesWritten == 0) {
break;
}
totalBytes += bytesWritten;
position += subdataRange.length;
}
if (callback != nil) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"fromStream",output,@"toStream",NUMINT(totalBytes),@"bytesProcessed",NUMINT(0),@"errorState",@"",@"errorDescription",nil];
[self _fireEventToListener:@"writeToStream" withObject:event listener:callback thisObject:nil];
}
return totalBytes;
}
// We don't need the asynch hint
-(void)pumpToCallback:(KrollCallback *)callback chunkSize:(int)size asynch:(BOOL)asynch
{
if (data == nil) {
[self throwException:@"TiStreamException"
subreason:@"Attempt to write from closed/nil data stream"
location:CODELOCATION];
}
int totalBytes = 0;
int bytesWritten = 0;
int length = [data length];
const void* source = [data bytes];
while (position < length) {
TiBuffer* tempBuffer = [[[TiBuffer alloc] _initWithPageContext:[self executionContext]] autorelease];
int bytesToWrite = MIN(size, length-position);
void* destination = malloc(bytesToWrite);
if (destination == NULL) {
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",[NSNull null],@"buffer",NUMINT(-1),@"bytesProcessed",NUMINT(totalBytes),@"totalBytesProcessed", NUMINT(1),@"errorState",@"Memory allocation failure",@"errorDescription", nil];
[self _fireEventToListener:@"pump" withObject:event listener:callback thisObject:nil];
break;
}
memcpy(destination, source+position, bytesToWrite);
[tempBuffer setData:[NSMutableData dataWithBytesNoCopy:destination length:bytesToWrite freeWhenDone:YES]];
totalBytes += bytesToWrite;
position += bytesToWrite;
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",tempBuffer,@"buffer",NUMINT(bytesToWrite),@"bytesProcessed",NUMINT(totalBytes),@"totalBytesProcessed", NUMINT(0),@"errorState",@"",@"errorDescription",nil];
[self _fireEventToListener:@"pump" withObject:event listener:callback thisObject:nil];
}
// We've reached the end of the stream - so we need to pump the -1 EOF
NSDictionary* event = [NSDictionary dictionaryWithObjectsAndKeys:self,@"source",[NSNull null],@"buffer",NUMINT(-1),@"bytesProcessed",NUMINT(totalBytes),@"totalBytesProcessed", NUMINT(0),@"errorState",@"",@"errorDescription", nil];
[self _fireEventToListener:@"pump" withObject:event listener:callback thisObject:nil];
}
-(NSNumber*)isReadable:(id)_void
{
return NUMBOOL(mode & TI_READ);
}
-(NSNumber*)isWritable:(id)_void
{
return NUMBOOL(mode & (TI_WRITE | TI_APPEND));
}
-(void)close:(id)_void
{
RELEASE_TO_NIL(data);
position = 0;
}
@end