aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMProgressMonitorInputStream.m
blob: 2336268080926b6df4534c6ad6664885c61192de (plain)
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
//
//  GTMProgressMonitorInputStream.m
//
//  Copyright 2007-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMProgressMonitorInputStream.h"
#import "GTMDefines.h"
#import "GTMDebugSelectorValidation.h"

@implementation GTMProgressMonitorInputStream

// we'll forward all unhandled messages to the NSInputStream class
// or to the encapsulated input stream.  This is needed
// for all messages sent to NSInputStream which aren't
// handled by our superclass; that includes various private run
// loop calls.
+ (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
  return [NSInputStream methodSignatureForSelector:selector];
}

+ (void)forwardInvocation:(NSInvocation*)invocation {  
  [invocation invokeWithTarget:[NSInputStream class]];
}

- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
  return [inputStream_ methodSignatureForSelector:selector];
}

- (void)forwardInvocation:(NSInvocation*)invocation {    
  [invocation invokeWithTarget:inputStream_];
}

#pragma mark -

+ (id)inputStreamWithStream:(NSInputStream *)input 
                     length:(unsigned long long)length {
  
  return [[[self alloc] initWithStream:input 
                                length:length] autorelease];
}

- (id)initWithStream:(NSInputStream *)input 
              length:(unsigned long long)length {
  
  if ((self = [super init]) != nil) {
    
    inputStream_ = [input retain];
    dataSize_ = length;
  }
  return self;
}

- (id)init {
  _GTMDevAssert(NO, @"should call initWithStream:length:");
  return nil;
}

- (void)dealloc {
  [inputStream_ release];
  [super dealloc]; 
}

#pragma mark -


- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {

  NSInteger numRead = [inputStream_ read:buffer maxLength:len];
  
  if (numRead > 0) {
    
    numBytesRead_ += numRead;
    
    if (monitorDelegate_ && monitorSelector_) {
      
      // call the monitor delegate with the number of bytes read and the
      // total bytes read
      
      NSMethodSignature *signature = [monitorDelegate_ methodSignatureForSelector:monitorSelector_];
      NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
      [invocation setSelector:monitorSelector_];
      [invocation setTarget:monitorDelegate_];
      [invocation setArgument:&self atIndex:2];
      [invocation setArgument:&numBytesRead_ atIndex:3];
      [invocation setArgument:&dataSize_ atIndex:4];
      [invocation invoke];
    }
  }
  return numRead;
}

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
  return [inputStream_ getBuffer:buffer length:len];
}

- (BOOL)hasBytesAvailable {
  return [inputStream_ hasBytesAvailable];
}

#pragma mark Standard messages

// Pass expected messages to our encapsulated stream.
//
// We want our encapsulated NSInputStream to handle the standard messages;
// we don't want the superclass to handle them.
- (void)open {
  [inputStream_ open]; 
}

- (void)close {
  [inputStream_ close]; 
}

- (id)delegate {
  return [inputStream_ delegate]; 
}

- (void)setDelegate:(id)delegate {
  [inputStream_ setDelegate:delegate];
}

- (id)propertyForKey:(NSString *)key {
  return [inputStream_ propertyForKey:key]; 
}
- (BOOL)setProperty:(id)property forKey:(NSString *)key {
  return [inputStream_ setProperty:property forKey:key]; 
}

- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
  [inputStream_ scheduleInRunLoop:aRunLoop forMode:mode]; 
}
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
  [inputStream_ removeFromRunLoop:aRunLoop forMode:mode]; 
}

- (NSStreamStatus)streamStatus {
  return [inputStream_ streamStatus]; 
}

- (NSError *)streamError {
  return [inputStream_ streamError];
}

#pragma mark Setters and getters

- (void)setMonitorDelegate:(id)monitorDelegate
                  selector:(SEL)monitorSelector {
  monitorDelegate_ = monitorDelegate; // non-retained
  monitorSelector_ = monitorSelector; 
  GTMAssertSelectorNilOrImplementedWithArguments(monitorDelegate,
                                                 monitorSelector,
                                                 @encode(GTMProgressMonitorInputStream *),
                                                 @encode(unsigned long long),
                                                 @encode(unsigned long long),
                                                 NULL);
}

- (id)monitorDelegate {
  return monitorDelegate_; 
}

- (SEL)monitorSelector {
  return monitorSelector_;
}

- (void)setMonitorSource:(id)source {
  monitorSource_ = source;  // non-retained
}

- (id)monitorSource {
  return monitorSource_; 
}

@end