Node.js连接Buffer对象的正确方法

万能的适应各种编码而且正确的拼接Buffer对象的方法是什么呢?我们从Node.js在github上的源码中找出这样一段正确读取文件,并连接buffer对象的方法

var buffers = [];
var nread = 0;
readStream.on('data', function (chunk) {
    buffers.push(chunk);
    nread += chunk.length;
});
readStream.on('end', function () {
    var buffer = null;
    switch(buffers.length) {
        case 0: buffer = new Buffer(0);
            break;
        case 1: buffer = buffers[0];
            break;
        default:
            buffer = new Buffer(nread);
            for (var i = 0, pos = 0, l = buffers.length; i < l; i++) {
                var chunk = buffers[i];
                chunk.copy(buffer, pos);
                pos += chunk.length;
            }
        break;
    }
});

Continue reading

Windows的信号(Signals)

// Signal types
#define SIGINT 2 // interrupt(Ctrl+C中断)
#define SIGILL 4 // illegal instruction – invalid function image(非法指令)
#define SIGFPE 8 // floating point exception(浮点异常)
#define SIGSEGV 11 // segment violation(段错误)
#define SIGTERM 5 // Software termination signal from kill(Kill发出的软件终止)
#define SIGBREAK 21 //Ctrl-Break sequence(Ctrl+Break中断)
#define SIGABRT 22 // abnormal termination triggered by abort call(Abort)