一起读nodejs(九)----缓存类(Buffer)
本文是对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'
) into 0x20
(character code of a space). If you want to convert a null character into 0x00
, 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 of Buffer
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进制字符.
var b = new Buffer(50);b.fill("h");buf.INSPECT_MAX_BYTES