Skip to content
Browse files

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

…ata instead of an NSString.
  • Loading branch information...
1 parent c882134 commit 83045d2332f8d3ef691e0e163371c2aaa8945531 @uliwitness committed Jul 10, 2010
Showing with 35 additions and 4 deletions.
  1. +2 −0 NSFileHandle+AppendToStringAndNotify.h
  2. +33 −4 NSFileHandle+AppendToStringAndNotify.m
View
2 NSFileHandle+AppendToStringAndNotify.h
@@ -33,5 +33,7 @@
// endSelector must be of the form: -(void) dataReadFrom: (NSFileHandle*)sender finished: (BOOL)finished;
-(void) readDataToEndOfFileIntoString: (NSMutableString*)str endSelector: (SEL)sel
delegate: (id)del;
+-(void) readDataToEndOfFileIntoData: (NSMutableData*)theData endSelector: (SEL)sel
+ delegate: (id)del;
@end
View
37 NSFileHandle+AppendToStringAndNotify.m
@@ -62,6 +62,29 @@ -(void) readDataToEndOfFileIntoString: (NSMutableString*)str endSelector: (SEL)s
}
+-(void) readDataToEndOfFileIntoData: (NSMutableData*)theData endSelector: (SEL)sel
+ delegate: (id)del
+{
+ if( !gUKAppendAndNotifyData )
+ gUKAppendAndNotifyData = [[NSMutableArray alloc] init];
+
+ [gUKAppendAndNotifyData addObject: [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSValue valueWithNonretainedObject: self], @"object",
+ [NSValue valueWithNonretainedObject: del], @"delegate",
+ [NSValue valueWithBytes: &sel objCType: @encode(SEL)], @"selector",
+ theData, @"outputdata",
+ nil] ];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(notifyFileHandleReadCompletionForAppendAndNotify:)
+ name:NSFileHandleReadCompletionNotification object: self];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(notifyFileHandleReadCompletionForAppendAndNotify:)
+ name:NSFileHandleReadToEndOfFileCompletionNotification object: self];
+ [self readInBackgroundAndNotify];
+}
+
+
@end
@@ -84,8 +107,8 @@ -(NSDictionary*) infoDictionaryForAppendAndNotify
-(void) notifyFileHandleReadCompletionForAppendAndNotify: (NSNotification*) notification
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
- NSData* data;
- NSDictionary* info = [self infoDictionaryForAppendAndNotify];
+ NSData* data = nil;
+ NSDictionary* info = [self infoDictionaryForAppendAndNotify];
// Set up callback:
SEL sel = nil;
@@ -104,8 +127,14 @@ -(void) notifyFileHandleReadCompletionForAppendAndNotify: (NSNotification*) noti
if( data && [data length] ) // Still data left:
{
// Append data:
- NSString* dataStr = [[[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding] autorelease];
- [[info objectForKey: @"outputstring"] appendString: dataStr];
+ NSMutableString* outStr = [info objectForKey: @"outputstring"];
+ if( outStr )
+ {
+ NSString* dataStr = [[[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding] autorelease];
+ [outStr appendString: dataStr];
+ }
+ else
+ [[info objectForKey: @"outputdata"] appendData: data];
// Actually send notification:
[inv setArgument: &finished atIndex:3];

0 comments on commit 83045d2

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