Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Huge Chunks of data to NSData objects-iOS

I've a video file about 2GB in size. This header of video file is encrypted (approximately 528 bytes encrypted). In order to Decrypt this video file i'm reading all bytes from the file into an NSData object. As soon as i write this file into NSData object my Application crashes(possibly b'coz max-256MB RAM for iPad).

So how do i go about storing this NSData object into Virtual memory of an iPad/ iPhone temporarily?

Any other approach by means of which i can achieve the same?

like image 221
Pranav Jaiswal Avatar asked Nov 07 '11 17:11

Pranav Jaiswal


1 Answers

Use an NSInputStream to read in the file piece by piece so you aren't loading it all into memory all at once. Specifically you'll want to make use of hasBytesAvailable and read:maxLength:.

Something like:

NSInputStream *myStream = [NSInputStream inputStreamWithFilAtPath:pathToAbsurdlyLargeFile];
[myStream open];
Byte buffer[BUFFER_SIZE];
while ([myStream hasBytesAvailable])
{
   int bytesRead = [myStream read:buffer maxLength:BUFFER_SIZE];
   NSData *myData = [NSData dataWithBytes:buffer length:bytesRead];
   // do other stuff...
}
[myStream close];

Note that you may not need to create an NSData object. You just mentioned you were using it, so I threw it in.

like image 98
Sam Avatar answered Nov 01 '22 07:11

Sam