44、buffer的应用
server.js
//服务端可以操作二进制,Buffer可以和字符串进行相互转化
//Buffer代表的都是二进制数据 内存(buffer不能扩容) java 数组不能扩容 动态数组,再生成一个新的内存,拷贝进去
1、buffer的三种声明方式,通过长度来声明(大小)
const buffer = Buffer.alloc(5);//开发中数字都是字节为单位
//不具备扩展push shift这些方法
//console.log(buffer.slice(0,1));//slice方法也是“浅拷贝”
浅拷贝的例子
let myArr = [1,2,3];
let arr = [myArr,2,3];
let newArr = arr.slice(0);
newArr[0][0] = 100;
console.log(myArr);//[100,2,3]
const buffer = Buffer.alloc(5);
let buf = buffer.slice(0,1);
buf[0]=100;
console.log(buffer);
//<Buffer 64 00 00 00 00>
const buffer1 = Buffer.from('珠峰');//根据汉字来转化成buffer
console.log(buffer1);
console.log(buffer1.length);
//<Buffer e7 8f a0 e5 b3 b0>
//6
const buffer2 = Buffer.from([0x16,0x32]);//通过数组来制定存放的内容
console.log(buffer2);//<Buffer 16 32>
//slice()
//length可以获取buffer字节的长度
//Buffer.isBuffer
console.log(Buffer.isBuffer(buffer));//true
//文件操作 索引修改 更改内部内容 拼接buffer
//copy 用的少
//concat 原理是copy
const buf1 = Buffer.from('珠峰');
const buf2 = Buffer.from('架构');
const bigBuf = Buffer.alloc(12);
//taretBuffer,targetStart,sourceStart,sourceEnd
//copy的实现
Buffer.prototype.copy = function(targetBuffer,targetStart,sourceStart=0,sourceEnd=this.length){
for(let i= sourceStart;i<sourceEnd;i++)
{
targetBuffer[targetStart++]=this[i];
}
}
buf1.copy(bigBuf,0,0,6);
buf2.copy(bigBuf,6);
console.log(bigBuf.toString());//登录架构
const buf1 = Buffer.from('登录');
const buf2 = Buffer.from('架构');
Buffer.concat=function(bufferList,length=bufferList.reduce((a,b)=>a+b.length,0)){
let buffer = Buffer.alloc(length);
let offset = 0;
bufferList.forEach(buf=>{
buf.copy(buffer,offset);
offset += buf.length;
});
return buffer.slice(0,offset);
}
console.log(Buffer.concat([buf1,buf2,buf1,buf2]).toString());//登录架构登录架构
//文件上传->分片上传->"二进制"->buffer来拼接