PageRenderTime 44ms CodeModel.GetById 2ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Networking/GDataProgressMonitorInputStream.m

http://macfuse.googlecode.com/
Objective C | 263 lines | 180 code | 53 blank | 30 comment | 14 complexity | 0d196baafddf9f95650fc180e41368a1 MD5 | raw file
  1/* Copyright (c) 2007 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16#import "GDataProgressMonitorInputStream.h"
 17
 18@interface GDataProgressMonitorInputStream (PrivateMethods)
 19- (void)invokeReadSelectorWithBuffer:(NSData *)data;
 20@end
 21
 22@implementation GDataProgressMonitorInputStream
 23
 24// we'll forward all unhandled messages to the NSInputStream class
 25// or to the encapsulated input stream.  This is needed
 26// for all messages sent to NSInputStream which aren't
 27// handled by our superclass; that includes various private run
 28// loop calls.
 29+ (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
 30  return [NSInputStream methodSignatureForSelector:selector];
 31}
 32
 33+ (void)forwardInvocation:(NSInvocation*)invocation {
 34  [invocation invokeWithTarget:[NSInputStream class]];
 35}
 36
 37- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
 38  return [inputStream_ methodSignatureForSelector:selector];
 39}
 40
 41- (void)forwardInvocation:(NSInvocation*)invocation {
 42  [invocation invokeWithTarget:inputStream_];
 43}
 44
 45#pragma mark -
 46
 47+ (id)inputStreamWithStream:(NSInputStream *)input
 48                     length:(unsigned long long)length {
 49
 50  return [[[self alloc] initWithStream:input
 51                                        length:length] autorelease];
 52}
 53
 54- (id)initWithStream:(NSInputStream *)input
 55              length:(unsigned long long)length {
 56
 57  if ((self = [super init]) != nil) {
 58
 59    inputStream_ = [input retain];
 60    dataSize_ = length;
 61
 62    thread_ = [[NSThread currentThread] retain];
 63  }
 64  return self;
 65}
 66
 67- (id)init {
 68  return [self initWithStream:nil length:0];
 69}
 70
 71- (void)dealloc {
 72  [inputStream_ release];
 73  [thread_ release];
 74  [runLoopModes_ release];
 75  [super dealloc];
 76}
 77
 78#pragma mark -
 79
 80- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
 81
 82  NSInteger numRead = [inputStream_ read:buffer maxLength:len];
 83
 84  if (numRead > 0) {
 85
 86    numBytesRead_ += numRead;
 87
 88    BOOL isOnOriginalThread = [thread_ isEqual:[NSThread currentThread]];
 89
 90    if (monitorDelegate_) {
 91
 92      if (monitorSelector_) {
 93        // call the monitor delegate with the number of bytes read and the
 94        // total bytes read
 95        NSMethodSignature *signature = [monitorDelegate_ methodSignatureForSelector:monitorSelector_];
 96        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
 97        [invocation setSelector:monitorSelector_];
 98        [invocation setTarget:monitorDelegate_];
 99        [invocation setArgument:&self atIndex:2];
100        [invocation setArgument:&numBytesRead_ atIndex:3];
101        [invocation setArgument:&dataSize_ atIndex:4];
102
103        if (isOnOriginalThread) {
104          [invocation invoke];
105        } else if (runLoopModes_) {
106          [invocation performSelector:@selector(invoke)
107                             onThread:thread_
108                           withObject:nil
109                        waitUntilDone:NO
110                                modes:runLoopModes_];
111        } else {
112          [invocation performSelector:@selector(invoke)
113                             onThread:thread_
114                           withObject:nil
115                        waitUntilDone:NO];
116        }
117      }
118
119      if (readSelector_) {
120        // call the read selector with the buffer and number of bytes actually
121        // read into it
122        SEL sel = @selector(invokeReadSelectorWithBuffer:);
123
124        if (isOnOriginalThread) {
125          // invoke immediately
126          NSData *data = [NSData dataWithBytesNoCopy:buffer
127                                              length:numRead
128                                        freeWhenDone:NO];
129          [self performSelector:sel withObject:data];
130        } else {
131          // copy the buffer into an NSData to be retained by the
132          // performSelector, and invoke on the proper thread
133          NSData *data = [NSData dataWithBytes:buffer length:numRead];
134          if (runLoopModes_) {
135            [self performSelector:sel
136                         onThread:thread_
137                       withObject:data
138                    waitUntilDone:NO
139                            modes:runLoopModes_];
140          } else {
141            [self performSelector:sel
142                         onThread:thread_
143                       withObject:data
144                    waitUntilDone:NO];
145          }
146        }
147      }
148    }
149  }
150
151  return numRead;
152}
153
154- (void)invokeReadSelectorWithBuffer:(NSData *)data {
155  const void *buffer = [data bytes];
156  unsigned long long length = [data length];
157
158  NSMethodSignature *signature = [monitorDelegate_ methodSignatureForSelector:readSelector_];
159  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
160  [invocation setSelector:readSelector_];
161  [invocation setTarget:monitorDelegate_];
162  [invocation setArgument:&self atIndex:2];
163  [invocation setArgument:&buffer atIndex:3];
164  [invocation setArgument:&length atIndex:4];
165  [invocation invoke];
166}
167
168- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
169  return [inputStream_ getBuffer:buffer length:len];
170}
171
172- (BOOL)hasBytesAvailable {
173  return [inputStream_ hasBytesAvailable];
174}
175
176#pragma mark Standard messages
177
178// Pass expected messages to our encapsulated stream.
179//
180// We want our encapsulated NSInputStream to handle the standard messages;
181// we don't want the superclass to handle them.
182- (void)open {
183  [inputStream_ open];
184}
185
186- (void)close {
187  [inputStream_ close];
188}
189
190- (id)delegate {
191  return [inputStream_ delegate];
192}
193
194- (void)setDelegate:(id)delegate {
195  [inputStream_ setDelegate:delegate];
196}
197
198- (id)propertyForKey:(NSString *)key {
199  return [inputStream_ propertyForKey:key];
200}
201- (BOOL)setProperty:(id)property forKey:(NSString *)key {
202  return [inputStream_ setProperty:property forKey:key];
203}
204
205- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
206  [inputStream_ scheduleInRunLoop:aRunLoop forMode:mode];
207}
208- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
209  [inputStream_ removeFromRunLoop:aRunLoop forMode:mode];
210}
211
212- (NSStreamStatus)streamStatus {
213  return [inputStream_ streamStatus];
214}
215
216- (NSError *)streamError {
217  return [inputStream_ streamError];
218}
219
220#pragma mark Setters and getters
221
222- (void)setMonitorDelegate:(id)monitorDelegate {
223  monitorDelegate_ = monitorDelegate; // non-retained
224}
225
226- (id)monitorDelegate {
227  return monitorDelegate_;
228}
229
230- (void)setMonitorSelector:(SEL)monitorSelector {
231  monitorSelector_ = monitorSelector;
232}
233
234- (SEL)monitorSelector {
235  return monitorSelector_;
236}
237
238- (void)setReadSelector:(SEL)readSelector {
239  readSelector_ = readSelector;
240}
241
242- (SEL)readSelector {
243  return readSelector_;
244}
245
246- (void)setMonitorSource:(id)source {
247  monitorSource_ = source;  // non-retained
248}
249
250- (id)monitorSource {
251  return monitorSource_;
252}
253
254- (NSArray *)runLoopModes {
255  return runLoopModes_;
256}
257
258- (void)setRunLoopModes:(NSArray *)modes {
259  [runLoopModes_ autorelease];
260  runLoopModes_ = [modes retain];
261}
262
263@end