I'm trying to compress a file using /usr/bin/compress, launched with no file name so that it takes data from stdin and writes compressed data to stdout. I create the task with pipes for standardInput, Output & Error. In a loop, I read a block of data from the file, write to the task's stdin, then read from stdout using [NSFileHandle availableData]. I've tried block sizes of 2K, 8K and 12K. In all cases, my program just hangs. I am targeting for 10.6

The way my code is structured, I prefer it to block on availableData since I can't do anything until the data arrives and the caller expects return only when processing is complete. So far this is all in the main thread. Once it works, I'll move this part to a separate thread so as not to block the main, and yes, I will then need to make the caller accept an asynchronous completion once the subordinate thread exits.

1 Answer
1

You have no control over how compress works - buffering, relationship of input to output, etc. - so writing a block to it then reading one back is unlikely to work.

You obviously know about threads (you intend to move your code off the main thread later), so just write the data to the pipe in a different thread - you can schedule a concurrent block using GCD, fire up an NSThread, etc. Once you do that you decouple the writing to & reading from the pipes and they can each proceed at their own pace.