-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBufferOutputStreamToInputStream.m
More file actions
188 lines (122 loc) · 4.76 KB
/
BufferOutputStreamToInputStream.m
File metadata and controls
188 lines (122 loc) · 4.76 KB
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
//
// BufferWriter.m
// Streams
//
// Created by Peter Paulis on 03/02/15.
// Copyright (c) 2015 Peter Paulis. All rights reserved.
//
#import "BufferOutputStreamToInputStream.h"
// Categories
#import "NSStream+BoundPairAdditions.h"
#define BufferedStreamBufferSize 512
#define TotalBufferCapacityHint 4096*1024 // 4MB
#define CleanDataBufferOverOffset (TotalBufferCapacityHint / 2.)
@interface BufferOutputStreamToInputStream()
@property (nonatomic, strong, readwrite) NSOutputStream * outputStream;
@property (strong, nonatomic, readwrite) NSInputStream * inputStream;
@property (nonatomic, strong) NSMutableData * dataBuffer;
@property (nonatomic, assign) NSUInteger offset;
@property (nonatomic, assign) BOOL hasSpaceAvailable;
@end
@implementation BufferOutputStreamToInputStream
- (id)init {
self = [super init];
if (self) {
// create bounded input and output stream
NSOutputStream * os;
NSInputStream * is;
[NSStream createBoundInputStream:&is outputStream:&os bufferSize:512];
self.outputStream = os;
self.inputStream = is;
}
return self;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Getters / Setters
////////////////////////////////////////////////////////////////////////
- (NSMutableData *)dataBuffer {
if (_dataBuffer) {
return _dataBuffer;
}
_dataBuffer = [[NSMutableData alloc] initWithCapacity:TotalBufferCapacityHint];
return _dataBuffer;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Public
////////////////////////////////////////////////////////////////////////
- (void)openOutputStream {
self.outputStream.delegate = self;
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream open];
}
- (void)closeOutputStream {
[self.outputStream close];
[self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.outputStream.delegate = nil;
}
- (void)addDataToBuffer:(NSData *)data {
NSAssert([self.outputStream streamStatus] == NSStreamStatusOpen, @"NOT OPENED");
[self.dataBuffer appendData:data];
if (self.hasSpaceAvailable) {
[self sendDataChunk];
}
}
- (void)addBytesToBuffer:(const void *)bytes length:(NSUInteger)length {
NSAssert([self.outputStream streamStatus] == NSStreamStatusOpen, @"NOT OPENED");
[self.dataBuffer appendBytes:bytes length:length];
if (self.hasSpaceAvailable) {
[self sendDataChunk];
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark - Private
////////////////////////////////////////////////////////////////////////
- (BOOL)sendDataChunk {
if (self.dataBuffer == nil) {
return NO;
}
NSUInteger dataLength = [self.dataBuffer length];
NSUInteger readLength = BufferedStreamBufferSize;
if (self.offset == dataLength) {
// all data written so far
return NO;
} else if ((self.offset + BufferedStreamBufferSize) > dataLength) {
// more data than can be written
readLength = dataLength - self.offset;
}
if (readLength <= 0) {
return NO;
}
uint8_t *readBytes = (uint8_t*)malloc(readLength);
[self.dataBuffer getBytes:readBytes range:NSMakeRange(self.offset, readLength)];
NSInteger writtenLength = [self.outputStream write:readBytes maxLength:readLength];
if (writtenLength > 0) {
self.offset += writtenLength;
if (self.offset >= CleanDataBufferOverOffset) {
NSRange range = NSMakeRange(0, self.offset);
[self.dataBuffer replaceBytesInRange:range withBytes:NULL length:0];
self.offset = 0;
}
} else if (writtenLength < 0) {
NSLog(@"WRITE BUFFER FAILED");
return NO;
}
return YES;
}
////////////////////////////////////////////////////////////////////////
#pragma mark - NSStream
////////////////////////////////////////////////////////////////////////
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
if (aStream == self.outputStream) {
if (eventCode == NSStreamEventHasSpaceAvailable) {
if ([self sendDataChunk]) {
self.hasSpaceAvailable = NO;
} else {
self.hasSpaceAvailable = YES;
}
} else if (eventCode == NSStreamEventErrorOccurred) {
NSLog(@"STREAM ERROR %@", aStream.streamError);
}
}
}
@end