关于Node.js中Buffer的一些你可能不知道的用法
作者:老雷 发布时间:2024-05-09 09:05:33
前言
在大多数介绍 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'));
// 转换成十六进制字符串:68656c6c6f2c20776f726c6421
console.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 length
var 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])
这些 API 为在 Node.js 中操作数据提供了极大的便利。假设我们要将一个整形数值存储到文件中,比如当前时间戳为1447656645380,如果将其当作一个字符串存储时,需要占用 11 字节的空间,而将其转换为二进制存储时仅需 6 字节空间即可:
var buf = new Buffer(6);
buf.writeUIntBE(1447656645380, 0, 6);
// <Buffer 01 51 0f 0f 63 04>
buf.readUIntBE(0, 6);
// 1447656645380
在使用 Node.js 编写一些底层功能时,比如一个网络通信模块、某个数据库的客户端模块,或者需要从文件中操作大量结构化数据时,以上 Buffer 对象提供的 API 都是必不可少的。
接下来将演示一个使用 Buffer 对象操作结构化数据的例子。
操作结构化数据
假设有一个学生考试成绩数据库,每条记录结构如下:
学号 | 课程代码 | 分数 |
---|---|---|
XXXXXX | XXXX | XX |
其中学号是一个 6 位的数字,课程代码是一个 4 位数字,分数最高分为 100 分。
在使用文本来存储这些数据时,比如使用 CSV 格式存储可能是这样的:
100001,1001,99
100002,1001,67
100003,1001,88
其中每条记录占用 15 字节的空间,而使用二进制存储时其结构将会是这样:
学号 | 课程代码 | 分数 |
---|---|---|
3 字节 | 2 字节 | 1 字节 |
每一条记录仅需要 6 字节的空间即可,仅仅是使用文本存储的 40%!下面是用来操作这些记录的程序:
// 读取一条记录
// buf Buffer 对象
// offset 本条记录在 Buffer 对象的开始位置
// data {number, lesson, score}
function writeRecord (buf, offset, data) {
buf.writeUIntBE(data.number, offset, 3);
buf.writeUInt16BE(data.lesson, offset + 3);
buf.writeInt8(data.score, offset + 5);
}
// 写入一条记录
// buf Buffer 对象
// offset 本条记录在 Buffer 对象的开始位置
function readRecord (buf, offset) {
return {
number: buf.readUIntBE(offset, 3),
lesson: buf.readUInt16BE(offset + 3),
score: buf.readInt8(offset + 5)
};
}
// 写入记录列表
// list 记录列表,每一条包含 {number, lesson, score}
function writeList (list) {
var buf = new Buffer(list.length * 6);
var offset = 0;
for (var i = 0; i < list.length; i++) {
writeRecord(buf, offset, list[i]);
offset += 6;
}
return buf;
}
// 读取记录列表
// buf Buffer 对象
function readList (buf) {
var offset = 0;
var list = [];
while (offset < buf.length) {
list.push(readRecord(buf, offset));
offset += 6;
}
return list;
}
我们可以再编写一段程序来看看效果:
var list = [
{number: 100001, lesson: 1001, score: 99},
{number: 100002, lesson: 1001, score: 88},
{number: 100003, lesson: 1001, score: 77},
{number: 100004, lesson: 1001, score: 66},
{number: 100005, lesson: 1001, score: 55},
];
console.log(list);
var buf = writeList(list);
console.log(buf);
// 输出 <Buffer 01 86 a1 03 e9 63 01 86 a2 03 e9 58 01 86 a3 03 e9 4d 01 86 a4 03 e9 42 01 86 a5 03 e9 37>
var ret = readList(buf);
console.log(ret);
/* 输出
[ { number: 100001, lesson: 1001, score: 99 },
{ number: 100002, lesson: 1001, score: 88 },
{ number: 100003, lesson: 1001, score: 77 },
{ number: 100004, lesson: 1001, score: 66 },
{ number: 100005, lesson: 1001, score: 55 } ]
*/
lei-proto 模块介绍
上面的例子中,当每一条记录的结构有变化时,我们需要修改readRecord()
和writeRecord()
,重新计算每一个字段在 Buffer 中的偏移量,当记录的字段比较复杂时很容易出错。为此我编写了lei-proto模块,它允许你通过简单定义每条记录的结构即可生成对应的readRecord()
和`writeRecord()
函数。
首先执行以下命令安装此模块:
$ npm install lei-proto --save
使用lei-proto模块后,前文的例子可以改为这样:
var parsePorto = require('lei-proto');
// 生成指定记录结构的数据编码/解码器
var record = parsePorto([
['number', 'uint', 3],
['lesson', 'uint', 2],
['score', 'uint', 1]
]);
function readList (buf) {
var list = [];
var offset = 0;
while (offset < buf.length) {
list.push(record.decode(buf.slice(offset, offset + 6)));
offset += 6;
}
return list;
}
function writeList (list) {
return Buffer.concat(list.map(record.encodeEx));
}
运行与上文同样的测试程序,可看到其结果是一样的:
<Buffer 01 86 a1 03 e9 63 01 86 a2 03 e9 58 01 86 a3 03 e9 4d 01 86 a4 03 e9 42 01 86 a5 03 e9 37>
[ { number: 100001, lesson: 1001, score: 99 },
{ number: 100002, lesson: 1001, score: 88 },
{ number: 100003, lesson: 1001, score: 77 },
{ number: 100004, lesson: 1001, score: 66 },
{ number: 100005, lesson: 1001, score: 55 } ]
关于lei-proto模块的详细使用方法可访问该模块的主页浏览:https://github.com/leizongmin/node-lei-proto
对此感兴趣的读者也可研究一下其实现原理。
来源:http://morning.work/page/2015-11/nodejs_buffer.html
猜你喜欢
- 一、分屏展示当你想同时看到多个文件的时候:右击标签页;选择 move right 或者 split vertical;效果:二、远程 Pyt
- 在Python中,任何类型的对象都可以做真值测试,并且保证返回True或者False。以下几种值(不论类型)在真值测试中返回False:1.
- 这篇文章主要介绍了python的time模块和datetime模块实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 你是否曾经想在数据库中存储一个日期而没有时间部分,或者想存储一个时间值希望有更高的精度?在SQL Server 2008的介绍中,微软介绍了
- 一、动机(Motivate)“观察者模式”在现实生活中,实例其实是很多的,比如:八九十年代我们订阅的
- 这里以将Apache的日志写入到ElasticSearch为例,来演示一下如何使用Python将Spark数据导入到ES中。实际工作中,由于
- 本文采用OpenCV3和Python3 来实现静态图片的人脸识别,采用的是Haar文件级联。 首先需要将OpenCV3源代码中找到data文
- Spring @Enable 模块概览框架实现@Enable注解模块激活模块Spring Framework@EnableWebMvcWeb
- 当你执行大型程序的时候,突然出现exception,会让程序直接停止,这种对服务器自动程序很不友好,而python有着较好的异常捕获机制,不
- ThreadLocal在threading模块中,可以见得它是为我们的线程服务的。它的主要作用是存储当前线程的变量,各个线程之间的变量名是可
- 最近在做一个小案例的时候遇到了Math.max.apply这么一个用法,之前很少遇到过感觉挺有趣的,就记录一下。1Math.max语法: M
- 本文记录了Anaconda2安装NLTK的方法,供大家参考,具体内容如下先看我的python和Anaconda版本启动anaconda命令窗
- python如何处理“&#”开头加数字的html字符,比如:风水这类数据。用python
- 以下就重复记录删除的问题作一阐述。 有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如N
- 本文实例讲述了Python简单网络编程。分享给大家供大家参考,具体如下:内容目录1. 客户端(client.py)2. 服务端(server
- CSS Modules:局部作用域 & 模块化CSS Modules 为每一个局部类赋予全局唯一的类名,这样组件样式间就不会相互影响
- 1. 原地交换两个数字Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例:x,y= 10,20print
- 问题介绍在安装torch之后,命令行(Anaconda Powershell Prompt)运行这三行代码:python # pythoni
- 最近在B站上看到Vscode可以远程连接Linux, 不仅有与linux一模一样的终端,而且写代码很舒服,所以尝试了一下远程连接。首先,要先
- 1 from multiprocessing import Pool,Queue。其中Queue在Pool中不起作用,具体原因未明。解决方案