I'm using NSTask
to run an external utility which returns a long string of data. The problem is that when the returned string exceeds a large amount of data (around 32759 chars) it becomes null
or truncates the returned string. How do I return the full output?
NSTask *myTask = [[NSTask alloc] init];
[myTask setLaunchPath:myExternalCommand];
[myTask setArguments:[NSArray arrayWithObjects: arg1, arg2, nil]];
NSPipe *pipe = [NSPipe pipe];
[myTask setStandardOutput:pipe];
NSFileHandle *taskHandle;
taskHandle = [pipe fileHandleForReading];
[myTask launch];
[myTask waitUntilExit];
NSData *taskData;
taskData = [taskHandle readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:taskData
encoding:NSUTF8StringEncoding];
NSLog(@"Output: \n%@", outputString);
// (null or truncated) when stdout exceeds x amount of stdout
To test the functionality use cat
or similar on a large file for the myExternalCommand
. The issue seems to happen right after the character length of 32759...
solution? I'm not sure, but what might need to happen is to somehow read the return stdout
in chunks, then append the outputString
data if possible.
update: I tried moving waitUntilExit
after readDataToEndOfFile
per suggestion, but it did not affect the outcome.
*please note, I'm looking for an
Obj-C
solution, thanks.