Node.js – v8.deserializer.readRawBytes() 方法
该V8。deserializer.readRawBytes()方法用于从解串器的内部缓冲区读取原始字节。它基本上返回缓冲区而不反序列化它。输入参数“length”指的是在将原始字节写入同一内部缓冲区时传递的缓冲区长度。
语法
v8.deserializer.readRawBytes(length)
参数
它需要一个参数-
length-此参数接受传递给的缓冲区长度的输入。serializer.writeRawBytes()
示例1
创建一个名为“readRawBytes.js”的文件并复制以下代码片段。创建文件后,使用命令“nodereadRawBytes.js”运行此代码。
//v8.serializer.readRawBytes()演示示例 //导入v8模块 const v8 = require('v8'); //初始化序列化器对象 const serializer = new v8.Serializer(); //将数据写入内部缓冲区 serializer.writeRawBytes(v8.serialize(12345)); //初始化序列化器对象 const deserializer = new v8.Deserializer(serializer.releaseBuffer()); //从内部缓冲区读取原始字节数据 console.log(deserializer.readRawBytes(6));输出结果
C:\home\node>> node readRawBytes.js
示例2
让我们再看一个例子
//v8.serializer.readRawBytes()演示示例 //导入v8模块 const v8 = require('v8'); //初始化序列化器对象 const serializer = new v8.Serializer(); var buffer; //将数据写入内部缓冲区 function writeRawBytes(data, len) { serializer.writeRawBytes(v8.serialize(data)); buffer = serializer.releaseBuffer(); console.log("Data written & Retrieved :") console.log(buffer) readRawBytes(len); } //从内部缓冲区读取原始字节数据 function readRawBytes(len) { const deserializer = new v8.Deserializer(buffer); console.log(deserializer.readRawBytes(len)); } writeRawBytes(9876, 6) writeRawBytes('Welcome to nhooo', 29) writeRawBytes(12345, 6)输出结果
C:\home\node>> node readRawBytes.js Data written & Retrieved :Data written & Retrieved : Data written & Retrieved :