当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js v8.deserializer.readRawBytes()用法及代码示例


这个v8.deserializer.readRawBytes()方法用于从解串器的内部缓冲区读取原始字节。它本质上返回缓冲区而不反序列化它。输入参数 ‘length’ 指的是在将原始字节写入同一内​​部缓冲区时传递的缓冲区长度。

用法

v8.deserializer.readRawBytes(length)

参数

它需要一个参数 -

  • length- 此参数接受传递给的缓冲区长度的输入serializer.writeRawBytes()

Example 1

创建一个文件名"readRawBytes.js"并复制以下代码片段。创建文件后,使用命令“node readRawBytes.js”运行此代码。

// v8.serializer.readRawBytes() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Initializing the serializer object
const serializer = new v8.Serializer();

// Wriitng the data to the internal buffer
serializer.writeRawBytes(v8.serialize(12345));

// Initializing the serializer object
const deserializer = new
v8.Deserializer(serializer.releaseBuffer());

// Reading the raw bytes data from internal buffer
console.log(deserializer.readRawBytes(6));

输出

C:\home\node>> node readRawBytes.js
<Buffer ff 0d 49 f2 c0 01>

Example 2

让我们再看一个例子

// v8.serializer.readRawBytes() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Initializing the serializer object
const serializer = new v8.Serializer();
var buffer;

// Wriitng the data to the internal buffer
function writeRawBytes(data, len) {
   serializer.writeRawBytes(v8.serialize(data));
   buffer = serializer.releaseBuffer();
   console.log("Data written & Retrieved:")
   console.log(buffer)
   readRawBytes(len);
}

// Reading the raw bytes data from internal buffer
function readRawBytes(len) {
   const deserializer = new v8.Deserializer(buffer);
   console.log(deserializer.readRawBytes(len));
}

writeRawBytes(9876, 6)
writeRawBytes('Welcome to TutorialsPoint', 29)
writeRawBytes(12345, 6)

输出

C:\home\node>> node readRawBytes.js
Data written & Retrieved:
   <Buffer ff 0d 49 a8 9a 01>
   <Buffer ff 0d 49 a8 9a 01>
Data written & Retrieved:
   <Buffer ff 0d 22 19 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f
   72 69 61 6c 73 50 6f 69 6e 74>
   <Buffer ff 0d 22 19 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f
   72 69 61 6c 73 50 6f 69 6e 74>
Data written & Retrieved:
   <Buffer ff 0d 49 f2 c0 01>
   <Buffer ff 0d 49 f2 c0 01>

相关用法


注:本文由纯净天空筛选整理自Mayank Agarwal大神的英文原创作品 Node.js – v8.deserializer.readRawBytes() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。