简体   繁体   English

将文件读入 Node.js 中 memory 的特定块

[英]Read a file into a specific block of memory in Node.js

Can you have Node.js read a file as binary into a specific slot of memory somehow?您可以让 Node.js 以某种方式将文件作为二进制文件读取到 memory 的特定插槽中吗?

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?如果不在 Node.js 中,那么浏览器不知何故?

Can you have Node.js read a file as binary into a specific slot of memory somehow?您可以让 Node.js 以某种方式将文件作为二进制文件读取到 memory 的特定插槽中吗?

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.是的, fileHandle.read()fs.read()接口都允许您将缓冲区和偏移量传递到您希望它读取数据的缓冲区中。

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. fs.readFile()接口不提供此功能,因此您必须自己打开文件并将数据自己读入缓冲区,然后关闭文件。 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.如果您统计文件以查看它有多大,您仍然可以通过一次读取操作将整个文件读入缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM