node.js BufferITeye - AG环亚娱乐

node.js BufferITeye

2019-01-13 17:39:02 | 作者: 昌淼 | 标签: 里边,时分,这个 | 浏览: 1251

在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; were 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];                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章