Nodejs學習——緩衝區操作
緩衝區的操作
合併
Buffer.concat(list[, totalLength])
- 需要合併的緩衝對列表
- 緩衝區的總大小
- 返回合併後新的緩衝區
例項:
var buffer1 = Buffer.from('不積跬步無以至千里');
var buffer2 = Buffer.from('不積小流無以成江海');
var
console.log(buffer3);
執行結果:
比較
buf.compare(otherBuffer);
- 引數1:參與比較的緩衝區
- 引數2:otherBuffer開始的索引值
- 引數3:otherBuffer結束的索引值
- 引數4:Buffer開始的索引值
- 引數5:Buffer結束的索引值
- 返回結果為int型:0 -buf和otherBuffer相同,1-buf大於otherBuffer ,-1-buf小於otherBuffer
var buffer1 = Buffer.from('不積跬步無以至千里');
var buffer2 = Buffer.from('不積小流無以成江海');
var result = buffer1.compare(buffer2);
console.log(result);
執行結果:
var buffer1 = Buffer.from('不積跬步無以至千里');
var buffer2 = Buffer.from('不積小流無以成江海');
var result = buffer1.compare(buffer2,1,10,1,2);
console.log(result);
執行結果:
buf.equals(otherBuffer)
返回值為布林值,判斷兩個Buffer例項儲存的資料是否相同,不同於compare之處在於equals不能指定判斷的範圍。
拷貝:buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])
- 要拷貝的Buffer物件
- TargetBuffer要拷貝物件的起始索引值(預設為0)
- sourceStart被拷貝起始位置的索引值(預設為0)
- sourceEnd結束索引值(預設為buf.length)
例項:
var buffer1 = Buffer.from('不積跬步無以至千里');
var buffer2 = Buffer.from('不積小流無以成江海');
var result = buffer1.compare(buffer2,1,10,1,2);
buffer1.copy(buffer2);
console.log(buffer1.equals(buffer2))
執行結果:
裁剪緩衝區
buf.slice([start[, end]])
- 開始索引值(預設為0)
- 結束索引值(預設為0)
- 返回裁剪後的緩衝區,指向同一塊緩衝,但從start-end剪下
例項:
var buffer1 = Buffer.from('不積跬步無以至千里');
console.log(buffer1);
console.log(buffer1.slice(1,2));
執行結果:
可以看出,從索引下標1開始擷取,同時索引下標2截止(不會獲取end指向的值)
var buffer1 = Buffer.from('不積跬步無以至千里');
console.log(buffer1);
console.log(buffer1.slice(1,buffer1.length));
執行結果:
常用方法合集
1 |
new Buffer(size) |
2 |
new Buffer(buffer) |
3 |
new Buffer(str[, encoding]) |
4 |
buf.length |
5 |
buf.write(string[, offset[, length]][, encoding]) |
6 |
buf.writeUIntLE(value, offset, byteLength[, noAssert]) const buf = Buffer.allocUnsafe(6);
buf.writeUIntLE(0x1234567890ab, 0, 6);
// 輸出: <Buffer ab 90 78 56 34 12> console.log(buf); noAssert 值為 true 時,不再驗證 value 和 offset 的有效性。 預設是 false。 |
7 |
buf.writeUIntBE(value, offset, byteLength[, noAssert]) const buf = Buffer.allocUnsafe(6);
buf.writeUIntBE(0x1234567890ab, 0, 6);
// 輸出: <Buffer 12 34 56 78 90 ab> console.log(buf); |
8 |
buf.writeIntLE(value, offset, byteLength[, noAssert]) |
9 |
buf.writeIntBE(value, offset, byteLength[, noAssert]) |
10 |
buf.readUIntLE(offset, byteLength[, noAssert]) |
11 |
buf.readUIntBE(offset, byteLength[, noAssert]) |
12 |
buf.readIntLE(offset, byteLength[, noAssert]) |
13 |
buf.readIntBE(offset, byteLength[, noAssert]) |
14 |
buf.toString([encoding[, start[, end]]]) |
15 |
buf.toJSON() |
16 |
buf[index] |
17 |
buf.equals(otherBuffer) |
18 |
buf.compare(otherBuffer) |
19 |
buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]]) |
20 |
buf.slice([start[, end]]) |
21 |
buf.readUInt8(offset[, noAssert]) |
22 |
buf.readUInt16LE(offset[, noAssert]) |
23 |
buf.readUInt16BE(offset[, noAssert]) |
24 |
buf.readUInt32LE(offset[, noAssert]) |
25 |
buf.readUInt32BE(offset[, noAssert]) |
26 |
buf.readInt8(offset[, noAssert]) |
27 |
buf.readInt16LE(offset[, noAssert]) |
28 |
buf.readInt16BE(offset[, noAssert]) |
29 |
buf.readInt32LE(offset[, noAssert]) |
30 |
buf.readInt32BE(offset[, noAssert]) |
31 |
buf.readFloatLE(offset[, noAssert]) |
32 |
buf.readFloatBE(offset[, noAssert]) |
33 |
buf.readDoubleLE(offset[, noAssert]) |
34 |
buf.readDoubleBE(offset[, noAssert]) |
35 |
buf.writeUInt8(value, offset[, noAssert]) |
36 |
buf.writeUInt16LE(value, offset[, noAssert]) |
37 |
buf.writeUInt16BE(value, offset[, noAssert]) |
38 |
buf.writeUInt32LE(value, offset[, noAssert]) |
39 |
buf.writeUInt32BE(value, offset[, noAssert]) |
40 |
buf.writeInt8(value, offset[, noAssert])<="" td=""> |
41 |
buf.writeInt16LE(value, offset[, noAssert]) |
42 |
buf.writeInt16BE(value, offset[, noAssert]) |
43 |
buf.writeInt32LE(value, offset[, noAssert]) |
44 |
buf.writeInt32BE(value, offset[, noAssert]) |
45 |
buf.writeFloatLE(value, offset[, noAssert]) |
46 |
buf.writeFloatBE(value, offset[, noAssert]) |
47 |
buf.writeDoubleLE(value, offset[, noAssert]) |
48 |
buf.writeDoubleBE(value, offset[, noAssert]) |
49 |
buf.fill(value[, offset][, end]) |