您好,登錄后才能下訂單哦!
本文實例講述了node Buffer緩存區常見操作。分享給大家供大家參考,具體如下:
創建buffer類
var buf=new buffer(10); var buf=new buffer([10,20,30,40]); var buf=new buffer("www.baidu.com","utf-8");
寫入緩存區
buf.write(string[,offset[,length]][encoding]) buf=new buffer(256); len=buf.write("www.baidu.com") console,log("寫入字節數:"+len)
從緩存讀取數據
buf.toString([encoding[,start[,end]]]) buf=new buffer(26); for(var i=0;i<26;i++;){ buf[i]=i+97; } console.log(buf.toString('ascill')); console.log(buf.toString('ascill',0,5)) console.log(buf.toString('utf-8',0,5)) console.log(buf.toString('undefined',0,5))
轉為json
buf.toJSON() var buf=new buffer('www.baidu.com'); var json=buf.toJSON('buf') console.log(json)
緩存合并
buffer.concat(list[,totalLength]); var buffer1=new Buffer('百度搜索'); var buffer2=new Buffer('www.baidu.com'); var buffer3=Buffer.concat([buffer1,buffer2]); console.log("buffer3內容"+buffer3.toString())
緩存區比較
buf.compare(otherBuffer) var buffer1=new Buffer('ABC'); var buffer2=new Buffer('ABCD'); var result=buffer1.compare(buffer2); if(result<0){ console.log(buffer1+"在"+buffer2+"之前") }else if(result==0){ console.log(buffer1+"等于"+buffer) }else{ console.log(buffer1+"在"+buffer2+"之后") }
拷貝緩存區
buf.copy(targetBuffer[,targetStart[,sourceStart[,sourcEnd]]]) var buffer1=new Buffer('ABC'); var buffer2=new Buffer(3); buffer1.copy(Buffer2); console.log("buffer2 content:"+buffer2.toString())
緩存區裁剪
buf.slice([start[,end]]) var buffer1=new Buffer('baidu') var buffer2=buffer1.slice(0,2); console.log("buffer2 content:"+buffer2.toString()); buffer2 content:ba
緩存區長度
buf.length var buffer=new Buffer("www.baidu.com"); console.log("buffer length:"+buffer.length) buffer length:13
希望本文所述對大家node.js程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。