节点 fs.readFileSync returns 一个 uInt8 数组而不是原始缓冲区数组?
Node fs.readFileSync returns a uInt8 array instead of raw buffer array?
这是为什么:
var myArrayBuffer = fs.readFileSync(file, null)
返回一个 uInt8 数组而不是一个 arrayBuffer?为什么这似乎有效?
var myArrayBuffer = fs.readFileSync(file, null).buffer;
var myAArray = new Uint16Array( myArrayBuffer.slice(266,(sizeofArray*sizeOfArrayElement));
为什么 fs.readFile 将我的文件解析为 uInt8 数组?没有意义,文件有一堆不同的数据类型,不是 1 字节长。
因为自 v3.0.0 Buffer
class 继承自 Uint8Array
class。引用 the doc:
Buffer
instances are also Uint8Array
instances. However, there are subtle incompatibilities with the TypedArray
specification in ECMAScript 2015. For example, while ArrayBuffer#slice()
creates a copy of the slice, the implementation of Buffer#slice()
creates a view over the existing Buffer
without copying, making Buffer#slice()
far more efficient. [...]
It is possible to
create a new Buffer
that shares the same allocated memory as a
TypedArray
instance by using the TypeArray
object's .buffer
property.
...这正是您的示例中所做的。
这是为什么:
var myArrayBuffer = fs.readFileSync(file, null)
返回一个 uInt8 数组而不是一个 arrayBuffer?为什么这似乎有效?
var myArrayBuffer = fs.readFileSync(file, null).buffer;
var myAArray = new Uint16Array( myArrayBuffer.slice(266,(sizeofArray*sizeOfArrayElement));
为什么 fs.readFile 将我的文件解析为 uInt8 数组?没有意义,文件有一堆不同的数据类型,不是 1 字节长。
因为自 v3.0.0 Buffer
class 继承自 Uint8Array
class。引用 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.
...这正是您的示例中所做的。