• Node.js Video Tutorials

NodeJS v8.deserializer.readRawBytes() Method



The NodeJs v8.deserializer.readRawBytes() method of class v8.deserializer is used to read raw bytes from the deserializer's internal buffer. This method basically returns the internal buffer without deserializing it.

Syntax

Following is the syntax of the NodeJS v8.deserializer.readRawBytes() method

v8.deserializer.readRawBytes(length)

Parameters

This method accepts only one parameter.

  • length − This parameter specifies the length of the buffer to be read from the deserializer's internal buffer.

Return Value

This method read the raw bytes from the deserializer's internal buffer.

If length parameter is specified, it will return the length of the buffer that was passed to this method.

Example

In the example below, we are trying to write raw bytes to the serializer's internal buffer using v8.serializer.writeRawBytes() method. Then we are trying to read the raw buffer data from deserializer's internal buffer using v8.deserializer.readRawBytes() method.

const v8 = require('v8');
const serializer = new v8.Serializer();
serializer.writeRawBytes(v8.serialize(32435));
let buffer = serializer.releaseBuffer();  
const deserializer = new v8.Deserializer(buffer);
console.log(deserializer.readRawBytes());

Output

Following is the output of the above program −

<Buffer ff 0d 49 e6 fa 03>

Example

In the following example, we are passing a length as a parameter to the readRawBytes() method.

const v8 = require('v8');
const serializer = new v8.Serializer();
serializer.writeRawBytes(v8.serialize(32435));
let buffer = serializer.releaseBuffer();
console.log(buffer); 
const deserializer = new v8.Deserializer(buffer);
console.log(deserializer.readRawBytes(2));

Output

As we can see in the output below, the readRawBytes() method returned only 2 raw bytes from the deserializer's internal buffer because 2 is passed as length.

<Buffer ff 0d 49 e6 fa 03>
<Buffer ff 0d>
nodejs_v8_module.htm
Advertisements