/
HSRandomDataInputStream.m
159 lines (118 loc) · 3.7 KB
/
HSRandomDataInputStream.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
//
// HSRandomDataInputStream.m
// HSCountingInputStream
//
// Created by BJ Homer on 5/7/11.
// Copyright 2011 BJ Homer. All rights reserved.
//
#import "HSRandomDataInputStream.h"
@implementation HSRandomDataInputStream
{
NSStreamStatus streamStatus;
id <NSStreamDelegate> delegate;
CFReadStreamClientCallBack copiedCallback;
CFStreamClientContext copiedContext;
CFOptionFlags requestedEvents;
}
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
streamStatus = NSStreamStatusNotOpen;
[self setDelegate:self];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
#pragma mark - NSStream subclass overrides
- (void)open {
streamStatus = NSStreamStatusOpen;
}
- (void)close {
streamStatus = NSStreamStatusClosed;
}
- (id<NSStreamDelegate>)delegate {
return delegate;
}
- (void)setDelegate:(id<NSStreamDelegate>)aDelegate {
delegate = aDelegate;
if (delegate == nil) {
delegate = self;
}
}
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
// Nothing to do here, because this stream does not need a run loop to produce its data.
}
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
// Nothing to do here, because this stream does not need a run loop to produce its data.
}
- (id)propertyForKey:(NSString *)key {
return nil;
}
- (BOOL)setProperty:(id)property forKey:(NSString *)key {
return NO;
}
- (NSStreamStatus)streamStatus {
return streamStatus;
}
- (NSError *)streamError {
return nil;
}
#pragma mark - NSInputStream subclass overrides
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
for (NSUInteger i=0; i<len; ++i) {
buffer[i] = arc4random() & 0x00000000ffffffff;
}
if (CFReadStreamGetStatus((CFReadStreamRef)self) == kCFStreamStatusOpen) {
double delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (copiedCallback && (requestedEvents & kCFStreamEventHasBytesAvailable)) {
copiedCallback((CFReadStreamRef)self, kCFStreamEventHasBytesAvailable, &copiedContext);
}
});
}
return len;
}
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
// Not appropriate for this kind of stream; return NO.
return NO;
}
- (BOOL)hasBytesAvailable {
// There are always bytes available.
return YES;
}
#pragma mark - Undocumented CFReadStream bridged methods
- (void)_scheduleInCFRunLoop:(CFRunLoopRef)aRunLoop forMode:(CFStringRef)aMode {
// Nothing to do here, because this stream does not need a run loop to produce its data.
}
- (BOOL)_setCFClientFlags:(CFOptionFlags)inFlags
callback:(CFReadStreamClientCallBack)inCallback
context:(CFStreamClientContext *)inContext {
if (inCallback != NULL) {
requestedEvents = inFlags;
copiedCallback = inCallback;
memcpy(&copiedContext, inContext, sizeof(CFStreamClientContext));
if (copiedContext.info && copiedContext.retain) {
copiedContext.retain(copiedContext.info);
}
copiedCallback((CFReadStreamRef)self, kCFStreamEventHasBytesAvailable, &copiedContext);
}
else {
requestedEvents = kCFStreamEventNone;
copiedCallback = NULL;
if (copiedContext.info && copiedContext.release) {
copiedContext.release(copiedContext.info);
}
memset(&copiedContext, 0, sizeof(CFStreamClientContext));
}
return YES;
}
- (void)_unscheduleFromCFRunLoop:(CFRunLoopRef)aRunLoop forMode:(CFStringRef)aMode {
// Nothing to do here, because this stream does not need a run loop to produce its data.
}
@end