這個v8.deserializer.readHeader()方法用於讀取和驗證標頭。它還檢查格式版本。遇到無效或不受支持的有線格式時會引發錯誤。
用法
v8.deserializer.readHeader()
參數
由於它從內部緩衝區讀取標頭,因此不需要任何輸入參數。反序列化標頭後,它將返回 True。
Example 1
創建一個文件名"readHeader.js"並複製以下代碼片段。創建文件後,使用命令“node readHeader.js”運行此代碼。
// v8.deserializer.readHeader() Demo Example
// Importing the v8 module
const v8 = require('v8');
// Defining the serializer object
const serializer = new v8.Serializer();
// Writing the headers to the internal buffer
serializer.writeHeader();
// Defining the deserializer object
const deserializer = new v8.Deserializer(serializer.releaseBuffer());
// Reading the headers from internal buffer
console.log(deserializer.readHeader());
// Printing the header values from internal buffer
console.log(serializer.releaseBuffer());
輸出
C:\home\node>> node readHeader.js undefined <Buffer ff 0d 22 1a 57 65 6c 63 6f 6d 65 20 74 6f 20 54 75 74 6f 72 69 61 6c 73 20 50 6f 69 6e 74>
Example 2
讓我們再看一個例子
// v8.serializer.readHeader() Demo Example
// Importing the v8 module
const v8 = require('v8');
// Defining the serializer object
const serializer = new v8.Serializer();
// Writing some value using writeUint32
console.log(serializer.writeUint32(7869));
// Writing the headers to the internal buffer
serializer.writeHeader();
// Defining the deserializer object
const deserializer = new
v8.Deserializer(serializer.releaseBuffer());
// Reading the headers from internal buffer
console.log(deserializer.readHeader());
// Printing the header values from internal buffer
console.log(serializer.releaseBuffer());
輸出
C:\home\node>> node readHeader.js undefined /home/mayankaggarwal/mysql-test/util.js:18 console.log(deserializer.readHeader()); ^ Error:Unable to deserialize cloned data due to invalid or unsupported version. at Object. (/home/node/readHeader .js:18:26) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
相關用法
- Node.js v8.deserializer.readRawBytes()用法及代碼示例
- Node.js v8.deserializer.readUint32()用法及代碼示例
- Node.js v8.deserializer.readValue()用法及代碼示例
- Node.js v8.deserializer.readUint64()用法及代碼示例
- Node.js v8.deserialize()用法及代碼示例
- Node.js v8.getHeapSpaceStatistics()用法及代碼示例
- Node.js v8.serializer.writeRawBytes()用法及代碼示例
- Node.js v8.Deserializer.readUint32()用法及代碼示例
- Node.js v8.serializer.releaseBuffer()用法及代碼示例
- Node.js v8.Serializer.writeUint32()用法及代碼示例
- Node.js v8.serializer.writeUint32()用法及代碼示例
- Node.js v8.Serializer.releaseBuffer()用法及代碼示例
- Node.js v8.Serializer.writeUint64()用法及代碼示例
- Node.js v8.serializer.writeUint64()用法及代碼示例
- Node.js v8.cachedDataVersionTag()用法及代碼示例
- Node.js v8.serializer.writeDouble()用法及代碼示例
- Node.js v8.getHeapStatistics()用法及代碼示例
- Node.js v8.Deserializer.readDouble()用法及代碼示例
- Node.js v8.Deserializer.readUint64()用法及代碼示例
- Node.js v8.Serializer.writeRawBytes()用法及代碼示例
注:本文由純淨天空篩選整理自Mayank Agarwal大神的英文原創作品 Node.js – v8.deserializer.readHeader() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。