node.JS二进制操作模块buffer对象使用方法详解(5)

var buf1 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]); var buf2 = Buffer.from([5, 6, 7, 8, 9, 1, 2, 3, 4]); // 输出: 0(buf2中的1234对比buf2中的1234) console.log(buf1.compare(buf2, 5, 9, 0, 4)); // 输出: -1(buf2中的567891对比buf1中的56789) console.log(buf1.compare(buf2, 0, 6, 4)); // 输出: 1(buf2中的1对比buf2中的6789) console.log(buf1.compare(buf2, 5, 6, 5));

buf.equals(otherBuffer)

如果 buf 与 otherBuffer 具有完全相同的字节,则返回 true,否则返回 false

otherBuffer <Buffer> 要比较的 Buffer

返回: <Boolean>

var buf1 = Buffer.from('ABC'); var buf2 = Buffer.from('ABC'); var buf3 = Buffer.from('abc'); console.log(buf1.equals(buf2));//true console.log(buf1.equals(buf3));//false

buf.fill(value[, offset[, end]][, encoding])

value <String> | <Buffer> | <Integer> 用来填充 buf 的值

offset <Integer> 开始填充 buf 的位置。默认: 0

end <Integer> 结束填充 buf 的位置(不包含)。默认: buf.length

encoding <String> 如果 value 是一个字符串,则这是它的字符编码。 默认: 'utf8'

返回: <Buffer> buf 的引用

如果未指定 offset 和 end,则填充整个 buf。 这个简化使得一个Buffer的创建与填充可以在一行内完成

var b = Buffer.allocUnsafe(10).fill('h'); console.log(b.toString());//hhhhhhhhhh

buf.indexOf(value[, byteOffset][, encoding])

value <String> | <Buffer> | <Integer> 要搜索的值

byteOffset <Integer> buf 中开始搜索的位置。默认: 0

encoding <String> 如果 value 是一个字符串,则这是它的字符编码。 默认: 'utf8'

返回: <Integer> buf 中 value 首次出现的索引,如果 buf 没包含 value 则返回 -1

如果value是字符串,则 value 根据 encoding 的字符编码进行解析;如果value是Buffer,则value会被作为一个整体使用。如果要比较部分 Buffer 可使用 buf.slice();如果value是数值,则 value 会解析为一个 0 至 255 之间的无符号八位整数值

var buf = Buffer.from('this is a buffer'); // 输出: 0 console.log(buf.indexOf('this')); // 输出: 2 console.log(buf.indexOf('is')); // 输出: 8 console.log(buf.indexOf(Buffer.from('a buffer'))); // 输出: 8 // (97 是 'a' 的十进制 ASCII 值) console.log(buf.indexOf(97)); // 输出: -1 console.log(buf.indexOf(Buffer.from('a buffer example'))); // 输出: 8 console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));

buf.lastIndexOf(value[, byteOffset][, encoding])

与 buf.indexOf() 类似,除了 buf 是从后往前搜索而不是从前往后

var buf = Buffer.from('this buffer is a buffer'); // 输出: 0 console.log(buf.lastIndexOf('this')); // 输出: 17 console.log(buf.lastIndexOf('buffer')); // 输出: 17 console.log(buf.lastIndexOf(Buffer.from('buffer'))); // 输出: 15 // (97 是 'a' 的十进制 ASCII 值) console.log(buf.lastIndexOf(97)); // 输出: -1 console.log(buf.lastIndexOf(Buffer.from('yolo'))); // 输出: 5 console.log(buf.lastIndexOf('buffer', 5)); // 输出: -1 console.log(buf.lastIndexOf('buffer', 4));

buf.includes(value[, byteOffset][, encoding])

该方法相当于 buf.indexOf() !== -1

value <String> | <Buffer> | <Integer> 要搜索的值

byteOffset <Integer> buf 中开始搜索的位置。默认: 0

encoding <String> 如果 value 是一个字符串,则这是它的字符编码。 默认: 'utf8'

返回: <Boolean> 如果 buf 找到 value,则返回 true,否则返回 false

var buf = Buffer.from('this is a buffer'); // 输出: true console.log(buf.includes('this')); // 输出: true console.log(buf.includes('is')); // 输出: true console.log(buf.includes(Buffer.from('a buffer'))); // 输出: true // (97 是 'a' 的十进制 ASCII 值) console.log(buf.includes(97)); // 输出: false console.log(buf.includes(Buffer.from('a buffer example'))); // 输出: true console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8))); // 输出: false

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/81a5fdd1361f21cfbdac27e742136dbf.html