Skip to content

Commit

Permalink
v0.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Jul 25, 2012
1 parent 9e384fa commit e3183a3
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Node BufferedReader

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

Version: 0.2.4
Version: 0.2.5

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.

18 changes: 4 additions & 14 deletions examples/all-in-one.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (bufferedReader, error, cb){
console.log (error);
if (!bufferedReader) return cb ();

bufferedReader.close (function (error){
if (error) console.log (error);
cb ();
});
};

var offset;

new BufferedReader ("lorem ipsum", { encoding: "utf8" })
Expand All @@ -24,15 +14,15 @@ new BufferedReader ("lorem ipsum", { encoding: "utf8" })
})
.on ("end", function (){
this.seek (offset, function (error){
if (error) return handleError (null, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

this.readBytes (9, function (error, bytes, bytesRead){
if (error) return handleError (this, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

console.log (bytes.toString ());
console.log (bytes.toString ()); //Prints: Curabitur

this.close (function (error){
if (error) handleError (null, error, function (){ /* Error correctly managed. */ });
if (error) console.log (error);
});
});
})
Expand Down
6 changes: 1 addition & 5 deletions examples/binary.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (error){
console.log (error);
};

new BufferedReader ("lorem ipsum 2")
.on ("error", function (error){
handleError (error);
console.log (error);
})
.on ("byte", function (b, byteOffset){
console.log ("byte: " + b + ", offset: " + byteOffset);
Expand Down
6 changes: 1 addition & 5 deletions examples/byteOffset.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (error){
console.log (error);
};

console.log ("First line, expected offset: 21");
console.log ("Second line, expected offset: 61\n");

new BufferedReader ("bmp", { encoding: "utf8", bufferSize: 4 })
.on ("error", function (error){
handleError (error);
console.log (error);
})
.on ("character", function (character, byteOffset){
console.log ("character: " + character + ", offset: " + byteOffset);
Expand Down
8 changes: 2 additions & 6 deletions examples/characters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ Two ways to read one character at a time:
- Using the "character" event.
*/

var handleError = function (error){
console.log (error);
};

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

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

new BufferedReader ("lorem ipsum 2", { encoding: "utf8" })
.on ("error", function (error){
handleError (error);
console.log (error);
})
.on ("character", function (character){
console.log ("character: " + character);
Expand Down
30 changes: 10 additions & 20 deletions examples/chunkBytes.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (bufferedReader, error, cb){
console.log (error);
if (!bufferedReader) return cb ();

bufferedReader.close (function (error){
if (error) console.log (error);
cb ();
});
};

console.log ("File size: 16");

new BufferedReader ("file").readBytes (10, function (error, bytes, bytesRead){
if (error) return handleError (this, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

console.log (bytes);
console.log ("Bytes read: " + bytesRead);
console.log (bytes); //Prints: 0x00 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 0x99
console.log ("Bytes read: " + bytesRead); //Prints: 10

this.readBytes (8, function (error, bytes, bytesRead){
if (error) return handleError (this, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

console.log (bytes);
console.log ("Bytes read: " + bytesRead);
console.log (bytes); //Prints: 0xaa 0xbb 0xcc 0xdd 0xee 0xff
console.log ("Bytes read: " + bytesRead); //Prints: 6

this.readBytes (4, function (error, bytes, bytesRead){
if (error) return handleError (this, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

//No more bytes, reached the end of the file
console.log (bytes);
console.log ("Bytes read: " + bytesRead);
console.log (bytes); //Prints: null
console.log ("Bytes read: " + bytesRead); //Prints: 0

this.close (function (error){
if (error) handleError (null, error, function (){ /* Error correctly managed. */ });
if (error) console.log (error);
});
});
});
Expand Down
6 changes: 1 addition & 5 deletions examples/readLine.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (error){
console.log (error);
};

new BufferedReader ("lorem ipsum", { encoding: "utf8" })
.on ("error", function (error){
handleError (error);
console.log (error);
})
.on ("line", function (line){
console.log ("line: " + line);
Expand Down
20 changes: 5 additions & 15 deletions examples/seek.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
var BufferedReader = require ("../build/buffered-reader");

var handleError = function (bufferedReader, error, cb){
console.log (error);
if (!bufferedReader) return cb ();

bufferedReader.close (function (error){
if (error) console.log (error);
cb ();
});
};

new BufferedReader ("file", { start: 3, end: 6 }).seek (1, function (error){
if (error) return handleError (null, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

this.readBytes (10, function (error, bytes, bytesRead){
if (error) return handleError (this, error, function (){ /* Error correctly managed. */ });
if (error) return console.log (error);

console.log (bytes);
console.log ("bytes read: " + bytesRead);
console.log (bytes); //Prints: 0x44 0x55 0x66
console.log ("bytes read: " + bytesRead); //Prints: 3

this.close (function (error){
if (error) handleError (null, error, function (){ /* Error correctly managed. */ });
if (error) console.log (error);
});
});
});
Loading

0 comments on commit e3183a3

Please sign in to comment.