HI在我的应用程序中我从服务器下载文件,总文件大小超过9GB。下载后1GB数据应用程序崩溃并显示错误为** mach_vm_map(size = 8388608)失败(错误代码= 3)* **错误:无法分配区域。任何人都可以帮助我解决这个问题。
+(void)base64Decode:(NSData *)fData writeToFile:(NSFileHandle *)fHandle
{
unsigned char inBuffer[4] ;
unsigned char outBuffer[3] ;
NSRange part ;
part.location = 0 ;
part.length = 1 ;
NSMutableData *oData = [[NSMutableData alloc] init] ;
BOOL bInProgress = YES ;
int i ;
int total = 0 ;
while ( bInProgress )
{
inBuffer[0] = inBuffer[1] = inBuffer[2] = inBuffer[3] = 0 ;
outBuffer[0] = outBuffer[1] = outBuffer[2] = 0 ;
for ( i = 0 ; i < 4 ; )
{
if ( [fData length] > part.location )
{
[fData getBytes:inBuffer+i range:part] ;
part.location++ ;
if ( ( inBuffer[i] == 0x0d ) || ( inBuffer[i] == 0x0a ) )
continue ;
else
i++ ;
}
else
{
bInProgress = NO ;
break ;
}
}
// if ( i != 4 )
// {
// NSLog ( @"Less\n" ) ;
// }
int unexpect = 0 ;
for ( int j = 0 ; j < i ; j++ )
{
if ( ( inBuffer[j] >= 'A' ) && ( inBuffer[j] <= 'Z' ) )
{
inBuffer[j] -= 'A' ;
}
else
{
if ( ( inBuffer[j] >= 'a' ) && ( inBuffer[j] <= 'z' ) )
{
inBuffer[j] -= 'a' ;
inBuffer[j] += 26 ;
}
else
{
if ( ( inBuffer[j] >= '0' ) && ( inBuffer[j] <= '9' ) )
{
inBuffer[j] -= '0' ;
inBuffer[j] += 52 ;
}
else
{
if ( inBuffer[j] == '+' )
{
inBuffer[j] = 62 ;
}
else
{
if ( inBuffer[j] == '/' )
{
inBuffer[j] = 63 ;
}
else
{
#ifdef DEBUG
NSLog ( @"fHandle Unexpected char: %c: %x\n", inBuffer[j], inBuffer[j] ) ;
#endif
unexpect++ ;
}
}
}
}
}
}
i -= unexpect ;
int len = (i * 6) / 8 ;
if ( ( i * 6 ) % 8 )
len++ ;
total += len ;
outBuffer[0] = ( inBuffer[0] << 2 ) | ( ( inBuffer[1] & 0x30 ) >> 4 );
outBuffer[1] = ( ( inBuffer[1] & 0x0f ) << 4 ) | ( ( inBuffer[2] & 0x3c ) >> 2 );
outBuffer[2] = ( ( inBuffer[2] & 0x03 ) << 6 ) | ( ( inBuffer[3] & 0x3f ) );
[oData appendBytes:outBuffer length:len] ;
if([oData length] > 1024*10)
{
NSError *error;
BOOL dataWritten=[self writeData:oData toFileHandle:fHandle error:&error];
NSLog(@"dataWritten %hhd",dataWritten);
if(oData)
{
[oData release];
oData = nil ;
}
//[oData release];
oData = [[NSMutableData alloc] init] ;
}
}
//fwrite([oData bytes], 1, [oData length], fHandle) ;
//[fHandle writeData:oData] ;
NSError *error;
[self writeData:oData toFileHandle:fHandle error:&error];
if(oData)
{
[oData release];
oData = nil ;
} // NSLog ( @"Total written: %d\n", total ) ;
}
+(BOOL)writeData:(NSData *)data
toFileHandle:(NSFileHandle *)fileHandler
error:(NSError **)error
{
@try
{
[fileHandler writeData:data];
}
@catch (NSException *e)
{
if (error != NULL)
{
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey : @"Failed to write data",
// Other stuff?
};
*error = [NSError errorWithDomain:@"MyStuff" code:123 userInfo:userInfo];
}
return NO;
}
return YES;
}
Here fileHandler is NSFileHandle *pdfPathHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];