I am trying to clear some existing code that downloads a large file from the server into chunks, checks the checksum for every 50 packets, then stitches them together. I have some problems to figure out if this is the most efficient way, since now it crashes due to memory problems. If I don't have a checksum, this doesn't seem to crash, but I would prefer to check my files first.
@property (nonatomic, retain) NSMutableData * ReceivedData;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSData *sequencePacketData = [[NSData alloc] initWithData:self.ReceivedData];
[self ProcessPacket:sequencePacketData];
[sequencePacketData release];
[[NSNotificationCenter defaultCenter] postNotificationName:DownloadNotification object:self];
}
- (void)ProcessPacket:(NSData *)sequencePacketData {
NSString *currentChecksum = [WebServiceManager MD5CheckSumForNSData:sequencePacketData];
BOOL checkSumValid = [dmgr ValidateChecksum:currentChecksum againstFileName:self.CurrentFileName];
self.IsSuccessful = checkSumValid;
if (!checkSumValid) {
return;
}
if (success)
{
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:sequencePath];
[handle seekToEndOfFile];
[handle writeData:sequencePacketData];
[handle closeFile];
}
else
{
[sequencePacketData writeToFile:sequencePath atomically:YES];
}
BOOL completeFileCheckSum;
if ([packetFile isEqualToString:@"50.bin"]) {
NSData *temData = [NSData dataWithContentsOfFile:sequencePath];
currentChecksum = [WebServiceManager MD5CheckSumForNSData:temData];
completeFileCheckSum = [dmgr ValidateChecksum:currentChecksum againstFileName:fileName];
NSLog(@"Checksum for whole file is valid: %i", completeFileCheckSum);
if (!completeFileCheckSum) {
NSError *err;
[fileManager removeItemAtPath:sequencePath error:&err];
return;
}
}
}
+ (NSString*)MD5CheckSumForNSData:(NSData *) input
{
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(input.bytes, input.length, md5Buffer);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
}
Checksum The checkField method simply grabs the checksum from the temp file and compares it.
NSAutoReleasePools , .., , , - ( 1 ). !