Re: NSTask and uudecode
Re: NSTask and uudecode
- Subject: Re: NSTask and uudecode
- From: "Stephen J. Butler" <email@hidden>
- Date: Wed, 27 Jan 2010 11:58:30 -0600
On Wed, Jan 27, 2010 at 11:37 AM, gMail.com <email@hidden> wrote:
> I have to decode a uuencoded NSString in memory so I use NSTask to launch
> the unix command uudecode. If I set the output to a file, it works. But if I
> setStandardOutput to a NSFileHandle I cannot later get any availableData.
> Can you please tell me why?
>
> // -p should set the standard output. Even without this args, it fails.
> NSArray *args = [NSArray arrayWithObjects:@"-p", nil];
> [task setLaunchPath:@"/usr/bin/uudecode"];
> [task setArguments:args];
>
> I set the Standard input to the handle managing the string
>
> NSPipe *writePipe = [NSPipe pipe];
> NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
> [task setStandardInput:writePipe];
> [writeHandle writeData:[uuencodedString
> dataUsingEncoding:NSMacOSRomanStringEncoding]];
> [writeHandle closeFile];
>
> I set a StandardOutput to a readHandle
>
> NSPipe *readPipe = [NSPipe pipe];
> NSFileHandle *readHandle = [readPipe fileHandleForReading];
> [task setStandardOutput:readHandle];
>
> Then I launch and wait
> [task launch];
> [task waitUntilExit];
>
> I cannot later get any data with
>
> NSData *dataOut = [readHandle availableData];
>
> The application doesn't go further. It remains blocked here.
> What do I miss?
Pipes are implemented as small buffers in the kernel. We're talking
only a few KB. If the data you're trying to write/read is over that
size then the uudecode process will block waiting for you to read data
from the pipe. You might not even be writing all the data. Consider
this sequence of events with an 8KB buffer size:
You: write 8KB
Program: read 8KB
You: write 8KB
Program: process
Program: write 8KB
Now we're blocked. You can't write any more data because Program is
blocking trying to send 8KB back. You have to be careful how you
sequence your events and you'll most likely have to run the runloop.
CocoaDev has a good example of how to wrap a unix process:
<http://www.cocoadev.com/index.pl?WrappingUnixApps>
Follow it exactly unless you know what you're doing.
Another option is to just get an uudecode method in your program.
Shouldn't be hard to find some C source... this has been around
forever.
_______________________________________________
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