Permalink
Browse files

v0.1.1

  • Loading branch information...
Gabriel Llamas
Gabriel Llamas committed Apr 25, 2012
1 parent 831a266 commit 895f36c2681ca880ffb299f84e6274af7c45da04
Showing with 22 additions and 6 deletions.
  1. +1 −1 README.md
  2. +1 −1 build/buffered-reader.js
  3. +1 −1 build/package.json
  4. +3 −0 examples/readLine.js
  5. +15 −2 src/buffered-reader.js
  6. +1 −1 src/package.json
View
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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
@@ -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
@@ -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
@@ -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"],

0 comments on commit 895f36c

Please sign in to comment.