Permalink
Browse files

v0.1.2

  • Loading branch information...
1 parent 895f36c commit 6079bf9f1da6faa6c2e9200b90f4cf0ab0417b07 Gabriel Llamas committed Apr 29, 2012
Showing with 81 additions and 59 deletions.
  1. +1 −1 README.md
  2. +0 −1 build/buffered-reader.js
  3. +0 −22 build/package.json
  4. +3 −3 examples/chunkBytes.js
  5. +20 −0 examples/skipBytes.js
  6. +56 −31 src/buffered-reader.js
  7. +1 −1 src/package.json
View
@@ -7,7 +7,7 @@ Node BufferedReader
[Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)
-Version: 0.1.1
+Version: 0.1.2
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,22 +0,0 @@
-{
- "name": "buffered-reader",
- "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"],
- "author": {
- "name": "Gabriel Llamas"
- },
- "repository": {
- "type": "git",
- "url": "git://github.com/Gagle/Node-BufferedReader.git"
- },
- "engines": {
- "node": "*"
- },
- "licenses": [{
- "type": "MIT",
- "url": "http://www.opensource.org/licenses/mit-license.html"
- }],
- "main": "buffered-reader"
-}
View
@@ -3,19 +3,19 @@ var BufferedReader = require ("../build/buffered-reader");
console.log ("File size: 16");
new BufferedReader ("file").readBytes (10, function (error, bytes, bytesRead){
- if (error) console.log (error);
+ if (error) return console.log (error);
console.log (bytes);
console.log ("Bytes read: " + bytesRead);
this.readBytes (8, function (error, bytes, bytesRead){
- if (error) console.log (error);
+ if (error) return console.log (error);
console.log (bytes);
console.log ("Bytes read: " + bytesRead);
this.readBytes (4, function (error, bytes, bytesRead){
- if (error) console.log (error);
+ if (error) return console.log (error);
//No more bytes, reached the end of the file
console.log (bytes);
View
@@ -0,0 +1,20 @@
+var BufferedReader = require ("../build/buffered-reader");
+
+new BufferedReader ("file").skip (2, function (error, skipped){
+ if (error) return console.log (error);
+
+ //Skipped 0x00, 0x01
+ //Prints 2
+ console.log (skipped);
+
+ this.readBytes (3, function (error, bytes, bytesRead){
+ if (error) return console.log (error);
+
+ //Prints 0x22, 0x33, 0x44
+ console.log (bytes);
+
+ this.close (function (error){
+ if (error) console.log (error);
+ });
+ });
+});
View
@@ -4,8 +4,8 @@
*
* @author Gabriel Llamas
* @created 10/04/2012
- * @modified 25/04/2012
- * @version 0.1.1
+ * @modified 29/04/2012
+ * @version 0.1.2
*/
"use strict";
@@ -175,32 +175,15 @@ BufferedReader.prototype._read = function (cb){
});
};
-BufferedReader.prototype.close = function (cb){
- if (cb) cb = cb.bind (this);
- if (!this._fd){
- if (cb) cb (null);
- return;
- }
-
- var me = this;
- FS.close (this._fd, function (error){
- me._fd = null;
- me._buffer = null;
- if (cb) cb.call (me, error);
- });
-};
-
-BufferedReader.prototype.readBytes = function (bytes, cb){
- cb = cb.bind (this);
- if (bytes < 1) return cb (INVALID_BYTES_RANGE_ERROR, null, -1);
- if (this._eof) return cb (null, null, 0);
-
+BufferedReader.prototype._readBytes = function (bytes, read, cb){
var fill = function (){
var endData = bytes - me._dataOffset;
var endBuffer = me._buffer.length - me._bufferOffset;
var end = endData > endBuffer ? endBuffer : endData;
- me._buffer.copy (data, me._dataOffset, me._bufferOffset, me._bufferOffset + end);
+ if (read){
+ me._buffer.copy (data, me._dataOffset, me._bufferOffset, me._bufferOffset + end);
+ }
me._bufferOffset += end;
if (me._bufferOffset === me._buffer.length) me._bufferOffset = 0;
me._dataOffset += end;
@@ -214,7 +197,7 @@ BufferedReader.prototype.readBytes = function (bytes, cb){
me._eof = true;
end = me._dataOffset;
me._dataOffset = 0;
- cb (null, data.slice (0, end), end);
+ cb (null, read ? data.slice (0, end) : data, end);
}else{
me._read (function (error){
if (error) return cb (error, null, -1);
@@ -226,7 +209,10 @@ BufferedReader.prototype.readBytes = function (bytes, cb){
};
var me = this;
- var data = new Buffer (bytes);
+ var data = null;
+ if (read){
+ data = new Buffer (bytes);
+ }
this._open (function (error, fd){
if (error) return cb (error, null, -1);
@@ -237,17 +223,19 @@ BufferedReader.prototype.readBytes = function (bytes, cb){
var end = me._bufferOffset + bytes;
if (end <= len){
- me._buffer.copy (data, 0, me._bufferOffset, end);
+ if (read){
+ me._buffer.copy (data, 0, me._bufferOffset, end);
+ }
me._bufferOffset = end;
cb (null, data, bytes);
}else{
var last = len - me._bufferOffset;
- if (last !== 0){
+ if (last !== 0 && read){
me._buffer.copy (data, 0, me._bufferOffset, me._bufferOffset + last);
}
if (me._noMoreBuffers){
me._eof = true;
- return cb (null, data.slice (0, last), last);
+ return cb (null, read ? data.slice (0, last) : data, last);
}
me._read (function (error){
@@ -257,12 +245,16 @@ BufferedReader.prototype.readBytes = function (bytes, cb){
var remaining = bytes - last;
if (len <= remaining){
me._eof = true;
- me._buffer.copy (data, last, 0, len);
+ if (read){
+ me._buffer.copy (data, last, 0, len);
+ }
var lastChunk = last + len;
- cb (null, data.slice (0, lastChunk), lastChunk);
+ cb (null, read ? data.slice (0, lastChunk) : data, lastChunk);
}else{
me._bufferOffset = remaining;
- me._buffer.copy (data, last, 0, me._bufferOffset);
+ if (read){
+ me._buffer.copy (data, last, 0, me._bufferOffset);
+ }
cb (null, data, bytes);
}
});
@@ -273,4 +265,37 @@ BufferedReader.prototype.readBytes = function (bytes, cb){
});
};
+BufferedReader.prototype.close = function (cb){
+ if (cb) cb = cb.bind (this);
+ if (!this._fd){
+ if (cb) cb (null);
+ return;
+ }
+
+ var me = this;
+ FS.close (this._fd, function (error){
+ me._fd = null;
+ me._buffer = null;
+ if (cb) cb (error);
+ });
+};
+
+BufferedReader.prototype.readBytes = function (bytes, cb){
+ cb = cb.bind (this);
+ if (bytes < 1) return cb (INVALID_BYTES_RANGE_ERROR, null, -1);
+ if (this._eof) return cb (null, null, 0);
+
+ this._readBytes (bytes, true, cb);
+};
+
+BufferedReader.prototype.skip = function (bytes, cb){
+ cb = cb.bind (this);
+ if (bytes < 1) return cb (INVALID_BYTES_RANGE_ERROR, -1);
+ if (this._eof) return cb (null, 0);
+
+ this._readBytes (bytes, false, function (error, bytes, bytesRead){
+ cb (error, bytesRead);
+ });
+};
+
module.exports = BufferedReader;
View
@@ -1,6 +1,6 @@
{
"name": "buffered-reader",
- "version": "0.1.1",
+ "version": "0.1.2",
"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 6079bf9

Please sign in to comment.