国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

關于Node.js中Buffer的一些你可能不知道的用法

2024-05-06 15:19:16
字體:
來源:轉載
供稿:網友

前言

在大多數介紹 Buffer 的文章中,主要是圍繞數據拼接和內存分配這兩方面的。比如我們使用fs模塊來讀取文件內容的時候,返回的就是一個 Buffer:

fs.readFile('filename', function (err, buf) { // <Buffer 2f 2a 2a 0a 20 2a 20 53 75 ... >});

在使用net或http模塊來接收網絡數據時,data事件的參數也是一個 Buffer,這時我們還需要使用Buffer.concat()來做數據拼接:

var bufs = [];conn.on('data', function (buf) { bufs.push(buf);});conn.on('end', function () { // 接收數據結束后,拼接所有收到的 Buffer 對象 var buf = Buffer.concat(bufs);});

還可以利用Buffer.toString()來做轉換base64或十六進制字符的轉換,比如:

console.log(new Buffer('hello, world!').toString('base64'));// 轉換成 base64 字符串:aGVsbG8sIHdvcmxkIQ==console.log(new Buffer('aGVsbG8sIHdvcmxkIQ==', 'base64').toString());// 還原 base64 字符串:hello, world!console.log(new Buffer('hello, world!').toString('hex'));// 轉換成十六進制字符串:68656c6c6f2c20776f726c6421console.log(new Buffer('68656c6c6f2c20776f726c6421', 'hex').toString());// 還原十六進制字符串:hello, world!

一般情況下,單個 Node.js 進程是有最大內存限制的,以下是來自官方文檔中的說明:

What is the memory limit on a node process?

Currently, by default v8 has a memory limit of 512MB on 32-bit systems, and 1.4GB on 64-bit systems. The limit can be raised by setting --max_old_space_size to a maximum of ~1024 (~1 GB) (32-bit) and ~4096 (~4GB) (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.

由于 Buffer 對象占用的內存空間是不計算在 Node.js 進程內存空間限制上的,因此,我們也常常會使用 Buffer 來存儲需要占用大量內存的數據:

// 分配一個 2G-1 字節的數據// 單次分配內存超過此值會拋出異常 RangeError: Invalid typed array lengthvar buf = new Buffer(1024 * 1024 * 1024 - 1);

以上便是 Buffer 的幾種常見用法。然而,閱讀 Buffer 的 API 文檔時,我們會發現更多的是readXXX()writeXXX()開頭的 API,具體如下:

buf.readUIntLE(offset, byteLength[, noAssert]) buf.readUIntBE(offset, byteLength[, noAssert]) buf.readIntLE(offset, byteLength[, noAssert]) buf.readIntBE(offset, byteLength[, noAssert]) buf.readUInt8(offset[, noAssert]) buf.readUInt16LE(offset[, noAssert]) buf.readUInt16BE(offset[, noAssert]) buf.readUInt32LE(offset[, noAssert]) buf.readUInt32BE(offset[, noAssert]) buf.readInt8(offset[, noAssert]) buf.readInt16LE(offset[, noAssert]) buf.readInt16BE(offset[, noAssert]) buf.readInt32LE(offset[, noAssert]) buf.readInt32BE(offset[, noAssert]) buf.readFloatLE(offset[, noAssert]) buf.readFloatBE(offset[, noAssert]) buf.readDoubleLE(offset[, noAssert]) buf.readDoubleBE(offset[, noAssert]) buf.write(string[, offset][, length][, encoding]) buf.writeUIntLE(value, offset, byteLength[, noAssert]) buf.writeUIntBE(value, offset, byteLength[, noAssert]) buf.writeIntLE(value, offset, byteLength[, noAssert]) buf.writeIntBE(value, offset, byteLength[, noAssert]) buf.writeUInt8(value, offset[, noAssert]) buf.writeUInt16LE(value, offset[, noAssert]) buf.writeUInt16BE(value, offset[, noAssert]) buf.writeUInt32LE(value, offset[, noAssert]) buf.writeUInt32BE(value, offset[, noAssert]) buf.writeInt8(value, offset[, noAssert]) buf.writeInt16LE(value, offset[, noAssert]) buf.writeInt16BE(value, offset[, noAssert]) buf.writeInt32LE(value, offset[, noAssert]) buf.writeInt32BE(value, offset[, noAssert]) buf.writeFloatLE(value, offset[, noAssert]) buf.writeFloatBE(value, offset[, noAssert]) buf.writeDoubleLE(value, offset[, noAssert]) buf.writeDoubleBE(value, offset[, noAssert])
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 大英县| 炉霍县| 广饶县| 衡南县| 昭平县| 桂阳县| 霍林郭勒市| 玉溪市| 邯郸县| 肥东县| 新邵县| 桃江县| 石渠县| 明光市| 突泉县| 黔西| 万州区| 射洪县| 定兴县| 南郑县| 舞阳县| 宁夏| 获嘉县| 读书| 安庆市| 运城市| 双城市| 株洲市| 中宁县| 丹阳市| 仙居县| 壶关县| 阜平县| 奉节县| 天门市| 闸北区| 大渡口区| 台山市| 南郑县| 民丰县| 岳普湖县|