Net Buffer Stream
1 NewBuffer (' Hello Pikachu ')2 //<buffer 6c 6c 6f E7 9a ae e5 8d A1 e4 B8 98>3 4 NewBuffer (' Hello Pikachu ', ' base64 ')5 //<buffer 1d E9 A1 af>6 7 varBUF =NewBuffer (' 123456 '); Console.log (BUF)8 //undefined9 Ten varBUF =NewBuffer (7); Buf.write (' 12345678 '); Console.log (buf.length) One //7, the Out-of-section will be ignored. A - varBUF =NewBuffer ([1,2,3,4]); Console.log (BUF) - //<buffer 04>
Buffer for binary data processing
At the beginning of the length is specified, no matter how the fight, the length will not change
1 varFS = require (' FS ');2 3Fs.readfile (' Imooc_class.jpg ',function(Err, origin_buffer) {4 Console.log (Buffer.isbuffer (Origin_buffer));5 6Fs.writefile (' imooc_buffer.jpg ', Origin_buffer,function(err) {7 if(Err) console.log (err);8 });9 Ten varBase64image = origin_buffer.tostring (' base64 ')); One Console.log (base64image); A - varDecodedImage =NewBuffer (Base64image, ' base64 '); - Console.log (Buffer.compare (Origin_buffer, DecodedImage)); the -Fs.writefile (' logo_decoded.jpg ', DecodedImage,function(err) { - if(Err) console.log (err); - }); +});
Stream flow
/**********************************/
1 varHTTP = require (' http ');2 varFS = require (' FS ');3 varRequest = require (' request '));4 5Http.createserver (function(req, res) {6 /*fs.readfile (' imooc_class.jpg ', function (err, res) {7 if (err) {8 res.end (' File not exist! ');9 }else {Ten Res.writeheader ($, {' Content-type ': ' text/html '}); One res.end (data); A } - });*/ - the //fs.createreadstream (' imooc_class.jpg '). pipe (res); - - //Crawl page pictures, output to a specified port -Request ('http://imgsrc.baidu.com/forum/w + -%3d580/sign=e2f3b45fa086c91708035231f93c70c6/fb781bd5ad6eddc426e3e45c3edbb6fd53663399.jpg '). pipe (res); +}). Listen (8090);
/********************************************/
1 var fs = require (' FS '); 2 3 fs.createreadstream (' 1.mp3 '). Pipe (Fs.createwritestream (' 1-pipe.mp3 '));
/********************************************/
1 varStream = require (' stream ');2 3 varreadable =Stream. readable;4 varwritable =Stream. writable;5 6 //instantiation of7 varReadstream =Newreadable ();8 varWritstream =Newwritable ();9 Ten //Readable stream OneReadstream.push (' I '); AReadstream.push (' Love ')); -Readstream.push (' node\n '); - //Readable stream End theReadstream.push (NULL); - - //Writable Stream -Writstream._write =function(chunk, encode, CB) { + Console.log (chunk.tostring ()); - CB (); + }; A atReadstream.pipe (Writstream);
/***************************************/
1 //Custom Flow2 varStream = require (' stream ');3 //Tool Class4 varUtil = require (' util '));5 6 functionReadstream () {7Stream. Readable.call ( This);8 }9 Ten util.inherits (Readstream, stream. readable); One AReadstream.prototype._read =function () { - //Readable stream - This. push (' I '); the This. push (' love '); - This. push (' node\n '); - //Readable stream End - This. Push (NULL); + }; - + functionWritstream () { AStream. Writable.call ( This); at This. _cached =NewBuffer (' '); - } - - util.inherits (Writstream, stream. writable); - -Writstream.prototype._write =function(chunk, encode, CB) { in Console.log (chunk.tostring ()); - CB (); to }; + - //Convert Stream the functionTransformstream () { *Stream. Transform.call ( This); $ }Panax Notoginseng - util.inherits (Transformstream, stream. Transform); the +Transformstream.prototype._transform =function(chunk, encode, CB) { A This. push (chunk); the CB (); + }; - $Transformstream.prototype._flush =function(CB) { $ This. Push (' Oh Yeah '); - }; - the varrs =NewReadstream (); - varWS =NewWritstream ();Wuyi varTS =NewTransformstream (); the -Rs.pipe (TS). pipe (WS);
node. JS (vi) "Buffer" "Stream"