Permalink
Browse files

Updated BufferedWriter

  • Loading branch information...
1 parent b48b629 commit bce5782e08f38d3f5457bc815dd3ad60565d64e3 Gabriel Llamas committed May 4, 2012
@@ -1 +1 @@
-"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,EOL=process.platform.indexOf("win")!==-1?new Buffer([13,10]):new Buffer([10]),INVALID_BUFFER_SIZE=new Error("The buffer size must be greater than 0."),BufferedWriter=function(a,b){EVENTS.EventEmitter.call(this),b=b||{},b.bufferSize===0&&(b.bufferSize=-1),this._settings={bufferSize:b.bufferSize||BUFFER_SIZE,encoding:b.encoding||null,append:b.append?"a":"w"};if(this._settings.bufferSize<1)throw INVALID_BUFFER_SIZE;this._fileName=a,this._stream=null,this._buffer=null,this._bufferOffset=0};BufferedWriter.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedWriter.prototype.constructor=BufferedWriter,BufferedWriter.prototype._flush=function(){this._stream.write(new Buffer(this._bufferOffset!==this._settings.bufferSize?this._buffer.slice(0,this._bufferOffset):this._buffer)),this._bufferOffset=0},BufferedWriter.prototype._canWrite=function(a){return a+this._bufferOffset>this._settings.bufferSize&&(a=this._settings.bufferSize-this._bufferOffset),a},BufferedWriter.prototype._write=function(a,b,c){var d=this;this._buffer=new Buffer(this._settings.bufferSize),this._stream=FS.createWriteStream(this._fileName,{flags:d._settings.append}),this._stream.on("error",function(a){d.emit(a)}),BufferedWriter.prototype._write=function(a,b,c){var d=this._canWrite(c);a.copy(this._buffer,this._bufferOffset,b,b+d),this._bufferOffset+=d,b+=d,c-=d,this._bufferOffset===this._settings.bufferSize&&(this._flush(),c!==0&&this._write(a,b,c))},this._write(a,b,c)},BufferedWriter.prototype.close=function(a){if(!this._stream)return;this._bufferOffset!==0&&this._flush();var b=this;this._stream.on("close",function(){a&&a.call(this)}),this._stream.destroySoon(),this._stream=null,this._buffer=null},BufferedWriter.prototype.newLine=function(){return this._write(EOL,0,EOL.length),this};var isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"},toHexArray=function(a){var b=[];do b.unshift(a&255),a>>>=8;while(a);return b};BufferedWriter.prototype.write=function(a,b,c){var d=typeof a;if(d==="number")b=0,a=toHexArray(a),c=a.length,a=new Buffer(a);else if(d==="string")b=0,c=Buffer.byteLength(a,this._settings.encoding),a=new Buffer(a,this._settings.encoding);else{isArray(a)&&(a=new Buffer(a));var e=arguments.length;e===1&&(b=0,c=1)}return this._write(a,b,c),this},module.exports=BufferedWriter;
+"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,EOL=process.platform.indexOf("win")!==-1?new Buffer([13,10]):new Buffer([10]),INVALID_BUFFER_SIZE=new Error("The buffer size must be greater than 0."),BufferedWriter=function(a,b){EVENTS.EventEmitter.call(this),b=b||{},b.bufferSize===0&&(b.bufferSize=-1),this._settings={bufferSize:b.bufferSize||BUFFER_SIZE,encoding:b.encoding||null,append:b.append?"a":"w"};if(this._settings.bufferSize<1)throw INVALID_BUFFER_SIZE;this._fileName=a,this._stream=null,this._buffer=null,this._bufferOffset=0};BufferedWriter.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedWriter.prototype.constructor=BufferedWriter,BufferedWriter.prototype._flush=function(){var a=new Buffer(this._bufferOffset!==this._settings.bufferSize?this._buffer.slice(0,this._bufferOffset):this._buffer);this._stream.write(a),this._bufferOffset=0},BufferedWriter.prototype._canWrite=function(a){return a+this._bufferOffset>this._settings.bufferSize&&(a=this._settings.bufferSize-this._bufferOffset),a},BufferedWriter.prototype._write=function(a,b,c){var d=this;this._stream||(this._buffer=new Buffer(this._settings.bufferSize),this._stream=FS.createWriteStream(this._fileName,{flags:d._settings.append}),this._stream.on("error",function(a){d.emit(a)}));var e=this._canWrite(c);a.copy(this._buffer,this._bufferOffset,b,b+e),this._bufferOffset+=e,b+=e,c-=e,this._bufferOffset===this._settings.bufferSize&&(this._flush(),c!==0&&this._write(a,b,c))},BufferedWriter.prototype.close=function(a){if(!this._stream)return;this._bufferOffset!==0&&this._flush();var b=this;this._stream.on("close",function(){a&&a.call(this)}),this._stream.destroySoon(),this._stream=null,this._buffer=null},BufferedWriter.prototype.newLine=function(){return this._write(EOL,0,EOL.length),this};var isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"},toHexArray=function(a){var b=[];do b.unshift(a&255),a>>>=8;while(a);return b};BufferedWriter.prototype.write=function(a,b,c){var d=typeof a;if(d==="number")b=0,a=toHexArray(a),c=a.length,a=new Buffer(a);else if(d==="string")b=0,c=Buffer.byteLength(a,this._settings.encoding),a=new Buffer(a,this._settings.encoding);else{isArray(a)&&(a=new Buffer(a));var e=arguments.length;e===1&&(b=0,c=1)}return this._write(a,b,c),this},module.exports=BufferedWriter;
@@ -1,6 +1,6 @@
{
"name": "buffered-writer",
- "version": "0.1.5",
+ "version": "0.1.6",
"description": "Fully configurable buffered writer.",
"keywords": [
"buffer",
@@ -34,7 +34,7 @@
"name": "Gagle",
"email": "gaglekas@gmail.com"
},
- "_id": "buffered-writer@0.1.5",
+ "_id": "buffered-writer@0.1.6",
"dependencies": {},
"devDependencies": {},
"optionalDependencies": {},
@@ -43,7 +43,7 @@
"_nodeVersion": "v0.6.16",
"_defaultsLoaded": true,
"dist": {
- "shasum": "d0d0583294240a47654f00f4c9a7d55da51a1f1d"
+ "shasum": "591661b2069841e9fc43aa6e3a5d5b677e084fac"
},
- "_from": "buffered-writer@0.1.5"
+ "_from": "buffered-writer@0.1.6"
}

0 comments on commit bce5782

Please sign in to comment.