Node.js – v8.deserializer.readRawBytes() Method


The v8.deserializer.readRawBytes() method is used for reading the raw bytes from the deserializer’s internal buffer. It basically returns the buffer without deserializing it. The input parameter ‘length’ refers to the length of the buffer that was passed while writing the raw bytes to the same internal buffer.

Syntax

v8.deserializer.readRawBytes(length)

Parameters

It takes a single parameter −

  • length − This parameter takes input for the length of the buffer that was passed to serializer.writeRawBytes().

Example 1

Create a file with the name "readRawBytes.js" and copy the following code snippet. After creating the file, use the command "node readRawBytes.js" to run this code.

// 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));

Output

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

Example 2

Let’s have a look at one more example

// 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)

Output

C:\home
ode>> 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>

Updated on: 18-Aug-2021

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements