Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed May 13, 2012
1 parent b5a6cc4 commit 373cbaf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ Node BufferedReader

[Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)

Version: 0.2.0
Version: 0.2.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.

Expand Down
2 changes: 1 addition & 1 deletion build/buffered-reader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/package.json
@@ -1,6 +1,6 @@
{
"name": "buffered-reader",
"version": "0.2.0",
"version": "0.2.1",
"description": "Fully configurable buffered reader.",
"keywords": ["buffer", "reader", "line", "read line", "file", "read file", "read text file",
"read binary file", "binary"],
Expand Down
2 changes: 1 addition & 1 deletion examples/binary.js
Expand Up @@ -2,7 +2,7 @@ var BufferedReader = require ("../build/buffered-reader");

new BufferedReader ("lorem ipsum 2")
.on ("error", function (error){
console.log ("error: " + error);
console.log (error);
})
.on ("byte", function (b){
console.log ("byte: " + b);
Expand Down
8 changes: 4 additions & 4 deletions examples/characters.js
Expand Up @@ -2,16 +2,16 @@ var BufferedReader = require ("../build/buffered-reader");

/**
Two ways to read one character at a time:
- using "buffer" event with a buffer size of 1. Don't do this, it's very inefficient, it's just
- Using the "buffer" event with a buffer size of 1. Don't do this, it's very inefficient, it's just
to give an example of a buffer event and size. Default size is 16KB.
- using "character" event.
- Using the "character" event.
*/

console.log ("\"buffer\" event: ");

new BufferedReader ("lorem ipsum 2", { encoding: "utf8", bufferSize: 1 })
.on ("error", function (error){
console.log ("error: " + error);
console.log (error);
})
.on ("buffer", function (buffer){
console.log ("buffer: " + buffer);
Expand All @@ -21,7 +21,7 @@ new BufferedReader ("lorem ipsum 2", { encoding: "utf8", bufferSize: 1 })

new BufferedReader ("lorem ipsum 2", { encoding: "utf8" })
.on ("error", function (error){
console.log ("error: " + error);
console.log (error);
})
.on ("character", function (character){
console.log ("character: " + character);
Expand Down
5 changes: 4 additions & 1 deletion examples/readLine.js
Expand Up @@ -2,12 +2,15 @@ var BufferedReader = require ("../build/buffered-reader");

new BufferedReader ("lorem ipsum", { encoding: "utf8" })
.on ("error", function (error){
console.log ("error: " + error);
console.log (error);
})
.on ("line", function (line){
console.log ("line: " + line);
if (line === "Phasellus pulvinar mauris in purus consequat vel congue orci hendrerit."){
this.interrupt ();
}
})
.on ("end", function (){
console.log ("EOF");
})
.read ();
19 changes: 10 additions & 9 deletions src/buffered-reader.js
Expand Up @@ -4,8 +4,8 @@
*
* @author Gabriel Llamas
* @created 10/04/2012
* @modified 01/05/2012
* @version 0.2.0
* @modified 13/05/2012
* @version 0.2.1
*/
"use strict";

Expand Down Expand Up @@ -68,14 +68,14 @@ BufferedReader.prototype.read = function (){
var stream = FS.createReadStream (this._fileName, this._settings);

var lastChunk;
var buffer;
var me = this;

var loop = this.listeners ("character").length !== 0 || this.listeners ("line").length !== 0 ||
this.listeners ("byte").length !== 0;
var onChar = this.listeners ("character").length !== 0;
var onLine = this.listeners ("line").length !== 0;
var onByte = this.listeners ("byte").length !== 0;
var loop = onChar || onLine || onByte;

stream.on ("data", function (data){
buffer = data;
var offset = 0;
var chunk;
var character;
Expand All @@ -87,12 +87,13 @@ BufferedReader.prototype.read = function (){

character = data[i];
if (stream.encoding){
me.emit ("character", character === "\r" ? "\n" : character);
if (onChar) me.emit ("character", character === "\r" ? "\n" : character);
}else{
me.emit ("byte", character);
if (onByte) me.emit ("byte", character);
continue;
}

if (!onLine) continue;
if (character === "\n" || character === "\r"){
chunk = data.slice (offset, i);
offset = i + 1;
Expand All @@ -110,7 +111,7 @@ BufferedReader.prototype.read = function (){
}
}

if (stream.encoding && offset !== len){
if (onLine && stream.encoding && offset !== len){
var s = offset === 0 ? data : data.slice (offset);
lastChunk = lastChunk ? lastChunk.concat (s) : s;
}
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
@@ -1,6 +1,6 @@
{
"name": "buffered-reader",
"version": "0.2.0",
"version": "0.2.1",
"description": "Fully configurable buffered reader.",
"keywords": ["buffer", "reader", "line", "read line", "file", "read file", "read text file",
"read binary file", "binary"],
Expand Down

0 comments on commit 373cbaf

Please sign in to comment.