有用的代码块-第04章-Buffer模块处理

参考文档: https://nodejs.org/api/buffer.html

Buffer 的静态方法

// 创建Buffer
Buffer.alloc(size[, fill[, encoding]])
Buffer.allocUnsafe(size)
Buffer.allocUnsafeSlow(size)

// 拷贝
Buffer.from(array)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(string[, encoding])
Buffer.from(object[, offsetOrEncoding[, length]])

// 获取长度
Buffer.byteLength(string[, encoding])

res.writeHeader(200, {
    "Content-Type": "text/plain;charset=utf-8",
    "Content-Length": Buffer.byteLength(buf, "utf8");
});

// 比较
Buffer.compare(buf1, buf2)

// 连接
Buffer.concat(list[, totalLength])

// 判断是否是Buffer
Buffer.isBuffer(obj)

// 判断是否是该字符集
Buffer.isEncoding(encoding)

常用Buffer编码: `utf8`, `ascii`, `utf16le`/`ucs2`, `base64`, `binary`/`latin1`, `hex`

// 预分配大小, 默认8192
Buffer.poolSize

Buffer 的基本操作

buf[index]
buf.buffer
buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
buf.entries()
buf.equals(otherBuffer)
buf.fill(value[, offset[, end]][, encoding])
buf.includes(value[, byteOffset][, encoding])
buf.indexOf(value[, byteOffset][, encoding])
buf.keys()
buf.lastIndexOf(value[, byteOffset][, encoding])
buf.length

// 切割
const buf = buf.slice([start[, end]])

// 将buffer数组, 以不同长度的数字顺序翻转
buf.swap16() 12345678 => 21436587
buf.swap32() 12345678 => 43218765
buf.swap64() 12345678 => 87654321

// 与JSON之间的相互转换
buf.toJSON() {"type":"Buffer", "data": []}
const json = JSON.stringify(buf);
const copy = JSON.parse(json, (key, value) => {
  return value && value.type === 'Buffer' ?
    Buffer.from(value.data) :
    value;
});

// 转换成字符串
buf.toString([encoding[, start[, end]]])

buf.values() 返回一个指针, for..of是可以使用buf

读取 Buffer

buf.readDoubleBE(offset[, noAssert])
buf.readDoubleLE(offset[, noAssert])
buf.readFloatBE(offset[, noAssert])
buf.readFloatLE(offset[, noAssert])
buf.readInt8(offset[, noAssert])
buf.readInt16BE(offset[, noAssert])
buf.readInt16LE(offset[, noAssert])
buf.readInt32BE(offset[, noAssert])
buf.readInt32LE(offset[, noAssert])
buf.readIntBE(offset, byteLength[, noAssert])
buf.readIntLE(offset, byteLength[, noAssert])
buf.readUInt8(offset[, noAssert])
buf.readUInt16BE(offset[, noAssert])
buf.readUInt16LE(offset[, noAssert])
buf.readUInt32BE(offset[, noAssert])
buf.readUInt32LE(offset[, noAssert])
buf.readUIntBE(offset, byteLength[, noAssert])
buf.readUIntLE(offset, byteLength[, noAssert])

LE little endian 小端, BE big endian 大端

写入 Buffer

buf.write(string[, offset[, length]][, encoding])
buf.writeDoubleBE(value, offset[, noAssert])
buf.writeDoubleLE(value, offset[, noAssert])
buf.writeFloatBE(value, offset[, noAssert])
buf.writeFloatLE(value, offset[, noAssert])
buf.writeInt8(value, offset[, noAssert])
buf.writeInt16BE(value, offset[, noAssert])
buf.writeInt16LE(value, offset[, noAssert])
buf.writeInt32BE(value, offset[, noAssert])
buf.writeInt32LE(value, offset[, noAssert])
buf.writeIntBE(value, offset, byteLength[, noAssert])
buf.writeIntLE(value, offset, byteLength[, noAssert])
buf.writeUInt8(value, offset[, noAssert])
buf.writeUInt16BE(value, offset[, noAssert])
buf.writeUInt16LE(value, offset[, noAssert])
buf.writeUInt32BE(value, offset[, noAssert])
buf.writeUInt32LE(value, offset[, noAssert])
buf.writeUIntBE(value, offset, byteLength[, noAssert])
buf.writeUIntLE(value, offset, byteLength[, noAssert])
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.