我正在尝试清理一些现有的代码,这些代码从服务器中分块下载一个大文件,检查每 50 个数据包的校验和,然后将它们拼接在一起。我在查看它是否是最有效的方法时遇到了一些麻烦,因为现在它由于内存问题而崩溃了一段时间。如果我没有校验和,它似乎不会崩溃,但我希望我可以先检查我的文件。
@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;
}
check checksum againstFile 方法只是从临时文件中获取校验和并进行比较。
我读到了 NSAutoReleasePools 以及如果您正在加载一堆图像并需要清除内存等,这将如何提供帮助,但我不确定这是否真的适用于此,或者其他任何东西都可以帮助下载大文件(略小于 1 GB)。谢谢!