tux91
tux91

Reputation: 1684

Creating NSData from a file reading it in chunks

A certain piece of code works like this:

  1. Read a file from the disk and store it as NSData in memory
  2. Encrypt (decrypt) it, thus resulting in an additional NSData object of the same size in memory
  3. 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:

  1. Look at the file and split it into 100mb chunks (for example)
  2. Read a chunk
  3. Encrypt (decrypt) it
  4. Append the encrypted chunk to the output file
  5. Throw the original chunk away, so that the whole process takes at most 200mb of RAM

My question is:

  1. Is that possible?
  2. If so, is that the best way to do that?
  3. If so, how would I go about implementing it?

Upvotes: 1

Views: 978

Answers (1)

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

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.

Upvotes: 1

Related Questions