Re: minisphere 3.0.1
Reply #1084 –
Following up on the above, this is the API I went with:
There are no separate methods for int16, int32, etc. Instead you have read/writeInt() and read/WriteUInt(), which take an argument specifying the number of bytes (1-6) used to encode the value. I got this idea from the Node.js Buffer API, which is actually quite a bit more powerful--or at least, less confusing--than the ArrayBuffer/TypedArray/DataView mess in ES6. In fact I actually adapted the variable-length integer encoding/decoding code from the Node.js source. It rather surprised me that Node is MIT-licensed (which allowed me to borrow code)--I was expecting LGPL or similar.
The full list of FileStream read methods is thus (with a matching set of write methods):
- read - ArrayBuffer
- readDouble - double/float64
- readFloat - float32
- readInt - sized int
- readString - string (utf-8)
- readUInt - sized unsigned int
So if you wanted to read a 16-bit little-endian integer from a file, you'd do:
/* readInt(num_bytes[, little_endian = false]); */
var num_pigs = file.readInt(2, true); // 16-bit = 2 bytes