Download a large file in an iOS application

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 {
  // find the directory I need to write to and the name of the file
NSString *currentChecksum = [WebServiceManager MD5CheckSumForNSData:sequencePacketData];
    BOOL checkSumValid = [dmgr ValidateChecksum:currentChecksum againstFileName:self.CurrentFileName];
    self.IsSuccessful = checkSumValid;

    if (!checkSumValid) {
        // log error msg
        return;
    }

    if (success)
    {
        NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:sequencePath];
        [handle seekToEndOfFile];
        [handle writeData:sequencePacketData];
        [handle closeFile];
    }
    else
    {
        [sequencePacketData writeToFile:sequencePath atomically:YES];
    }

    // When file is completely downloaded, check the checksum of the entire file:
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];

            // log error
            return;
        }
    }
}

+ (NSString*)MD5CheckSumForNSData:(NSData *) input
{
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

    // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(input.bytes, input.length, md5Buffer);

    // Convert unsigned char buffer to NSString of hex values
    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 ). !

+3
1

. , - , , .

ivars:

NSFileHandle* filehandle;
MD5_CTX md5sum;

MD5_CTX OpenSSL, ... iOS? . , MD5 , . : http://people.csail.mit.edu/rivest/Md5.c ( OpenSSL , , . OpenSSL, MD5.)

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    MD5Init(&md5sum);

    filehandle = [[NSFileHandle filehandleForWritingAtPath:path] retain];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    MD5Update(&md5sum, [data bytes], [data length]);

    [filehandle writeData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    MD5Final(&md5sum);
    // MD5 sum is in md5sum.digest[]

    [filehandle closeFile];

    // verify MD5 sum, etc..
}

, MD5, - .

+6

All Articles