本文是对nodejs0.8.9版本的api开发手册解读.nodejs网址
缓存类(Buffer)
stability:3 - Stable
纯javascript对Unicode支持不叫不错,但是对二进制数据的支持就不怎么样了,当需要处理TCP流或者文件系统时,是必须要处理八进制流(octet streams).Node有几种策略来操作,创建,销毁八进制值流.
原始数据被储存在buffer类的实例中,一个buffer很想一个integer的数组,但是符合一个在v8堆栈外的原始内存分配.一个buffer是不能被改变大小的.
buffer类是全局的,尽量不要一有需要就使用require('buffer')加载.
在buffers和javascript string对象之间转换需要显示的调用一个编码函数.下面是string类型不懂的编码格式:
'ascii'
- for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set. Note that this encoding converts a null character ('\0'
or'\u0000'
) into0x20
(character code of a space). If you want to convert a null character into0x00
, you should use'utf8'
.
'utf8'
- Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.多字节编码的Unicode字符.很多web页面和其他文档都使用UTF-8格式.
'utf16le'
- 2 or 4 bytes, little endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.2个或者4个字节,小字节编码的Unicode字符.代理对的支持范围是10000~10FFFF.
'ucs2'
- Alias of'utf16le'
.utf16le的别名
'base64'
- Base64 string encoding.基于64的字符串编码.
'binary'
- A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor ofBuffer
objects where possible. This encoding will be removed in future versions of Node.一种仅使用每个字符的前8个字节把原始二进制数据编码成strings.这个方法已经过时,应该避免在使用buffer对象时使用,这种编码将会来未来的node版本中被移除.
'hex'
- Encode each byte as two hexadecimal characters.将每一个字节编码称两个16进制字符.
buf = new Buffer(256);
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
写入的字符个数(可能和写入的字节数不一样)被保存在Buffer._charWritten变量中,并且将会在下次调用buf.writer()时被覆盖.
str = "node.js";
buf = new Buffer(str.length);
for (var i = 0; i < str.length ; i++) {
buf[i] = str.charCodeAt(i);
}
console.log(buf);
// node.js
obj
Object 一个对象string
String 一个字符串encoding
String, Optional, Default: 'utf8' 编码,可选,默认'utf8'str = '\u00bd + \u00bc = \u00be';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes
list
ArrayList of Buffer objects to concat 一个数组,需要连接的buffer对象的一个数组.totalLength
NumberTotal length of the buffers when concatenated 这个连接后的beffer对象的总长度.buf = new Buffer(1234);
console.log(buf.length);
buf.write("some string", 0, "ascii");
console.log(buf.length);
// 1234
// 1234
targetBuffer
Buffer object - Buffer to copy into 复制的目标buffer对象targetStart
Number, Optional, Default: 0 在目标buffer的哪个位置开始写入sourceStart
Number, Optional, Default: 0在原buffer的哪个位置开始读取sourceEnd
Number, Optional, Default:buffer.length原buffer读取到哪里结束.
buf1 = new Buffer(26);
buf2 = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf1[i] = i + 97; // 97 is ASCII a
buf2[i] = 33; // ASCII !
}
buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));
// !!!!!!!!qrst!!!!!!!!!!!!!
start
Number, Optional, Default: 0 开始位置,默认0end
Number, Optional, Default:buffer.length 结束位置,默认buffer大小.
var buf1 = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf1[i] = i + 97; // 97 is ASCII a
}
var buf2 = buf1.slice(0, 3);
console.log(buf2.toString('ascii', 0, buf2.length));
buf1[0] = 33;
console.log(buf2.toString('ascii', 0, buf2.length));
// abc
// !bc
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
for (ii = 0; ii < buf.length; ii++) {
console.log(buf.readUInt8(ii));
}
// 0x3
// 0x4
// 0x23
// 0x42
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf.readUInt16BE(0));
console.log(buf.readUInt16LE(0));
console.log(buf.readUInt16BE(1));
console.log(buf.readUInt16LE(1));
console.log(buf.readUInt16BE(2));
console.log(buf.readUInt16LE(2));
// 0x0304
// 0x0403
// 0x0423
// 0x2304
// 0x2342
// 0x4223
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf.readUInt32BE(0));
console.log(buf.readUInt32LE(0));
// 0x03042342
// 0x42230403
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x80;
buf[3] = 0x3f;
console.log(buf.readFloatLE(0));
// 0x01
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(8);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x55;
buf[3] = 0x55;
buf[4] = 0x55;
buf[5] = 0x55;
buf[6] = 0xd5;
buf[7] = 0x3f;
console.log(buf.readDoubleLE(0));
// 0.3333333333333333
value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf.writeUInt8(0x3, 0);
buf.writeUInt8(0x4, 1);
buf.writeUInt8(0x23, 2);
buf.writeUInt8(0x42, 3);
console.log(buf);
// <Buffer 03 04 23 42>
value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf.writeUInt16BE(0xdead, 0);
buf.writeUInt16BE(0xbeef, 2);
console.log(buf);
buf.writeUInt16LE(0xdead, 0);
buf.writeUInt16LE(0xbeef, 2);
console.log(buf);
// <Buffer de ad be ef>
// <Buffer ad de ef be>
value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf.writeUInt32BE(0xfeedface, 0);
console.log(buf);
buf.writeUInt32LE(0xfeedface, 0);
console.log(buf);
// <Buffer fe ed fa ce>
// <Buffer ce fa ed fe>
value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(4);
buf.writeFloatBE(0xcafebabe, 0);
console.log(buf);
buf.writeFloatLE(0xcafebabe, 0);
console.log(buf);
// <Buffer 4f 4a fe bb>
// <Buffer bb fe 4a 4f>
value Number 数字
offset
Number 偏移量noAssert
Boolean, Optional, Default: false 是否忽略offset验证,offset可能超出范围.var buf = new Buffer(8);
buf.writeDoubleBE(0xdeadbeefcafebabe, 0);
console.log(buf);
buf.writeDoubleLE(0xdeadbeefcafebabe, 0);
console.log(buf);
// <Buffer 43 eb d5 b7 dd f9 5f d7>
// <Buffer d7 5f f9 dd b7 d5 eb 43>
value 要填充的值
offset
Number, Optional 偏移量,可选 默认0end
Number, Optional 结束位置 可选.默认buffer大小var b = new Buffer(50);
b.fill("h");