PageRenderTime 33ms CodeModel.GetById 14ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #import "GDataProgressMonitorInputStream.h"
  16. @interface GDataProgressMonitorInputStream (PrivateMethods)
  17. - (void)invokeReadSelectorWithBuffer:(NSData *)data;
  18. @end
  19. @implementation GDataProgressMonitorInputStream
  20. // we'll forward all unhandled messages to the NSInputStream class
  21. // or to the encapsulated input stream. This is needed
  22. // for all messages sent to NSInputStream which aren't
  23. // handled by our superclass; that includes various private run
  24. // loop calls.
  25. + (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
  26. return [NSInputStream methodSignatureForSelector:selector];
  27. }
  28. + (void)forwardInvocation:(NSInvocation*)invocation {
  29. [invocation invokeWithTarget:[NSInputStream class]];
  30. }
  31. - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
  32. return [inputStream_ methodSignatureForSelector:selector];
  33. }
  34. - (void)forwardInvocation:(NSInvocation*)invocation {
  35. [invocation invokeWithTarget:inputStream_];
  36. }
  37. #pragma mark -
  38. + (id)inputStreamWithStream:(NSInputStream *)input
  39. length:(unsigned long long)length {
  40. return [[[self alloc] initWithStream:input
  41. length:length] autorelease];
  42. }
  43. - (id)initWithStream:(NSInputStream *)input
  44. length:(unsigned long long)length {
  45. if ((self = [super init]) != nil) {
  46. inputStream_ = [input retain];
  47. dataSize_ = length;
  48. thread_ = [[NSThread currentThread] retain];
  49. }
  50. return self;
  51. }
  52. - (id)init {
  53. return [self initWithStream:nil length:0];
  54. }
  55. - (void)dealloc {
  56. [inputStream_ release];
  57. [thread_ release];
  58. [runLoopModes_ release];
  59. [super dealloc];
  60. }
  61. #pragma mark -
  62. - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
  63. NSInteger numRead = [inputStream_ read:buffer maxLength:len];
  64. if (numRead > 0) {
  65. numBytesRead_ += numRead;
  66. BOOL isOnOriginalThread = [thread_ isEqual:[NSThread currentThread]];
  67. if (monitorDelegate_) {
  68. if (monitorSelector_) {
  69. // call the monitor delegate with the number of bytes read and the
  70. // total bytes read
  71. NSMethodSignature *signature = [monitorDelegate_ methodSignatureForSelector:monitorSelector_];
  72. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  73. [invocation setSelector:monitorSelector_];
  74. [invocation setTarget:monitorDelegate_];
  75. [invocation setArgument:&self atIndex:2];
  76. [invocation setArgument:&numBytesRead_ atIndex:3];
  77. [invocation setArgument:&dataSize_ atIndex:4];
  78. if (isOnOriginalThread) {
  79. [invocation invoke];
  80. } else if (runLoopModes_) {
  81. [invocation performSelector:@selector(invoke)
  82. onThread:thread_
  83. withObject:nil
  84. waitUntilDone:NO
  85. modes:runLoopModes_];
  86. } else {
  87. [invocation performSelector:@selector(invoke)
  88. onThread:thread_
  89. withObject:nil
  90. waitUntilDone:NO];
  91. }
  92. }
  93. if (readSelector_) {
  94. // call the read selector with the buffer and number of bytes actually
  95. // read into it
  96. SEL sel = @selector(invokeReadSelectorWithBuffer:);
  97. if (isOnOriginalThread) {
  98. // invoke immediately
  99. NSData *data = [NSData dataWithBytesNoCopy:buffer
  100. length:numRead
  101. freeWhenDone:NO];
  102. [self performSelector:sel withObject:data];
  103. } else {
  104. // copy the buffer into an NSData to be retained by the
  105. // performSelector, and invoke on the proper thread
  106. NSData *data = [NSData dataWithBytes:buffer length:numRead];
  107. if (runLoopModes_) {
  108. [self performSelector:sel
  109. onThread:thread_
  110. withObject:data
  111. waitUntilDone:NO
  112. modes:runLoopModes_];
  113. } else {
  114. [self performSelector:sel
  115. onThread:thread_
  116. withObject:data
  117. waitUntilDone:NO];
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return numRead;
  124. }
  125. - (void)invokeReadSelectorWithBuffer:(NSData *)data {
  126. const void *buffer = [data bytes];
  127. unsigned long long length = [data length];
  128. NSMethodSignature *signature = [monitorDelegate_ methodSignatureForSelector:readSelector_];
  129. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  130. [invocation setSelector:readSelector_];
  131. [invocation setTarget:monitorDelegate_];
  132. [invocation setArgument:&self atIndex:2];
  133. [invocation setArgument:&buffer atIndex:3];
  134. [invocation setArgument:&length atIndex:4];
  135. [invocation invoke];
  136. }
  137. - (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
  138. return [inputStream_ getBuffer:buffer length:len];
  139. }
  140. - (BOOL)hasBytesAvailable {
  141. return [inputStream_ hasBytesAvailable];
  142. }
  143. #pragma mark Standard messages
  144. // Pass expected messages to our encapsulated stream.
  145. //
  146. // We want our encapsulated NSInputStream to handle the standard messages;
  147. // we don't want the superclass to handle them.
  148. - (void)open {
  149. [inputStream_ open];
  150. }
  151. - (void)close {
  152. [inputStream_ close];
  153. }
  154. - (id)delegate {
  155. return [inputStream_ delegate];
  156. }
  157. - (void)setDelegate:(id)delegate {
  158. [inputStream_ setDelegate:delegate];
  159. }
  160. - (id)propertyForKey:(NSString *)key {
  161. return [inputStream_ propertyForKey:key];
  162. }
  163. - (BOOL)setProperty:(id)property forKey:(NSString *)key {
  164. return [inputStream_ setProperty:property forKey:key];
  165. }
  166. - (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
  167. [inputStream_ scheduleInRunLoop:aRunLoop forMode:mode];
  168. }
  169. - (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode {
  170. [inputStream_ removeFromRunLoop:aRunLoop forMode:mode];
  171. }
  172. - (NSStreamStatus)streamStatus {
  173. return [inputStream_ streamStatus];
  174. }
  175. - (NSError *)streamError {
  176. return [inputStream_ streamError];
  177. }
  178. #pragma mark Setters and getters
  179. - (void)setMonitorDelegate:(id)monitorDelegate {
  180. monitorDelegate_ = monitorDelegate; // non-retained
  181. }
  182. - (id)monitorDelegate {
  183. return monitorDelegate_;
  184. }
  185. - (void)setMonitorSelector:(SEL)monitorSelector {
  186. monitorSelector_ = monitorSelector;
  187. }
  188. - (SEL)monitorSelector {
  189. return monitorSelector_;
  190. }
  191. - (void)setReadSelector:(SEL)readSelector {
  192. readSelector_ = readSelector;
  193. }
  194. - (SEL)readSelector {
  195. return readSelector_;
  196. }
  197. - (void)setMonitorSource:(id)source {
  198. monitorSource_ = source; // non-retained
  199. }
  200. - (id)monitorSource {
  201. return monitorSource_;
  202. }
  203. - (NSArray *)runLoopModes {
  204. return runLoopModes_;
  205. }
  206. - (void)setRunLoopModes:(NSArray *)modes {
  207. [runLoopModes_ autorelease];
  208. runLoopModes_ = [modes retain];
  209. }
  210. @end