Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v0.1.1

  • Loading branch information...
commit 895f36c2681ca880ffb299f84e6274af7c45da04 1 parent 831a266
@gagle authored
View
2  README.md
@@ -7,7 +7,7 @@ Node BufferedReader
[Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)
-Version: 0.1.0
+Version: 0.1.1
When you need to read a file you typically read a chunk of bytes called "buffer" to avoid multiple calls to the underlying I/O layer, so instead of reading directly from the disk, you read from the previous filled buffer. Doing this you win performance.
View
2  build/buffered-reader.js
@@ -1 +1 @@
-"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,INVALID_BUFFER_SIZE=new Error("The buffer size must be greater than 0."),INVALID_BYTES_RANGE_ERROR=new Error("The number of bytes must be greater than 0."),NO_FILE_ERROR=new Error("The source is not a file."),BufferedReader=function(a,b,c){EVENTS.EventEmitter.call(this);var d=arguments.length;d===1?(b=BUFFER_SIZE,c=null):d===2&&typeof b=="string"&&(c=b,b=BUFFER_SIZE);if(b<1)throw INVALID_BUFFER_SIZE;this._settings={encoding:c,bufferSize:b},this._fileName=a,this._fd=null,this._buffer=null,this._fileOffset=0,this._bufferOffset=0,this._eof=!1,this._noMoreBuffers=!1,this._fileSize=null,this._dataOffset=0};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.read=function(){var a=FS.createReadStream(this._fileName,this._settings),b,c,d=this,e=this.listeners("character").length!==0||this.listeners("line").length!==0||this.listeners("byte").length!==0;a.on("data",function(f){c=f;var g=0,h,i,j=f.length;if(e){for(var k=0;k<j;k++){i=f[k];if(!a.encoding){d.emit("byte",i);continue}d.emit("character",i==="\r"?"\n":i);if(i==="\n"||i==="\r")h=f.slice(g,k),g=k+1,b&&(h=b.concat(h),b=null),k+1!==j&&i==="\r"&&f[k+1]==="\n"&&k++,d.emit("line",h)}if(a.encoding&&g!==j){var l=g===0?f:f.slice(g);b=b?b.concat(l):l}}d.emit("buffer",f)}),a.on("end",function(){e&&b&&d.emit("line",b),d.emit("end")}),a.on("error",function(a){d.emit("error",a)})},BufferedReader.prototype._open=function(a){if(this._fd)return a(null,this._fd);var b=this;FS.stat(this._fileName,function(c,d){if(c)return a(c,null);d.isFile()?FS.open(b._fileName,"r",function(c,e){if(c)return a(c,null);b._fileSize=d.size,b._fd=e,b._buffer=new Buffer(b._settings.bufferSize),b._read(function(c){if(c)return a(c,null);a(null,b._fd)})}):a(NO_FILE_ERROR,null)})},BufferedReader.prototype._read=function(a){var b=this,c=this._settings.bufferSize;FS.read(this._fd,this._buffer,0,c,this._fileOffset,function(d,e){if(d)return a(d);b._bufferValidSize=e,b._fileOffset+=e,b._fileOffset===b._fileSize&&(b._noMoreBuffers=!0),e<c&&(b._buffer=b._buffer.slice(0,e)),a()})},BufferedReader.prototype.close=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=this;FS.close(this._fd,function(c){b._fd=null,b._buffer=null,a&&a.call(b,c)})},BufferedReader.prototype.readBytes=function(a,b){b=b.bind(this);if(a<1)return b(INVALID_BYTES_RANGE_ERROR,null,-1);if(this._eof)return b(null,null,0);var c=function(){var f=a-d._dataOffset,g=d._buffer.length-d._bufferOffset,h=f>g?g:f;d._buffer.copy(e,d._dataOffset,d._bufferOffset,d._bufferOffset+h),d._bufferOffset+=h,d._bufferOffset===d._buffer.length&&(d._bufferOffset=0),d._dataOffset+=h,d._dataOffset===a?(d._dataOffset=0,d._eof=d._noMoreBuffers,b(null,e,a)):d._noMoreBuffers?(d._eof=!0,h=d._dataOffset,d._dataOffset=0,b(null,e.slice(0,h),h)):d._read(function(a){if(a)return b(a,null,-1);c()})},d=this,e=new Buffer(a);this._open(function(f,g){if(f)return b(f,null,-1);var h=d._buffer.length;if(a<h){var i=d._bufferOffset+a;if(i<=h)d._buffer.copy(e,0,d._bufferOffset,i),d._bufferOffset=i,b(null,e,a);else{var j=h-d._bufferOffset;j!==0&&d._buffer.copy(e,0,d._bufferOffset,d._bufferOffset+j);if(d._noMoreBuffers)return d._eof=!0,b(null,e.slice(0,j),j);d._read(function(c){if(c)return b(c,null);h=d._buffer.length;var f=a-j;if(h<=f){d._eof=!0,d._buffer.copy(e,j,0,h);var g=j+h;b(null,e.slice(0,g),g)}else d._bufferOffset=f,d._buffer.copy(e,j,0,d._bufferOffset),b(null,e,a)})}}else c()})},module.exports=BufferedReader;
+"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,INVALID_BUFFER_SIZE=new Error("The buffer size must be greater than 0."),INVALID_BYTES_RANGE_ERROR=new Error("The number of bytes must be greater than 0."),NO_FILE_ERROR=new Error("The source is not a file."),BufferedReader=function(a,b,c){EVENTS.EventEmitter.call(this);var d=arguments.length;d===1?(b=BUFFER_SIZE,c=null):d===2&&typeof b=="string"&&(c=b,b=BUFFER_SIZE);if(b<1)throw INVALID_BUFFER_SIZE;this._settings={encoding:c,bufferSize:b},this._fileName=a,this._interrupted=!1,this._fd=null,this._buffer=null,this._fileOffset=0,this._bufferOffset=0,this._eof=!1,this._noMoreBuffers=!1,this._fileSize=null,this._dataOffset=0};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.interrupt=function(){this._interrupted=!0},BufferedReader.prototype.read=function(){var a=FS.createReadStream(this._fileName,this._settings),b,c,d=this,e=this.listeners("character").length!==0||this.listeners("line").length!==0||this.listeners("byte").length!==0;a.on("data",function(f){c=f;var g=0,h,i,j=f.length;if(e){for(var k=0;k<j;k++){if(d._interrupted)break;i=f[k];if(!a.encoding){d.emit("byte",i);continue}d.emit("character",i==="\r"?"\n":i);if(i==="\n"||i==="\r")h=f.slice(g,k),g=k+1,b&&(h=b.concat(h),b=null),k+1!==j&&i==="\r"&&f[k+1]==="\n"&&k++,d.emit("line",h)}if(a.encoding&&g!==j){var l=g===0?f:f.slice(g);b=b?b.concat(l):l}}d.emit("buffer",f),d._interrupted&&(d._interrupted=!1,a.destroy(),d.emit("end"))}),a.on("end",function(){d._interrupted=!1,e&&b&&d.emit("line",b),d.emit("end")}),a.on("error",function(a){d._interrupted=!1,d.emit("error",a)})},BufferedReader.prototype._open=function(a){if(this._fd)return a(null,this._fd);var b=this;FS.stat(this._fileName,function(c,d){if(c)return a(c,null);d.isFile()?FS.open(b._fileName,"r",function(c,e){if(c)return a(c,null);b._fileSize=d.size,b._fd=e,b._buffer=new Buffer(b._settings.bufferSize),b._read(function(c){if(c)return a(c,null);a(null,b._fd)})}):a(NO_FILE_ERROR,null)})},BufferedReader.prototype._read=function(a){var b=this,c=this._settings.bufferSize;FS.read(this._fd,this._buffer,0,c,this._fileOffset,function(d,e){if(d)return a(d);b._bufferValidSize=e,b._fileOffset+=e,b._fileOffset===b._fileSize&&(b._noMoreBuffers=!0),e<c&&(b._buffer=b._buffer.slice(0,e)),a()})},BufferedReader.prototype.close=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=this;FS.close(this._fd,function(c){b._fd=null,b._buffer=null,a&&a.call(b,c)})},BufferedReader.prototype.readBytes=function(a,b){b=b.bind(this);if(a<1)return b(INVALID_BYTES_RANGE_ERROR,null,-1);if(this._eof)return b(null,null,0);var c=function(){var f=a-d._dataOffset,g=d._buffer.length-d._bufferOffset,h=f>g?g:f;d._buffer.copy(e,d._dataOffset,d._bufferOffset,d._bufferOffset+h),d._bufferOffset+=h,d._bufferOffset===d._buffer.length&&(d._bufferOffset=0),d._dataOffset+=h,d._dataOffset===a?(d._dataOffset=0,d._eof=d._noMoreBuffers,b(null,e,a)):d._noMoreBuffers?(d._eof=!0,h=d._dataOffset,d._dataOffset=0,b(null,e.slice(0,h),h)):d._read(function(a){if(a)return b(a,null,-1);c()})},d=this,e=new Buffer(a);this._open(function(f,g){if(f)return b(f,null,-1);var h=d._buffer.length;if(a<h){var i=d._bufferOffset+a;if(i<=h)d._buffer.copy(e,0,d._bufferOffset,i),d._bufferOffset=i,b(null,e,a);else{var j=h-d._bufferOffset;j!==0&&d._buffer.copy(e,0,d._bufferOffset,d._bufferOffset+j);if(d._noMoreBuffers)return d._eof=!0,b(null,e.slice(0,j),j);d._read(function(c){if(c)return b(c,null);h=d._buffer.length;var f=a-j;if(h<=f){d._eof=!0,d._buffer.copy(e,j,0,h);var g=j+h;b(null,e.slice(0,g),g)}else d._bufferOffset=f,d._buffer.copy(e,j,0,d._bufferOffset),b(null,e,a)})}}else c()})},module.exports=BufferedReader;
View
2  build/package.json
@@ -1,6 +1,6 @@
{
"name": "buffered-reader",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Fully configurable buffered reader.",
"keywords": ["buffer", "reader", "line", "read line", "file", "read file", "read text file",
"read binary file", "binary"],
View
3  examples/readLine.js
@@ -6,5 +6,8 @@ new BufferedReader ("lorem ipsum", "utf8")
})
.on ("line", function (line){
console.log ("line: " + line);
+ if (line === "Phasellus pulvinar mauris in purus consequat vel congue orci hendrerit."){
+ this.interrupt ();
+ }
})
.read ();
View
17 src/buffered-reader.js
@@ -5,7 +5,7 @@
* @author Gabriel Llamas
* @created 10/04/2012
* @modified 25/04/2012
- * @version 0.1.0
+ * @version 0.1.1
*/
"use strict";
@@ -38,6 +38,7 @@ var BufferedReader = function (fileName, bufferSize, encoding){
};
this._fileName = fileName;
+ this._interrupted = false;
this._fd = null;
this._buffer = null;
this._fileOffset = 0;
@@ -51,6 +52,10 @@ var BufferedReader = function (fileName, bufferSize, encoding){
BufferedReader.prototype = Object.create (EVENTS.EventEmitter.prototype);
BufferedReader.prototype.constructor = BufferedReader;
+BufferedReader.prototype.interrupt = function (){
+ this._interrupted = true;
+};
+
BufferedReader.prototype.read = function (){
var stream = FS.createReadStream (this._fileName, this._settings);
@@ -70,6 +75,8 @@ BufferedReader.prototype.read = function (){
if (loop){
for (var i=0; i<len; i++){
+ if (me._interrupted) break;
+
character = data[i];
if (stream.encoding){
me.emit ("character", character === "\r" ? "\n" : character);
@@ -102,17 +109,23 @@ BufferedReader.prototype.read = function (){
}
me.emit ("buffer", data);
+ if (me._interrupted){
+ me._interrupted = false;
+ stream.destroy ();
+ me.emit ("end");
+ }
});
stream.on ("end", function (){
+ me._interrupted = false;
if (loop && lastChunk){
me.emit ("line", lastChunk);
}
-
me.emit ("end");
});
stream.on ("error", function (error){
+ me._interrupted = false;
me.emit ("error", error);
});
};
View
2  src/package.json
@@ -1,6 +1,6 @@
{
"name": "buffered-reader",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Fully configurable buffered reader.",
"keywords": ["buffer", "reader", "line", "read line", "file", "read file", "read text file",
"read binary file", "binary"],
Please sign in to comment.
Something went wrong with that request. Please try again.