I am trying to read a binary file byte by byte in Node using fs.open and FileHandle. Using FileHandle.read(buffer, offset, length, position), I can either:
- Leave the
positionargument blank, in which case every chunk of data read moves theFileHandle's internal position - Supply a
positionargument, in which case the internal position will remain unchanged
I would like to read a value from a specific position in the middle of the file (using FileHandle.read with a position), and based on that value, sequentially read some other data from another point in the file (using FileHandle.read with no position).
The first part is easy. For the second part, I know that I can:
- Maintain a custom position variable and supply it to every
readcall - Read a certain amount of data from the file just to change the internal position
The first solution is workable but a bit of a hassle, as the structure of that data is quite complicated. The second solution is not really viable as the files get rather large, and the whole point of reading the file byte by byte was to not read large chunks of its content into memory.
I can also build my own abstraction over FileHandle, one which would maintain its own internal position and allow me to alter it, but I'd rather avoid that if I can.
Is there a method that would allow me to skip an arbitrary amount of bytes in a FileHandle, or to manually set its internal position?