Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Apr 29, 2012
1 parent 895f36c commit 6079bf9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 59 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.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.

Expand Down
1 change: 0 additions & 1 deletion build/buffered-reader.js

This file was deleted.

22 changes: 0 additions & 22 deletions build/package.json

This file was deleted.

6 changes: 3 additions & 3 deletions examples/chunkBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions examples/skipBytes.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
87 changes: 56 additions & 31 deletions src/buffered-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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){
Expand All @@ -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);
}
});
Expand All @@ -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;
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down

0 comments on commit 6079bf9

Please sign in to comment.