A certain piece of code works like this:
- Read a file from the disk and store it as NSData in memory
- Encrypt (decrypt) it, thus resulting in an additional NSData object of the same size in memory
- Write the encrypted (decrypted) data to the disk
Now, for files like 10 or 100 mb in size, this works just fine and maintains a consistent rate of processing bytes per second (so processing a 100 mb file will take 10x the 10 mb one). If I go up to say 1.5 gigs, then the system has to keep 2 of those in memory so it starts swapping to disk and that brings the speed down dramatically.
So I thought maybe the following was possible:
- Look at the file and split it into 100mb chunks (for example)
- Read a chunk
- Encrypt (decrypt) it
- Append the encrypted chunk to the output file
- Throw the original chunk away, so that the whole process takes at most 200mb of RAM
My question is:
- Is that possible?
- If so, is that the best way to do that?
- If so, how would I go about implementing it?
What you want to do is entirely possible. Your questions demand an answer that’s a bit lengthy, so I refer you to this web page:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html
Note that the examples in the page deal with files. You can set up a buffer to the size you want and read as many bytes as you want at a time. You’ll even see an opportune place for you to call your encryption routine.
With the NSMutableData instance you create, you can then have it write out to disk using
-writeToFile:atomically:or-writeToURL:atomically:Give it a try, and best wishes to you in your endeavors.