/**
 * Created by Tony on 2017/4/10.
 */

var fs = require('fs');

module.exports = {
    writeArrayToFile: function(arr, filePath) {
        if (arr && filePath && Array.isArray(arr)) {
            var chunks = [], len = 0;
            for (var i = 0; i < arr.length; i++) {
                var buffer = new Buffer(arr[i]);
                chunks.push(buffer);
                len += buffer.length;
                //
            }
            var resultBuffer = new Buffer(len);
            for(var i=0,size=chunks.length,pos=0;i<size;i++){
                chunks[i].copy(resultBuffer,pos);
                pos += chunks[i].length;
            }
            fs.writeFile(filePath, resultBuffer, function(err){
                if(err) throw err;
                console.log('Write file: ' + filePath + ' ok!');
            });
        }
    }
}