对buffer.write方法在Node.js的使用说明
方法显示:用指定的编码将字符串写入缓冲区的偏移量。
返回写入的八进制字节数。
如果Buffer没有足够的空间来适应整个字符串,那么只有字符串的一部分将被写入。
Grammar:
复制代码代码如下所示:
buffer.write(String,{偏},{长},{编码})
接收参数:
字符串,写入缓冲区的数据。
位移的优点,可选,默认0。数据被写入缓冲区的位置。
长数,可选,默认:buffer.length u2013偏移,写数据的长度
编码字符串的编码格式,你需要使用,可选,默认是UTF8。
实例uff1a
复制代码代码如下所示:
但是,新的缓冲区(256);
len = buf.write( u00bd + u00bc = u00be ',0);
console.log(len +字节+ buf.tostring('utf8 ',0,Len));
源代码 uff1a
复制代码代码如下所示:
buffer.prototype.write =函数(字符串、偏移、长度、编码){
允许写(字符串,编码)
如果(util.isstring(偏移)util.isundefined(长度)){
编码=偏移量;
偏移量=0;
允许写(字符串,偏移量{,长度},编码)
} else if(需(偏移)){
偏移量;
如果(需(长度)){
长度=长度;
{人}
编码=长度;
长度=未定义;
}
/ / XXX遗产写(字符串编码、偏移、长度)-去除v0.13
{人}
如果(!writewarned){
如果(过程。throwdeprecation)
把新的错误(writemsg);
如果(过程。tracedeprecation)
Console.trace(writemsg);
其他的
Console.error(writemsg);
writewarned =真;
}
var =编码;
编码=偏移量;
偏移长度;
长度=交换;
}
VaR剩余= this.length偏移;
如果(util.isundefined(长度)| |长度>剩余)
长度=剩余;
编码=!!编码(编码+)。ToLowerCase():'utf8;
如果(string.length > 0(长度<0 | |偏移量<0))
把新的RangeError('attempt写超出缓冲区范围);
VaR RET;
开关(编码){
case'hex:
ret = this.hexwrite(字符串偏移量,长度);
打破;
case'utf8:
case'utf-8:
ret = this.utf8write(字符串偏移量,长度);
打破;
case'ascii:
ret = this.asciiwrite(字符串偏移量,长度);
打破;
case'binary:
ret = this.binarywrite(字符串偏移量,长度);
打破;
case'base64:
警告:最大长度不考虑 / base64write
ret = this.base64write(字符串偏移量,长度);
打破;
case'ucs2:
case'ucs-2:
case'utf16le:
case'utf-16le:
ret = this.ucs2write(字符串偏移量,长度);
打破;
违约:
把新的TypeError('unknown编码:+编码);
}
返回页首;
};