在js中基础类型没有二进制byte类型,但是js提供了ArrayBuffer群来处理各种二进制的存储,而node.js也为我们封装了一个用于存储二进制的通用Buffer类,这里说说Buffer。
1.Buffer构造:
function Buffer(subject, encoding, offset) {
if (!(this instanceof Buffer)) {
return new Buffer(subject, encoding, offset);
}
var type;
// Are we slicing?
if (typeof offset === 'number') {
if (!Buffer.isBuffer(subject)) {
throw new TypeError('First argument must be a Buffer when slicing');
}
this.length = +encoding > 0 ? Math.ceil(encoding) : 0;
this.parent = subject.parent ? subject.parent : subject;
this.offset = offset;
} else {
// Find the length
switch (type = typeof subject) {
case 'number':
this.length = +subject > 0 ? Math.ceil(subject) : 0;
break;
case 'string':
this.length = Buffer.byteLength(subject, encoding);
break;
case 'object': // Assume object is array-ish
this.length = +subject.length > 0 ? Math.ceil(subject.length) : 0;
break;
default:
throw new TypeError('First argument needs to be a number, ' +
'array or string.');
}
// Buffer.poolSize 这里的poolSize默认大小是8KB
if (this.length > Buffer.poolSize) {
// Big buffer, just alloc one. 大于8kb的buffer将重新分配内存
this.parent = new SlowBuffer(this.length); // SlowBuffer才是真正存储的地方,这里的长度为buffer的真实长度,大于8KB
this.offset = 0;
} else if (this.length > 0) {
// Small buffer. 当new的buffer小于8KB的时候,就会把多个buffer放到同一个allocPool 的SlowBuffer里面
if (!pool || pool.length - pool.used < this.length) allocPool(); // 当前已有的allocPool大小不够,如是重新分配一个allocPool,新分配的allocPool成为当前活动的allocPool
this.parent = pool;
this.offset = pool.used;
// Align on 8 byte boundary to avoid alignment issues on ARM.
pool.used = (pool.used + this.length + 7) & ~7; // 将pool原本使用的大小变成8的倍数,例如你实际用了9byte,它会说你用了16byte,这样你就浪费了7byte,目的是提供性能。
} else {
// Zero-length buffer 如果当前pool够放,就直接放进去
this.parent = zeroBuffer;
this.offset = 0;
}
// 下面这部分是写操作
// optimize by branching logic for new allocations
if (typeof subject !== 'number') {
if (type === 'string') {
// We are a string
this.length = this.write(subject, 0, encoding);
// if subject is buffer then use built-in copy method
} else if (Buffer.isBuffer(subject)) {
if (subject.parent)
subject.parent.copy(this.parent,
this.offset,
subject.offset,
this.length + subject.offset);
else
subject.copy(this.parent, this.offset, 0, this.length);
} else if (isArrayIsh(subject)) {
for (var i = 0; i < this.length; i++)
this.parent[i + this.offset] = subject[i];
}
}
}
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
}
我们创建一个buffer的时候,本质上内容是存放在SlowBuffer里面的,由于node.js对小于8KB的buffer做了pool处理,你可以理解为buffer池。正是这个原因出现了几种情况:
1.buffer大于8KB:这种情况下buffer直接使用一个SlowBuffer存放数据,不使用pool存储。
2.buffer小于8KB:小于的时候会检测当前pool够不够放,不够放就重新分配一个pool,然后新分配的pool就成了当前pool。这个时候之前的那个pool里面空出来的内存就直接浪费掉了。
3.如果当前pool足够容纳buffer,就直接放到当前pool里面,一个pool里面可以存放多个buffer。
针对2和3这种pool的情况,如果buffer的长度不是8的倍数,将会自动补齐。这样也会浪费掉一些空间。
Buffer.poolSize = 8 * 1024;
var pool; // 看到没,allocPool的时候新的会把旧的pool直接替换掉,这样旧的pool里面没用到的内存就浪费了。其实这种浪费并不可怕,可怕的是buffer的不正常释放导致整个pool内存无法被gc回收形成真正的内存泄露。
function allocPool() {
pool = new SlowBuffer(Buffer.poolSize);
pool.used = 0;
}
8KB事件:网传的8KB事件其实可以算做是一个buffer的错误用法,直接将buffer的引用置为null,最后用gc进行清理内存。由于pool里面有几个空余的内存无法释放,导致整个pool都无法被回收。这也说明,我们在使用buffer的时候最好手动清空buffer。
2.写入操作:
function Buffer(subject, encoding, offset) :利用构造方法,构造的时候直接传入内容,这个内容可以是多种对象,string,数组,objet等。
concat(list, [totalLength]):这个方法是把list里面的buffer合并到一起。
Buffer.concat = function(list, length) {
if (!Array.isArray(list)) {
throw new TypeError('Usage: Buffer.concat(list, [length])');
}
if (list.length === 0) { // 当list长度为0时,返回一个长度为0的buffer
return new Buffer(0);
} else if (list.length === 1) { // 当list长度为1时,返回list[0];,其实就是自己
return list[0];
}
if (typeof length !== 'number') { // 如果length没有值,就会计算list里面所有buffer的总长度
length = 0;
for (var i = 0; i < list.length; i++) {
var buf = list[i];
length += buf.length;
}
}
var buffer = new Buffer(length); // buffer的分配是固定的,不是可变长的
var pos = 0;
for (var i = 0; i < list.length; i++) { // 把所有的buffer组合到一起
var buf = list[i];
buf.copy(buffer, pos);
pos += buf.length;
}
return buffer;
};
buf.copy(targetBuffer, [targetStart], [sourceStart], [sourceEnd]):
Buffer.prototype.copy = function(target, target_start, start, end) {
// set undefined/NaN or out of bounds values equal to their default
if (!(target_start >= 0)) target_start = 0;
if (!(start >= 0)) start = 0;
if (!(end < this.length)) end = this.length;
// Copy 0 bytes; we're done
if (end === start ||
target.length === 0 ||
this.length === 0 ||
start > this.length)
return 0;
if (end < start)
throw new RangeError('sourceEnd < sourceStart');
if (target_start >= target.length)
throw new RangeError('targetStart out of bounds');
if (target.length - target_start < end - start) // 这里需要注意,如果长度不够源的拷贝就会被截取
end = target.length - target_start + start;
// 最蛋疼的是这句话,parent.copy是啥真没理解,我对js的继承很蛋疼啊,一直搞不懂。有人说这里用的是slowbuffer的copy方法,但是在代码里面没有看到它的copy方法
return this.parent.copy(target.parent || target,
target_start + (target.offset || 0),
start + this.offset,
end + this.offset);
};
target:拷贝到的目标位置,这里需要注意的是目标buffer内存的大小是需要比源大的,否则不会拷贝的。
target_start:目标的起始位置
start:源的起始位置
end:源的结束位置,不能超过length,超过就设置为length。
这里需要注意的是:1.如果根本不能执行拷贝,会报异常这个还好。2.能拷贝但是目标的存放位置不够,这个时候就会出现截取,这个肯定不是我们想看到的,也是需要注意的。
Buffer.prototype.write = function(string, offset, length, encoding):
Buffer.prototype.write = function(string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
if (isFinite(offset)) {
if (!isFinite(length)) {
encoding = length;
length = undefined;
}
} else { // legacy
var swap = encoding;
encoding = offset;
offset = length;
length = swap;
}
offset = +offset || 0;
var remaining = this.length - offset;
if (!length) {
length = remaining;
} else {
length = +length;
if (length > remaining) {
length = remaining;
}
}
encoding = String(encoding || 'utf8').toLowerCase(); // 这个是关键,这说明js的String对象写入到buffer的时候,默认字符编码为utf-8
if (string.length > 0 && (length < 0 || offset < 0))
throw new RangeError('attempt to write beyond buffer bounds');
// 在这之上的是对参数进行处理,在这之下的是写入操作,针对不同的编码格式调用不同的方法。
var ret;
switch (encoding) {
case 'hex':
ret = this.parent.hexWrite(string, this.offset + offset, length);
break;
case 'utf8':
case 'utf-8':
ret = this.parent.utf8Write(string, this.offset + offset, length);
break;
case 'ascii':
ret = this.parent.asciiWrite(string, this.offset + offset, length);
break;
case 'binary':
ret = this.parent.binaryWrite(string, this.offset + offset, length);
break;
case 'base64':
// Warning: maxLength not taken into account in base64Write
ret = this.parent.base64Write(string, this.offset + offset, length);
break;
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = this.parent.ucs2Write(string, this.offset + offset, length);
break;
default:
throw new TypeError('Unknown encoding: ' + encoding);
}
Buffer._charsWritten = SlowBuffer._charsWritten;
return ret;
};
// 实现很简单,调用了底层的写入方法
Buffer.prototype.utf8Write = function(string, offset) {
return this.write(string, offset, 'utf8');
};
Buffer.prototype.binaryWrite = function(string, offset) {
return this.write(string, offset, 'binary');
};
Buffer.prototype.asciiWrite = function(string, offset) {
return this.write(string, offset, 'ascii');
};
buf.fill(value, [offset], [end]):填充,填充的内容是value的内容,如果value是字符串的话,填充的是value = value.charCodeAt(0);值。
3.读取操作:
SlowBuffer.prototype.toString = function(encoding, start, end):大同小异这里只是贴出来,最喜的是默认为utf-8格式,因为我客户端传过来的数据就是utf-8,这样就不用转换了。
SlowBuffer.prototype.toString = function(encoding, start, end) {
encoding = String(encoding || 'utf8').toLowerCase();
start = +start || 0;
if (typeof end !== 'number') end = this.length;
// Fastpath empty strings
if (+end == start) {
return '';
}
switch (encoding) {
case 'hex':
return this.hexSlice(start, end);
case 'utf8':
case 'utf-8':
return this.utf8Slice(start, end);
case 'ascii':
return this.asciiSlice(start, end);
case 'binary':
return this.binarySlice(start, end);
case 'base64':
return this.base64Slice(start, end);
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return this.ucs2Slice(start, end);
default:
throw new TypeError('Unknown encoding: ' + encoding);
}
};
Buffer.prototype.utf8Slice = function(start, end) {
return this.toString('utf8', start, end);
};
Buffer.prototype.binarySlice = function(start, end) {
return this.toString('binary', start, end);
};
Buffer.prototype.asciiSlice = function(start, end) {
return this.toString('ascii', start, end);
};
Buffer.prototype.utf8Write = function(string, offset) {
return this.write(string, offset, 'utf8');
};
Buffer.prototype.binaryWrite = function(string, offset) {
return this.write(string, offset, 'binary');
};
Buffer.prototype.asciiWrite = function(string, offset) {
return this.write(string, offset, 'ascii');
};
Buffer.prototype.toJSON:把buffer直接转换成json对象输出
Buffer.prototype.toJSON = function() {
return Array.prototype.slice.call(this, 0);
};
Buffer.prototype.slice = function(start, end) :这个方法感觉跟拷贝差不多,但是算是各有利弊吧。它在操作的同时不会损害原有的buffer里面的内容。
Buffer.prototype.get = function get(offset) {
if (offset < 0 || offset >= this.length)
throw new RangeError('offset is out of bounds');
return this.parent[this.offset + offset];
};
Buffer.prototype.set = function set(offset, v) {
if (offset < 0 || offset >= this.length)
throw new RangeError('offset is out of bounds');
return this.parent[this.offset + offset] = v;
};
这两个方法也是读写方法,而且还比较简洁,如果对buffer对象自身进行操作可以用这个。