var buf1 = new Buffer('ABC'); // 拷贝一个缓冲区 var buf2 = new Buffer(2); var buf3 = new Buffer(3); var buf4 = new Buffer(5); var buf5 = new Buffer('abcdef'); buf1.copy(buf2); buf1.copy(buf3); buf1.copy(buf4); buf1.copy(buf5); console.log("buf2 content: " + buf2.toString()); //buf2 content: AB console.log("buf3 content: " + buf3.toString()); //buf3 content: ABC console.log("buf4 content: " + buf4.toString()); //buf4 content: ABC 因为开辟出来的内存大了些,所以这里接一堆怪符号 console.log("buf5 content: " + buf5.toString()); //buf5 content: ABCdef
缓冲区裁剪
语法:
buf.slice([start][, end])
参数:
参数描述如下:
start - 数字, 可选, 默认: 0
end - 数字, 可选, 默认: buffer.length
返回值:
返回一个新的缓冲区,它和旧缓冲区指向同一块内存,但是从索引 start 到 end 的位置剪切。
实例:
var buffer1 = new Buffer('runoob'); // 剪切缓冲区 var buffer2 = buffer1.slice(0,2); console.log("buffer2 content: " + buffer2.toString()); //buffer2 content: ru