forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSData+Stream.m
More file actions
35 lines (28 loc) · 734 Bytes
/
NSData+Stream.m
File metadata and controls
35 lines (28 loc) · 734 Bytes
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
#import "NSData+Stream.h"
@implementation NSData (Stream)
#define STDIN_CHUNK_SIZE 1024
+ (NSData*)dataWithStream:(FILE*)stream {
NSMutableData* data = [[NSMutableData alloc] init] ;
unsigned char *buf = NULL ;
NSInteger inBytes;
do {
buf = (unsigned char*)malloc(STDIN_CHUNK_SIZE);
inBytes = fread(buf, 1, STDIN_CHUNK_SIZE, stream);
[data appendBytes:buf
length:inBytes] ;
free(buf) ;
} while (inBytes > 0) ;
NSData* output = [NSData dataWithData:data] ;
[data release] ;
return output ;
}
- (void)writeToStream:(FILE*)stream {
NSInteger size = [self length] ;
if (size > 0) {
void* buffer = malloc(size) ;
[self getBytes:buffer] ;
fwrite(buffer, 1, size, stream) ;
free(buffer) ;
}
}
@end