Skip to content
Browse files

Added byteOffset in byte event

  • Loading branch information...
1 parent 8215814 commit 40d52d94ee5e666bb746334e40144c0b85c1e9d4 @gagle committed
Showing with 5 additions and 4 deletions.
  1. +1 −1 build/buffered-reader.js
  2. +2 −2 examples/binary.js
  3. +2 −1 src/buffered-reader.js
View
2 build/buffered-reader.js
@@ -1 +1 @@
-"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,INVALID_BUFFER_SIZE="The buffer size must be greater than 0.",INVALID_START_OFFSET="The start offset must be greater than or equals to 0.",INVALID_END_OFFSET="The end offset must be greater than or equals to 0.",INVALID_RANGE_OFFSET="The end offset must be greater than or equals to the start offset.",INVALID_BYTES_RANGE_ERROR="The number of bytes to read must be greater than 0.",INVALID_SEEK_OFFSET="The offset must be greater than or equals to 0.",NO_FILE_ERROR="The source is not a file.",BufferedReader=function(a,b){EVENTS.EventEmitter.call(this),b=b||{},b.bufferSize===0&&(b.bufferSize=-1),this._settings={bufferSize:b.bufferSize||BUFFER_SIZE,encoding:b.encoding||null,start:b.start||0,end:b.end};if(this._settings.bufferSize<1)throw new Error(INVALID_BUFFER_SIZE);if(this._settings.start<0)throw new Error(INVALID_START_OFFSET);if(this._settings.end<0)throw new Error(INVALID_END_OFFSET);if(this._settings.end<this._settings.start)throw new Error(INVALID_RANGE_OFFSET);this._fileName=a,this._fd=null,this._buffer=null,this._fileOffset=this._settings.start,this._bufferOffset=0,this._dataOffset=0,this._realOffset=this._settings.start,this._fileSize=null,this._initialized=!1,this._interrupted=!1,this._isEOF=!1,this._noMoreBuffers=!1,this._needRead=!1,this._reading=!1};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.interrupt=function(){this._reading&&(this._interrupted=!0)},BufferedReader.prototype.read=function(){this._reading=!0;var a=FS.createReadStream(this._fileName,this._settings),b,c=0,d=this,e=this.listeners("character").length!==0,f=this.listeners("line").length!==0,g=this.listeners("byte").length!==0,h=e||f||g;a.on("data",function(f){var i=0,j,k,l=f.length,m=!1;if(h){for(var n=0;n<l;n++){if(d._interrupted)break;k=f[n];if(!a.encoding){g&&d.emit("byte",k);continue}c+=Buffer.byteLength(k,d._settings.encoding),e&&d.emit("character",k,c);if(k==="\r"){m=!0;continue}k==="\n"&&(j=f.slice(i,m?n-1:n),i=n+1,b&&(j=b.concat(j),b=null),d.emit("line",j,c)),m=!1}if(a.encoding&&i!==l){var o=i===0?f:f.slice(i);b=b?b.concat(o):o}}d.emit("buffer",f,c),d._interrupted&&(d._interrupted=!1,a.destroy(),d.emit("end"))}),a.on("end",function(){d._interrupted=!1,h&&b&&d.emit("line",b,c),this._reading=!1,d.emit("end")}),a.on("error",function(a){d._interrupted=!1,d.emit("error",a)})},BufferedReader.prototype._init=function(a){var b=this;FS.stat(this._fileName,function(c,d){if(c)return a(c);if(d.isFile()){if(b._settings.start>=d.size)return b._isEOF=!0,a(null);!b._settings.end&&b._settings.end!==0&&(b._settings.end=d.size),b._settings.end>=d.size&&(b._settings.end=d.size-1),b._fileSize=d.size,a(null)}else a(new Error(NO_FILE_ERROR))})},BufferedReader.prototype._read=function(a){var b=this,c=this._settings.bufferSize;FS.read(this._fd,this._buffer,0,c,this._fileOffset,function(d,e){if(d)return a(d);b._fileOffset+=e,b._fileOffset===b._fileSize&&(b._noMoreBuffers=!0),e<c&&(b._buffer=b._buffer.slice(0,e)),a(null)})},BufferedReader.prototype._readBytes=function(a,b){if(this._needRead){this._needRead=!1;var c=this;this._read(function(d){if(d)return b(d,null,-1);c._readBytes(a,b)});return}var d=function(){var e=a-c._dataOffset,g=c._buffer.length-c._bufferOffset,h=g<=e?g:e;c._buffer.copy(f,c._dataOffset,c._bufferOffset,c._bufferOffset+h),c._bufferOffset+=h,c._realOffset+=h,c._bufferOffset===c._buffer.length&&(c._bufferOffset=0,c._needRead=!0),c._dataOffset+=h,c._dataOffset===a?(c._dataOffset=0,c._isEOF=c._noMoreBuffers,b(null,f,a)):c._noMoreBuffers?(c._isEOF=!0,h=c._dataOffset,c._dataOffset=0,b(null,f.slice(0,h),h)):(c._needRead=!1,c._read(function(a){if(a)return b(a,null,-1);d()}))},c=this,e=c._settings.end-c._realOffset+1;a=e<a?e:a;if(a===0)return b(null,null,0);var f=new Buffer(a),g=c._buffer.length;if(a<=g){var h=c._bufferOffset+a;if(h<=g)c._buffer.copy(f,0,c._bufferOffset,h),c._bufferOffset=h,c._realOffset+=a,b(null,f,a);else{var i=g-c._bufferOffset;c._realOffset+=i,i!==0&&c._buffer.copy(f,0,c._bufferOffset,c._bufferOffset+i);if(c._noMoreBuffers)return c._isEOF=!0,b(null,f.slice(0,i),i);c._read(function(d){if(d)return b(d,null,-1);g=c._buffer.length;var e=a-i;if(g<=e){c._realOffset+=g,c._isEOF=!0,c._buffer.copy(f,i,0,g);var h=i+g;b(null,f.slice(0,h),h)}else c._realOffset+=e,c._bufferOffset=e,c._buffer.copy(f,i,0,c._bufferOffset),b(null,f,a)})}}else d()},BufferedReader.prototype.close=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=this;FS.close(this._fd,function(c){b._fd=null,b._buffer=null,a&&a(c)})},BufferedReader.prototype.readBytes=function(a,b){b=b.bind(this);if(a<1||this._isEOF)return b(null,null,0);var c=function(){if(d._isEOF)return b(null,null,0);FS.open(d._fileName,"r",function(c,e){if(c)return b(c,null,-1);d._fd=e,d._buffer=new Buffer(d._settings.bufferSize),d._read(function(c){if(c)return b(c,null,-1);d._readBytes(a,b)})})},d=this;if(!this._initialized)this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()});else{if(!this._fd)return c();this._readBytes(a,b)}},BufferedReader.prototype.seek=function(a,b){b=b.bind(this);if(a<0)return b(new Error(INVALID_SEEK_OFFSET));var c=function(){a+=d._settings.start;if(a>=d._settings.end+1)d._isEOF=!0;else{d._isEOF=!1;var c=d._fileOffset-(d._buffer?d._buffer.length:0);a>=c&&a<d._fileOffset?(d._bufferOffset=a-c,d._realOffset=a):(d._needRead=d._fd?!0:!1,d._noMoreBuffers=!1,d._fileOffset=a,d._bufferOffset=0,d._realOffset=a)}b(null)},d=this;this._initialized?c():this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()})},BufferedReader.prototype.skip=function(a,b){b=b.bind(this);if(a<1||this._isEOF)return b(null,0);var c=function(){var c=d._settings.end-d._realOffset+1;a=a<=c?a:c,d.seek(d._realOffset-d._settings.start+a,function(){b(null,a)})},d=this;this._initialized?c():this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()})},module.exports=BufferedReader;
+"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,INVALID_BUFFER_SIZE="The buffer size must be greater than 0.",INVALID_START_OFFSET="The start offset must be greater than or equals to 0.",INVALID_END_OFFSET="The end offset must be greater than or equals to 0.",INVALID_RANGE_OFFSET="The end offset must be greater than or equals to the start offset.",INVALID_BYTES_RANGE_ERROR="The number of bytes to read must be greater than 0.",INVALID_SEEK_OFFSET="The offset must be greater than or equals to 0.",NO_FILE_ERROR="The source is not a file.",BufferedReader=function(a,b){EVENTS.EventEmitter.call(this),b=b||{},b.bufferSize===0&&(b.bufferSize=-1),this._settings={bufferSize:b.bufferSize||BUFFER_SIZE,encoding:b.encoding||null,start:b.start||0,end:b.end};if(this._settings.bufferSize<1)throw new Error(INVALID_BUFFER_SIZE);if(this._settings.start<0)throw new Error(INVALID_START_OFFSET);if(this._settings.end<0)throw new Error(INVALID_END_OFFSET);if(this._settings.end<this._settings.start)throw new Error(INVALID_RANGE_OFFSET);this._fileName=a,this._fd=null,this._buffer=null,this._fileOffset=this._settings.start,this._bufferOffset=0,this._dataOffset=0,this._realOffset=this._settings.start,this._fileSize=null,this._initialized=!1,this._interrupted=!1,this._isEOF=!1,this._noMoreBuffers=!1,this._needRead=!1,this._reading=!1};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.interrupt=function(){this._reading&&(this._interrupted=!0)},BufferedReader.prototype.read=function(){this._reading=!0;var a=FS.createReadStream(this._fileName,this._settings),b,c=0,d=this,e=this.listeners("character").length!==0,f=this.listeners("line").length!==0,g=this.listeners("byte").length!==0,h=e||f||g;a.on("data",function(f){var i=0,j,k,l=f.length,m=!1;if(h){for(var n=0;n<l;n++){if(d._interrupted)break;k=f[n];if(!a.encoding){c++,g&&d.emit("byte",k,c);continue}c+=Buffer.byteLength(k,d._settings.encoding),e&&d.emit("character",k,c);if(k==="\r"){m=!0;continue}k==="\n"&&(j=f.slice(i,m?n-1:n),i=n+1,b&&(j=b.concat(j),b=null),d.emit("line",j,c)),m=!1}if(a.encoding&&i!==l){var o=i===0?f:f.slice(i);b=b?b.concat(o):o}}d.emit("buffer",f,c),d._interrupted&&(d._interrupted=!1,a.destroy(),d.emit("end"))}),a.on("end",function(){d._interrupted=!1,h&&b&&d.emit("line",b,c),this._reading=!1,d.emit("end")}),a.on("error",function(a){d._interrupted=!1,d.emit("error",a)})},BufferedReader.prototype._init=function(a){var b=this;FS.stat(this._fileName,function(c,d){if(c)return a(c);if(d.isFile()){if(b._settings.start>=d.size)return b._isEOF=!0,a(null);!b._settings.end&&b._settings.end!==0&&(b._settings.end=d.size),b._settings.end>=d.size&&(b._settings.end=d.size-1),b._fileSize=d.size,a(null)}else a(new Error(NO_FILE_ERROR))})},BufferedReader.prototype._read=function(a){var b=this,c=this._settings.bufferSize;FS.read(this._fd,this._buffer,0,c,this._fileOffset,function(d,e){if(d)return a(d);b._fileOffset+=e,b._fileOffset===b._fileSize&&(b._noMoreBuffers=!0),e<c&&(b._buffer=b._buffer.slice(0,e)),a(null)})},BufferedReader.prototype._readBytes=function(a,b){if(this._needRead){this._needRead=!1;var c=this;this._read(function(d){if(d)return b(d,null,-1);c._readBytes(a,b)});return}var d=function(){var e=a-c._dataOffset,g=c._buffer.length-c._bufferOffset,h=g<=e?g:e;c._buffer.copy(f,c._dataOffset,c._bufferOffset,c._bufferOffset+h),c._bufferOffset+=h,c._realOffset+=h,c._bufferOffset===c._buffer.length&&(c._bufferOffset=0,c._needRead=!0),c._dataOffset+=h,c._dataOffset===a?(c._dataOffset=0,c._isEOF=c._noMoreBuffers,b(null,f,a)):c._noMoreBuffers?(c._isEOF=!0,h=c._dataOffset,c._dataOffset=0,b(null,f.slice(0,h),h)):(c._needRead=!1,c._read(function(a){if(a)return b(a,null,-1);d()}))},c=this,e=c._settings.end-c._realOffset+1;a=e<a?e:a;if(a===0)return b(null,null,0);var f=new Buffer(a),g=c._buffer.length;if(a<=g){var h=c._bufferOffset+a;if(h<=g)c._buffer.copy(f,0,c._bufferOffset,h),c._bufferOffset=h,c._realOffset+=a,b(null,f,a);else{var i=g-c._bufferOffset;c._realOffset+=i,i!==0&&c._buffer.copy(f,0,c._bufferOffset,c._bufferOffset+i);if(c._noMoreBuffers)return c._isEOF=!0,b(null,f.slice(0,i),i);c._read(function(d){if(d)return b(d,null,-1);g=c._buffer.length;var e=a-i;if(g<=e){c._realOffset+=g,c._isEOF=!0,c._buffer.copy(f,i,0,g);var h=i+g;b(null,f.slice(0,h),h)}else c._realOffset+=e,c._bufferOffset=e,c._buffer.copy(f,i,0,c._bufferOffset),b(null,f,a)})}}else d()},BufferedReader.prototype.close=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=this;FS.close(this._fd,function(c){b._fd=null,b._buffer=null,a&&a(c)})},BufferedReader.prototype.readBytes=function(a,b){b=b.bind(this);if(a<1||this._isEOF)return b(null,null,0);var c=function(){if(d._isEOF)return b(null,null,0);FS.open(d._fileName,"r",function(c,e){if(c)return b(c,null,-1);d._fd=e,d._buffer=new Buffer(d._settings.bufferSize),d._read(function(c){if(c)return b(c,null,-1);d._readBytes(a,b)})})},d=this;if(!this._initialized)this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()});else{if(!this._fd)return c();this._readBytes(a,b)}},BufferedReader.prototype.seek=function(a,b){b=b.bind(this);if(a<0)return b(new Error(INVALID_SEEK_OFFSET));var c=function(){a+=d._settings.start;if(a>=d._settings.end+1)d._isEOF=!0;else{d._isEOF=!1;var c=d._fileOffset-(d._buffer?d._buffer.length:0);a>=c&&a<d._fileOffset?(d._bufferOffset=a-c,d._realOffset=a):(d._needRead=d._fd?!0:!1,d._noMoreBuffers=!1,d._fileOffset=a,d._bufferOffset=0,d._realOffset=a)}b(null)},d=this;this._initialized?c():this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()})},BufferedReader.prototype.skip=function(a,b){b=b.bind(this);if(a<1||this._isEOF)return b(null,0);var c=function(){var c=d._settings.end-d._realOffset+1;a=a<=c?a:c,d.seek(d._realOffset-d._settings.start+a,function(){b(null,a)})},d=this;this._initialized?c():this._init(function(a){if(a)return b(a,null);d._initialized=!0,c()})},module.exports=BufferedReader;
View
4 examples/binary.js
@@ -4,8 +4,8 @@ new BufferedReader ("lorem ipsum 2")
.on ("error", function (error){
console.log (error);
})
- .on ("byte", function (b){
- console.log ("byte: " + b);
+ .on ("byte", function (b, byteOffset){
+ console.log ("byte: " + b + ", offset: " + byteOffset);
})
.on ("buffer", function (buffer){
console.log ("buffer: " + buffer);
View
3 src/buffered-reader.js
@@ -94,7 +94,8 @@ BufferedReader.prototype.read = function (){
character = data[i];
if (!stream.encoding){
- if (onByte) me.emit ("byte", character);
+ byteOffset++;
+ if (onByte) me.emit ("byte", character, byteOffset);
continue;
}

0 comments on commit 40d52d9

Please sign in to comment.
Something went wrong with that request. Please try again.