Re: NSTask and piped commands
Re: NSTask and piped commands
- Subject: Re: NSTask and piped commands
- From: Alastair Houghton <email@hidden>
- Date: Tue, 18 May 2010 21:34:06 +0100
On 18 May 2010, at 20:33, appledev wrote:
> [task launch];
>
> [task waitUntilExit];
>
> NSData *data;
> result = [file readDataToEndOfFile];
This part is not safe. If the tasks output enough data to fill the pipe buffer (which may be of whatever size the kernel chooses to make it), then your program will hang on the -waitUntilExit line.
You should instead do something like
NSMutableData *result = [NSMutableData data];
NSData *chunk;
while ((chunk = [file availableData]) && [chunk length])
[result appendData:chunk];
[task waitUntilExit];
There are obviously variations on that; if you can process the data as you go, that may be a better way to do it (but it's a bit complicated to do because there's no guarantee that the chunks you read won't straddle individual multi-byte characters).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden