Reputation: 79458
Can you have Node.js read a file as binary into a specific slot of memory somehow?
let buffer = new Uint8Array
let location = 0
let path = 'foo.binary'
fs.readIntoMemoryLocation(path, buffer, location)
Is something like that possible?
I am wondering because I have a bunch of stuff to process in one array buffer, and would like to add a file to it, rather than the file being its own buffer. Without copying the file from one place to another either. If not in Node.js, then the browser somehow?
Upvotes: 1
Views: 266
Reputation: 707976
Can you have Node.js read a file as binary into a specific slot of memory somehow?
Yes, both the fileHandle.read()
and the fs.read()
interfaces allow you to pass in a buffer and offset into that buffer that you want it to read data into.
The fs.readFile()
interface does not provide that so you will have to open the file yourself and read the data yourself into the buffer and then close the file. If you stat the file to see how big it is, you can still read the whole file into the buffer with one read operation.
Upvotes: 2