Skip to content

Commit

Permalink
4 cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Aug 31, 2013
1 parent 3a565fd commit f47c604
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 19 deletions.
138 changes: 119 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ var Reader = function (path, options){
this._p = 0;
this._b = null;
//start and end are the absolute limits of the buffer
this._start = 0;
this._end = null;
this._size = 0;
this._s = -1;
this._e = -1;
this._size = null;
//If bufferMode is false then file size <= buffer size and all the file is
//read into memory, there's no need to check limits
this._bufferMode = null;
Expand Down Expand Up @@ -96,6 +96,8 @@ Reader.prototype.close = function (){
me.emit ("close");
}
});

return this;
};

Reader.prototype.isEOF = function (){
Expand All @@ -104,26 +106,73 @@ Reader.prototype.isEOF = function (){
return this._p >= this._size;
};

Reader.prototype._inside = function (p){
return p >= this._s && p < this._e;
};

Reader.prototype._dump = function (target, offset, start, end, cb){
var me = this;

var reads = Math.ceil ((end - start)/this._highWaterMark);
var last = (end - start)%this._highWaterMark || this._highWaterMark;

(function read (reads){
if (reads === 1){
//Read to the buffer and copy to the target
fs.read (me._fd, me._b, 0, me._highWaterMark, start,
function (error, bytesRead){
if (error) return cb (error);

//Update the buffer limits
me._s = start;
me._e = start + bytesRead;

//Fill the target buffer
me._b.copy (target, offset, 0, last);

cb ();
});
}else{
//Read to the target
fs.read (me._fd, target, offset, me._highWaterMark, start,
function (error, bytesRead){
if (error) return cb (error);

offset += bytesRead;
start += bytesRead;

read (reads - 1);
});
}
})(reads);
};

Reader.prototype._read = function (bytes, cb){
var me = this;

//Trim the number of bytes to read
if (this._p + bytes >= this._size){
bytes = this._size - this._p;
}

var target = new Buffer (bytes);

if (!this._bufferMode){
//File size <= buffer size
if (!this._b) this._b = new Buffer (this._size);

var read = function (){
var bytesRead = me._p + bytes >= me._size ? me._size - me._p : bytes;
var b = new Buffer (bytesRead);
me._b.copy (b, 0, me._p, me._p + bytesRead);
me._p += bytesRead;
cb (null, bytesRead, b);
me._b.copy (target, 0, me._p, me._p + bytes);
me._p += bytes;
cb (null, bytes, target);
};

if (!this._readFile){
//Read all the file
fs.read (this._fd, this._b, 0, this._size, 0, function (error){
if (error) return cb (error);
me._readFile = true;
//start and end limits don't need to be updated, they're not used
read ();
});
}else{
Expand All @@ -132,22 +181,53 @@ Reader.prototype._read = function (bytes, cb){
}else{
//File size > buffer size
if (!this._b) this._b = new Buffer (this._highWaterMark);
console.log(12123)
cb()

var s = this._p;
var e = this._p + bytes;
var is = this._inside (s);
var ie = this._inside (e);

if (is && ie){console.log("case1")
//Case 1
//The bytes to read are already in the buffer
this._b.copy (target, 0, s - me._s, s - me._s + bytes);
this._p += bytes;
cb (null, bytes, target);
}else if (!is && !ie && !(this._s >= s && this._s <= e)){console.log("case2")
//Case 2
//This is also the case of the very first read
this._dump (target, 0, s, e, function (error){
if (error) return cb (error);
me._p += bytes;
cb (null, bytes, target);
});
}else if (is && !ie){console.log("case3")
//Case 3
//Copy the first bytes already in the buffer
this._b.copy (target, 0, s - me._s, this._e - me._s);
//Read the last bytes
this._dump (target, this._e - s, this._e, e, function (error){
if (error) return cb (error);
me._p += bytes;
cb (null, bytes, target);
});
}else if (!is && ie){console.log("case4")
//Case 4
//Copy the last bytes already in the buffer
this._b.copy (target, me._s - s, 0, e - me._s);
//Read the first bytes
this._dump (target, 0, s, this._s, function (error){
if (error) return cb (error);
me._p += bytes;
cb (null, bytes, target);
});
}
}



/*fs.read (this._fd, new Buffer (bytes), 0, bytes, this._p,
function (error, bytesRead, buffer){
if (error) return cb (error);
me._p += bytesRead;
cb (null, bytesRead, buffer.slice (0, bytesRead));
});*/
};

Reader.prototype.read = function (bytes, cb){
if (!this._q) throw new BinaryReaderError ("The reader is closed");
if (bytes < 1) throw new BinaryReaderError ("Must read one or more bytes");

var me = this;

Expand Down Expand Up @@ -226,4 +306,24 @@ Reader.prototype.tell = function (){
if (!this._q) throw new BinaryReaderError ("The reader is closed");

return this._p;
};

Reader.prototype.debug = function (){
return {
file: {
path: this._path,
fd: this._fd,
size: this._size
},
buffer: {
size: this._highWaterMark,
start: this._s,
end: this._e
},
cursor: {
index: this._p,
isEOF: this._p >= this._size
},
closed: !this._q
}
};
Binary file added s
Binary file not shown.
53 changes: 53 additions & 0 deletions t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var reader = require ("./lib");
var hex = require ("hex");

/*
var br = reader.open (__filename);
br.on ("error", function (error){
console.error (error);
})
.on ("close", function (){
console.log ("closed");
})
.read (10, function (bytesRead, buffer){
console.log (bytesRead, buffer.toString ());
})
.seek (-1, { current: true }, function (){
console.log ("p: " + br.tell ());
})
.read (20, function (bytesRead, buffer){
console.log (bytesRead, buffer.toString ());
})
.close ();
*/

/*
var br = reader.open ("s");
br.on ("error", function (error){
console.error (error);
})
.on ("close", function (){
console.log ("closed");
})
.seek (21, function (){
console.log ("p: " + br.tell ());
})
.read (5, function (bytesRead, buffer){
console.log (bytesRead, buffer);
})
*/


var br = reader.open ("test/file", { highWaterMark: 3 });

br.on ("error", function (error){
console.error (error);
})
.on ("close", function (){
console.log ("closed");
})
.read (5, function (bytesRead, buffer){
console.log (bytesRead, buffer);
})
Binary file added test/file
Binary file not shown.
Loading

0 comments on commit f47c604

Please sign in to comment.