标签:
Net Buffer Stream
1 new Buffer(‘Hello 皮卡丘‘) 2 //<Buffer 48 65 6c 6c 6f 20 e7 9a ae e5 8d a1 e4 b8 98> 3 4 new Buffer(‘Hello 皮卡丘‘, ‘base64‘) 5 //<Buffer 1d e9 65 a1 af> 6 7 var buf = new Buffer(‘123456‘);console.log(buf) 8 //undefined 9 10 var buf = new Buffer(7);buf.write(‘12345678‘);console.log(buf.length) 11 //7, 超出部分会被忽略掉 12 13 var buf = new Buffer([1,2,3,4]);console.log(buf) 14 //<Buffer 01 02 03 04>
Buffer 用于处理二进制数据
一开始长度被指定了,无论怎么打,长度都不会变
1 var fs = require(‘fs‘); 2 3 fs.readFile(‘imooc_class.jpg‘, function (err, origin_buffer) { 4 console.log(Buffer.isBuffer(origin_buffer)); 5 6 fs.writeFile(‘imooc_buffer.jpg‘, origin_buffer, function (err) { 7 if(err) console.log(err); 8 }); 9 10 var base64Image = origin_buffer.toString(‘base64‘); 11 console.log(base64Image); 12 13 var decodedImage = new Buffer(base64Image, ‘base64‘); 14 console.log(Buffer.compare(origin_buffer, decodedImage)); 15 16 fs.writeFile(‘logo_decoded.jpg‘, decodedImage, function (err) { 17 if(err) console.log(err); 18 }); 19 });
Stream流
/**********************************/
1 var http = require(‘http‘); 2 var fs = require(‘fs‘); 3 var request = require(‘request‘); 4 5 http.createServer(function (req, res) { 6 /*fs.readFile(‘imooc_class.jpg‘, function (err, res) { 7 if(err) { 8 res.end(‘file not exist!‘); 9 }else { 10 res.writeHeader(200, {‘Content-Type‘: ‘text/html‘});, 11 res.end(data); 12 } 13 });*/ 14 15 //fs.createReadStream(‘imooc_class.jpg‘).pipe(res); 16 17 //爬取网页图片,输出到指定端口 18 request(‘http://imgsrc.baidu.com/forum/w 19 20 %3D580/sign=e2f3b45fa086c91708035231f93c70c6/fb781bd5ad6eddc426e3e45c3edbb6fd53663399.jpg‘).pipe(res); 21 }).listen(8090);
/********************************************/
1 var fs = require(‘fs‘); 2 3 fs.createReadStream(‘1.mp3‘).pipe(fs.createWriteStream(‘1-pipe.mp3‘));
/********************************************/
1 var stream = require(‘stream‘); 2 3 var Readable = stream.Readable; 4 var Writable = stream.Writable; 5 6 //实例化 7 var readStream = new Readable(); 8 var writStream = new Writable(); 9 10 //可读流 11 readStream.push(‘I ‘); 12 readStream.push(‘Love ‘); 13 readStream.push(‘Node\n‘); 14 //可读流结束 15 readStream.push(null); 16 17 //可写流 18 writStream._write = function (chunk, encode, cb) { 19 console.log(chunk.toString()); 20 cb(); 21 }; 22 23 readStream.pipe(writStream);
/***************************************/
1 //定制流 2 var stream = require(‘stream‘); 3 //工具类 4 var util = require(‘util‘); 5 6 function ReadStream() { 7 stream.Readable.call(this); 8 } 9 10 util.inherits(ReadStream, stream.Readable); 11 12 ReadStream.prototype._read = function () { 13 //可读流 14 this.push(‘I ‘); 15 this.push(‘Love ‘); 16 this.push(‘Node\n‘); 17 //可读流结束 18 this.push(null); 19 }; 20 21 function WritStream() { 22 stream.Writable.call(this); 23 this._cached = new Buffer(‘‘); 24 } 25 26 util.inherits(WritStream, stream.Writable); 27 28 WritStream.prototype._write = function (chunk, encode, cb) { 29 console.log(chunk.toString()); 30 cb(); 31 }; 32 33 //转换流 34 function TransformStream() { 35 stream.Transform.call(this); 36 } 37 38 util.inherits(TransformStream, stream.Transform); 39 40 TransformStream.prototype._transform = function (chunk, encode, cb) { 41 this.push(chunk); 42 cb(); 43 }; 44 45 TransformStream.prototype._flush = function (cb) { 46 this.push(‘Oh Yeah‘); 47 }; 48 49 var rs = new ReadStream(); 50 var ws = new WritStream(); 51 var ts = new TransformStream(); 52 53 rs.pipe(ts).pipe(ws);
标签:
原文地址:http://www.cnblogs.com/lqcdsns/p/5400493.html