方法说明:
将string使用指定的encoding写入到buffer的offset处。
返回写入了多少个八进制字节。
如果Buffer没有足够的空间来适应整个string,那么将只有string的部分被写入。
语法:
复制代码 代码如下:
buffer.write(string, [offset], [length], [encoding])
接收参数:
string String,被写入buffer的数据.
offet number,可选,默认0.数据写入到buffer的位置.
length Number,可选,默认:buffer.length – offset,要写入数据的长度
encoding String,需要使用的编码格式,可选,默认为”utf8″.
例子:
复制代码 代码如下:
buf = new Buffer(256);
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
源码:
复制代码 代码如下:
Buffer.prototype.write = function(string, offset, length, encoding) {
// allow write(string, encoding)
if (util.isString(offset) && util.isUndefined(length)) {
encoding = offset;
offset = 0;
// allow write(string, offset[, length], encoding)
} else if (isFinite(offset)) {
offset = ~~offset;
if (isFinite(length)) {
length = ~~length;
} else {
encoding = length;
length = undefined;
}
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
} else {
if (!writeWarned) {
if (process.throwDeprecation)
throw new Error(writeMsg);
else if (process.traceDeprecation)
console.trace(writeMsg);
else
console.error(writeMsg);
writeWarned = true;
}
var swap = encoding;
encoding = offset;
offset = ~~length;
length = swap;
}
var remaining = this.length - offset;
if (util.isUndefined(length) || length > remaining)
length = remaining;
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
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.hexWrite(string, offset, length);
break;
case 'utf8':
case 'utf-8':
ret = this.utf8Write(string, offset, length);
break;
case 'ascii':
ret = this.asciiWrite(string, offset, length);
break;
case 'binary':
ret = this.binaryWrite(string, offset, length);
break;
case 'base64':
// Warning: maxLength not taken into account in base64Write
ret = this.base64Write(string, offset, length);
break;
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = this.ucs2Write(string, offset, length);
break;
default:
throw new TypeError('Unknown encoding: ' + encoding);
}
return ret;
};
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。