在Node中,應用需要處理網(wǎng)絡協(xié)議贴彼、操作數(shù)據(jù)庫、處理圖片强岸、接收上傳文件等,在網(wǎng)絡流和文件的操作中砾赔,還要處理大量二進制數(shù)據(jù)蝌箍,JavaScript語言沒有讀取或操作二進制數(shù)據(jù)流的機制青灼。
Buffer 類的實例類似于從 0 到 255 之間的整數(shù)數(shù)組(其他整數(shù)會通過 & 255 操作強制轉換到此范圍),但對應于 V8 堆外部的固定大小的原始內存分配妓盲。 Buffer 的大小在創(chuàng)建時確定杂拨,且無法更改。
Buffer 類在全局作用域中悯衬,因此無需使用 require('buffer').Buffer弹沽。
Buffer的創(chuàng)建:
//Buffer.from()、Buffer.alloc() 與 Buffer.allocUnsafe()
let buffer = Buffer.from('23');
console.log(buffer);
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]); //使用一個8位字節(jié)的數(shù)組分配一個新的 Buffer筋粗。
console.log(buf.toString());
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
const buf = Buffer.from(arr.buffer); // shares the memory with arr;
console.log(buf); //<Buffer 88 13 a0 0f>
arr[1] = 7000;
console.log(buf); //<Buffer 88 13 58 1b>
let buffer = Buffer.alloc(10); // 創(chuàng)建一個長度為 10策橘、且用零填充的 Buffer。
//<Buffer 00 00 00 00 00 00 00 00 00 00>
Buffer.concat(list[, totalLength]);
返回一個合并了 list 中所有 Buffer 實例的新 Buffer娜亿。
如果 list 中沒有元素丽已、或 totalLength 為 0,則返回一個長度為 0 的 Buffer买决。
const buf1 = Buffer.alloc(10, 0);
const buf2 = Buffer.alloc(14, 0);
const buf3 = Buffer.alloc(18, 0);
const totalLength = buf1.length+buf2.length+buf3.length;
console.log(totalLength);
const bufa = Buffer.concat([buf1, buf2, buf3], totalLength);
console.log(bufa);
console.log(bufa.length);
//42
//<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
//42
buf.write(string[, offset[, length]][, encoding]);
將sring寫入buf實例沛婴,同時返回寫入的字節(jié)數(shù)。
參數(shù)如下:
string:寫入的字符串督赤。
offset:從buf的第幾位開始寫入嘁灯,默認是0。
length:寫入多少個字節(jié)躲舌,默認是 buf.length - offset丑婿。
encoding:字符串的編碼,默認是utf8孽糖。
var buff = Buffer.alloc(4);
buff.write('a'); // 返回 1
console.log(buff); // 打印 <Buffer 61 00 00 00>
buff.write('ab'); // 返回 2
console.log(buff); // 打印 <Buffer 61 62 00 00>
buf.toString([encoding[, start[, end]]])枯冈; 轉成字符串
var buff = Buffer.from('hello');
console.log( buff.toString() ); // hello
console.log( buff.toString('utf8', 0, 2) ); // he
buf.toJSON() 轉成JSON字符串
var buff = Buffer.from('hello');
console.log( buff.toJSON() ); // { type: 'Buffer', data: [ 104, 101, 108, 108, 111 ] }
stream是一個抽象接口,該抽象接口是可讀办悟、可寫或是既可讀又可寫的尘奏,通過這些接口,我們可以和磁盤文件病蛉、套接字炫加、HTTP請求來交互,實現(xiàn)數(shù)據(jù)從一個地方流動到另一個地方的功能铺然。
所有的Stream對象都是EventEmitter的實例俗孝,常用的事件有四個:
data-當有數(shù)據(jù)可讀時觸發(fā)
end-當沒有數(shù)據(jù)可讀時觸發(fā)
error-在接受和寫入過程中發(fā)生錯誤時觸發(fā)
finish-當所有的數(shù)據(jù)已經(jīng)寫入到底層系統(tǒng)時觸
流讀取:
const fs = require('fs');
const readStream = fs.createReadStream('./test.txt');
let data = '';
readStream.setEncoding('utf8');
readStream.on('data', function(chunk){
data+=chunk;
});
readStream.on('end', function(){
console.log(data);
console.log('流讀取結束');
});
readStream.on('error', function(err){
console.log(err);
});
console.log('執(zhí)行結束魄健!');
寫入流:
const fs = require('fs');
const writeStream = fs.createWriteStream('./test.txt',{'flags':'a'});
let data = '測試內容8陈痢!沽瘦!';
writeStream.write(data, 'utf8');
writeStream.end();
writeStream.on('finish', function(){
console.log('寫入結束革骨!');
});
writeStream.on('error', function(err){
console.log(err);
});
console.log('執(zhí)行結束!!!!!');
管道流:管道提供一種輸出流到輸入流的機制农尖,通常用于從一個流中獲取數(shù)據(jù)到另一個流中。
const fs = require('fs');
const readStream = fs.createReadStream('./a.txt');
readStream.setEncoding('utf8');
const writeStream = fs.createWriteStream('./b.txt');
readStream.pipe(writeStream);
console.log('執(zhí)行結束A颊堋盛卡!');