-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCFileHandleOutputStream.m
60 lines (44 loc) · 1.36 KB
/
KCFileHandleOutputStream.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
//
// KCFileHandleOutputStream.m
// Degrees
//
// Created by Kevin Conner on 5/22/12.
// This software is free to use.
//
#import "KCFileHandleOutputStream.h"
// This class is adapted from http://stackoverflow.com/a/7136709/10906
@interface KCFileHandleOutputStream ()
@property (nonatomic, strong) NSFileHandle *fileHandle;
@end
@implementation KCFileHandleOutputStream
@synthesize delegate;
@synthesize fileHandle = _fileHandle;
#pragma mark - Init/dealloc
- (id)initWithFileHandle:(NSFileHandle *)fileHandle {
if (self = [super init]) {
self.fileHandle = fileHandle;
}
return self;
}
#pragma mark - NSOutputStream
- (BOOL)hasSpaceAvailable {
return YES;
}
- (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)length {
// TODO Don't write all data at once. Instead, schedule asynchronous main thread writes using parts of the data.
[self.fileHandle writeData:[NSData dataWithBytesNoCopy:(void *)buffer length:length freeWhenDone:NO]];
return length;
}
- (void)open {
[self.delegate stream:self handleEvent:NSStreamEventOpenCompleted];
}
- (void)close {
[self.delegate stream:self handleEvent:NSStreamEventEndEncountered];
}
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
// No-op.
}
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
// No-op.
}
@end