I'm writing a java program where I'm encrypting and decrypting files. I'm treating the files as basically byte arrays - take N bytes, encrypt and write to new files, repeat. And similarly when decrypting.
For this, I am currently copying the entire file into a byte array:
byte[] fileAsBytes = Files.readAllBytes(Paths.get(pathToFile))
But then with larger files, I run out of heap space. (I know I can increase the available memory to the JVM, but that is only dodging the issue. Plus I also have to run this on Android phones, where memory is even more limited).
So, I'm considering to only read N bytes at a time from disk, rather than copy all bytes and then read them from memory.
My question is - what is the best way to do this? I don't need random access, since I'll be reading the files sequentially. Also, how much of a slowdown am I looking at? Because from what I've studied, hard disk access is much slower than memory access.