Node体验之process对象和Buffer对象
一、process对象
1. process对象是一个全局对象,在哪都可以访问它。
2. 通过这个对象的属性和方法可以对当前运行程序的进程进行访问和控制。
3. 部分属性和方法:
argv ,一组包含命令行参数的数组。
execPath ,开启当前进程的绝对路径。
env ,返回用户环境信息。
platform ,返回当前操作系统平台。
cwd() ,返回当前进程的工作目录。
chdir(directory),改变当前进程的工作目录。
memoryUsage() ,返回node进程使用的内存情况,单位是byte。
exit(code),退出。
kill(pid),向进程发送信息结束进程。
stdout ,标准输出流。
stdin ,标准输入流。
stderr ,标准错误流。
示例:
(1)用stdout模仿conso.log
console.log("Hello"); //Hello
//这行代码可等价于
function Log(data) {
process.stdout.write(data);
}
Log("Hello"); //Hello
(2)使用stdin和stdout实现简单键盘输入的运算
var a,b;
process.stdout.write("请输入a的值: ");
process.stdin.resume() ; //默认情况下,输入流是关闭的,所以要先开启输入流
process.stdin.on('data',function (chunk) { //监听用户输入的数据
if(!a){
a = Number(chunk);
process.stdout.write("请输入b的值: ");
}else{
b = Number(chunk);
process.stdout.write("求和结果a + b = "+(a+b));
}
});
Buffer类
1.一个用于更好操作二进制数据的类。
我们在操作文件或网络数据传输的时候,其实都是在操作二进制数据流,node就提供了这个Buffer类来更好的操作,Buffer是一个全局的类。
2.创建Buffer类
方法:
new Buffer(size);
new Buffer(array);
new Buffer(string,[encoding]);
示例:
//方法一
var buf = new Buffer(5); //创建长度为10字节的Buffer实例
console.log(buf); //<Buffer e0 35 26 00 00>
console.log(buf.length); //5
//方法二
var buf1 = new Buffer([1,2,3,4,5]); //通过给定的数组创建buffer实例
console.log(buf1); //<Buffer 01 02 03 04 05>
console.log(buf1.length); //5
buf1[6] = 6;
console.log(buf1); //<Buffer 01 02 03 04 05>,不能再修改数组的长度
//方法三
var buf2 = new Buffer("www.baidu.com","utf-8"); //通过字符串创建buffer实例,utf-8 是默认的编码方式
console.log(buf2); //<Buffer 77 77 77 2e 62 61 69 64 75 2e 63 6f 6d>
console.log(buf2.length); //13
for(var i=0; i<buf2.length; i++){
console.log(buf2[i]); //输出十进制:119 119 119 46 98 97 105 100 117 46 99 111 109
console.log((buf2[i]).toString(16)); //输出十六进制:77 77 77 2e 62 61 69 64 75 2e 63 6f 6d
console.log(String.fromCharCode(buf2[i])); //输出字符串:www baidu com
}
//方法三中:中文字符和英文字符长度不一样
var str = "hello";
var buf3 = new Buffer(str);
console.log(str.length); //5
console.log(buf3.length); //5
var str1 = "你好";
var buf4 = new Buffer(str1);
console.log(str1.length); //2
console.log(buf4.length); //6
3.Buffer类的部分属性和方法
buf.length ,buffer的字节数大小。
buf[index] ,获取或设置在指定index索引位置的8字节内容。
buf.write(string,[offset],[length],[index]),根据指定的偏移位置和编码方式,将长度为length的数据string插入到buffer中。
buf.toString ([encoding],[start],[end]) ,根据encoding参数返回一个解码的string类型。
buf.toJSON(),返回一个JSON表示的Buffer实例,JSON.stringify将会默认调用来序列化这个Buffer实例。
buf.slice([start],[end]) ,返回一个新的buffer,这个buffer将会和原来的buffer引用相同地址,并且,修改这个新的buffer实例切片也会修改原来的buffer。
buf.copy(tatgetBuffer,[targetStart],[sourceStart],[sourceEnd]) ,拷贝Buffer。
示例:
//buf.write(要写入的数据,从第几位开始写,写入的数据的长度,写入的数据的编码方法)
var str3 = "hello world";
console.log(new Buffer(str3)); //<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
var buf5 = new Buffer(5);
buf5.write(str3);
console.log(buf5); //<Buffer 68 65 6c 6c 6f>
buf5.write(str3,2);
console.log(buf5);//<Buffer 68 65 68 65 6c>
//使用buf.tostring
var buf6 = new Buffer('hello');
console.log(buf6.toString()); //hello
console.log(buf6.toString('utf-8',1,3)); //el
var buf7 = new Buffer('你好');
console.log(buf7.toString()); //你好
console.log(buf7.toString('utf-8',1)); //��好(乱码)
console.log(buf7.toJSON()); //{ type: 'Buffer', data: [ 228, 189, 160, 229, 165, 189 ] }
//使用buf.slice()
var buf8 = new Buffer('hello world');
console.log(buf8); //<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
var buf9 = buf8.slice();
console.log(buf9); //<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
var buf10 = buf8.slice(2,6);
console.log(buf10); //<Buffer 6c 6c 6f 20>
//新的buffer将会和原来的buffer引用相同地址,并且,修改这个新的buffer实例切片也会修改原来的buffer。
buf10[0] = 2;
console.log(buf10); //<Buffer 02 6c 6f 20>
console.log(buf9); //<Buffer 68 65 02 6c 6f 20 77 6f 72 6c 64>,改变了原来的Buffer
//使用buf.copy()
var bf = new Buffer('hello');
console.log(bf); //<Buffer 68 65 6c 6c 6f>
var buf11 = new Buffer(10);
bf.copy(buf11);
console.log(buf11); //<Buffer 68 65 6c 6c 6f 00 00 00 00 00>
buf11[3] = 5;
console.log(buf11); //<Buffer 68 65 6c 05 6f 00 00 00 00 00>
console.log(bf); //<Buffer 68 65 6c 6c 6f> ,没有改变原来的buffer
var bf1 = new Buffer('hello');
var buf12 = new Buffer(10);
console.log(bf1); //<Buffer 68 65 6c 6c 6f>
console.log(buf12); //<Buffer 10 00 00 00 08 00 00 00 18 79>
bf1.copy(buf12,2);
console.log(buf12); //<Buffer 10 00 68 65 6c 6c 6f 00 18 79>
bf1.copy(buf12,1,2,4);
console.log(buf12); //<Buffer 10 6c 6c 65 6c 6c 6f 00 18 79>
4.Buffer静态类方法
Buffer.isEncoding(encoding) ,如果给定的编码encoding是有效的,返回true,否则返回false。
Buffer.isBuffer(obj) , 测试obj是否是一个Buffer类。
Buffer.byteLength(string,[encoding]) ,返回string这个字符串真实的byte长度,encoding默认编码是utf-8。
Buffer.concat(list,[totalLength]) ,返回一个保存着将传入Buffer数组中所有buffer对象拼接在一起的buffer对象。
示例:
//判断是否支持当前编码
console.log(Buffer.isEncoding('utf-8')); //true
console.log(Buffer.isEncoding('gbk')); //false
console.log(Buffer.isEncoding('hex')); //true
//判断是否是一个Buffer类
var arr = [1,2,3,4,5];
var buf = new Buffer(5);
console.log(Buffer.isBuffer(arr)); //false
console.log(Buffer.isBuffer(buf)); //true
//字符串真实长度
var buf1 = new Buffer('hello');
console.log(buf1.length); //5
console.log(Buffer.byteLength(buf1)); //5
var buf2 = new Buffer('你好');
console.log(buf2.length); //6
console.log(Buffer.byteLength(buf2)); //6
console.log(Buffer.byteLength(buf2,'ascii')); //6
//合并缓冲区:concat的使用
var str1 = 'hello';
var str2 = 'world';
var list = [new Buffer(str1),new Buffer(str2)];
console.log(list); //[ <Buffer 68 65 6c 6c 6f>, <Buffer 77 6f 72 6c 64> ]
var buf3 = Buffer.concat(list,10);
//var buf3 = Buffer.concat(list,(str1.length+str2.length));
console.log(buf3); //<Buffer 68 65 6c 6c 6f 77 6f 72 6c 64>
5.process对象操作中的buffer
process.stdout.write("请输入内容: ");
process.stdin.resume() ; //开启输入流
process.stdin.on('data',function(chrunk){
console.log(chrunk); //输入:1, 输出:<Buffer 31 0a>
console.log(chrunk.toString()); //转成字符串后输出:1
console.log("输入的内容是: " + chrunk); //此处因为有涉及字符串拼接,会自动调用tostring()方法,所以输出为:输入的内容是:1
});
注:如对本文有任何疑问,欢迎留言。