Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added version of async file reading helper code that writes to an NSD…

…ata instead of an NSString.
  • Loading branch information...
commit 83045d2332f8d3ef691e0e163371c2aaa8945531 1 parent c882134
Uli Kusterer authored
2  NSFileHandle+AppendToStringAndNotify.h
@@ -33,5 +33,7 @@
33 33 // endSelector must be of the form: -(void) dataReadFrom: (NSFileHandle*)sender finished: (BOOL)finished;
34 34 -(void) readDataToEndOfFileIntoString: (NSMutableString*)str endSelector: (SEL)sel
35 35 delegate: (id)del;
  36 +-(void) readDataToEndOfFileIntoData: (NSMutableData*)theData endSelector: (SEL)sel
  37 + delegate: (id)del;
36 38
37 39 @end
37 NSFileHandle+AppendToStringAndNotify.m
@@ -62,6 +62,29 @@ -(void) readDataToEndOfFileIntoString: (NSMutableString*)str endSelector: (SEL)s
62 62 }
63 63
64 64
  65 +-(void) readDataToEndOfFileIntoData: (NSMutableData*)theData endSelector: (SEL)sel
  66 + delegate: (id)del
  67 +{
  68 + if( !gUKAppendAndNotifyData )
  69 + gUKAppendAndNotifyData = [[NSMutableArray alloc] init];
  70 +
  71 + [gUKAppendAndNotifyData addObject: [NSDictionary dictionaryWithObjectsAndKeys:
  72 + [NSValue valueWithNonretainedObject: self], @"object",
  73 + [NSValue valueWithNonretainedObject: del], @"delegate",
  74 + [NSValue valueWithBytes: &sel objCType: @encode(SEL)], @"selector",
  75 + theData, @"outputdata",
  76 + nil] ];
  77 +
  78 + [[NSNotificationCenter defaultCenter] addObserver:self
  79 + selector:@selector(notifyFileHandleReadCompletionForAppendAndNotify:)
  80 + name:NSFileHandleReadCompletionNotification object: self];
  81 + [[NSNotificationCenter defaultCenter] addObserver:self
  82 + selector:@selector(notifyFileHandleReadCompletionForAppendAndNotify:)
  83 + name:NSFileHandleReadToEndOfFileCompletionNotification object: self];
  84 + [self readInBackgroundAndNotify];
  85 +}
  86 +
  87 +
65 88 @end
66 89
67 90
@@ -84,8 +107,8 @@ -(NSDictionary*) infoDictionaryForAppendAndNotify
84 107 -(void) notifyFileHandleReadCompletionForAppendAndNotify: (NSNotification*) notification
85 108 {
86 109 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
87   - NSData* data;
88   - NSDictionary* info = [self infoDictionaryForAppendAndNotify];
  110 + NSData* data = nil;
  111 + NSDictionary* info = [self infoDictionaryForAppendAndNotify];
89 112
90 113 // Set up callback:
91 114 SEL sel = nil;
@@ -104,8 +127,14 @@ -(void) notifyFileHandleReadCompletionForAppendAndNotify: (NSNotification*) noti
104 127 if( data && [data length] ) // Still data left:
105 128 {
106 129 // Append data:
107   - NSString* dataStr = [[[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding] autorelease];
108   - [[info objectForKey: @"outputstring"] appendString: dataStr];
  130 + NSMutableString* outStr = [info objectForKey: @"outputstring"];
  131 + if( outStr )
  132 + {
  133 + NSString* dataStr = [[[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding] autorelease];
  134 + [outStr appendString: dataStr];
  135 + }
  136 + else
  137 + [[info objectForKey: @"outputdata"] appendData: data];
109 138
110 139 // Actually send notification:
111 140 [inv setArgument: &finished atIndex:3];

0 comments on commit 83045d2

Please sign in to comment.
Something went wrong with that request. Please try again.