Reputation: 217
why is this:
var myArrayBuffer = fs.readFileSync(file, null)
returning an uInt8 array instead of a just a arrayBuffer? why does this seem to work?
var myArrayBuffer = fs.readFileSync(file, null).buffer;
var myAArray = new Uint16Array( myArrayBuffer.slice(266,(sizeofArray*sizeOfArrayElement));
Why would the fs.readFile parse my file into a uInt8 array? Makes no sense, the file has a bunch of different datatypes that are not 1 byte long.
Upvotes: 3
Views: 23460
Reputation: 106385
Because since v3.0.0 Buffer
class inherits from Uint8Array
class. Quoting the doc:
Buffer
instances are alsoUint8Array
instances. However, there are subtle incompatibilities with theTypedArray
specification in ECMAScript 2015. For example, whileArrayBuffer#slice()
creates a copy of the slice, the implementation ofBuffer#slice()
creates a view over the existingBuffer
without copying, makingBuffer#slice()
far more efficient. [...]It is possible to create a new
Buffer
that shares the same allocated memory as aTypedArray
instance by using theTypeArray
object's.buffer
property.
... which is exactly what's done in your example.
Upvotes: 6