writing data to a file handle
writing data to a file handle
- Subject: writing data to a file handle
- From: email@hidden
- Date: Thu, 7 Jun 2001 11:22:45 -0400
I'm having some problems writing data to a file handle. I have a method
in an NSData Category that retuns an encrypted copy of itself. It
encrypts the data using NSTask to call openssl. This method works
perfectly fine for small amounts of data, but when I try larger amounts
of about 100K it hangs when I try to write the data to the input
handle. Typing the same command in the terminal with a 100K file
finishes in under a second, so its not that openssl is taking a long
time reading the data.
Also, if this is a stupid way to encrypt data in Cocoa please tell me.
Here is the method:
- (NSData *)opensslUsingCipher:(NSString *)aCipher password:(NSString
*)aPassword decryption:(BOOL)aShouldDecrypt {
NSData *theData;
NSMutableArray *theArguments;
NSMutableData *theOutputData = [[NSMutableData alloc] init];
NSTask *theTask = [[NSTask alloc] init];
NSPipe *theInputPipe = [NSPipe pipe];
NSPipe *theOutputPipe = [NSPipe pipe];
NSFileHandle *theInputHandle = [theInputPipe fileHandleForWriting];
NSFileHandle *theOutputHandle = [theOutputPipe
fileHandleForReading];
theArguments = [NSArray arrayWithObject:aCipher];
if (aShouldDecrypt)
[theArguments addObject:@"-d"];
[theArguments addObject:@"-salt"];
[theArguments addObject:@"-pass"];
[theArguments addObject:[NSString stringWithFormat:@"pass:%@",
aPassword]];
[theTask setLaunchPath:@"/usr/bin/openssl"];
[theTask setArguments:theArguments];
[theTask setStandardInput:theInputPipe];
[theTask setStandardOutput:theOutputPipe];
[theTask launch];
[theInputHandle write
Data:self]; // HERE IS WHERE IT HANGS
[theInputHandle closeFile];
while ((theData = [theOutputHandle availableData]) && [theData
length])
[theOutputData append
Data:theData];
[theTask waitUntilExit];
if ([theTask terminationStatus] != 0)
theOutputData = nil;
return theOutputData;
}
Thanks,
Edwin