Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed unfixed fix

  • Loading branch information...
commit 3de869b41e3ae25fbe85546b3c45c24213fa9032 1 parent a37fa03
@gagle authored
Showing with 44 additions and 16 deletions.
  1. +1 −1  build/buffered-reader.js
  2. +15 −0 examples/seek.js
  3. +28 −15 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};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.interrupt=function(){this._interrupted=!0},BufferedReader.prototype.read=function(){var a=FS.createReadStream(this._fileName,this._settings),b,c,d=this,e=this.listeners("character").length!==0||this.listeners("line").length!==0||this.listeners("byte").length!==0;a.on("data",function(f){c=f;var g=0,h,i,j=f.length;if(e){for(var k=0;k<j;k++){if(d._interrupted)break;i=f[k];if(!a.encoding){d.emit("byte",i);continue}d.emit("character",i==="\r"?"\n":i);if(i==="\n"||i==="\r")h=f.slice(g,k),g=k+1,b&&(h=b.concat(h),b=null),k+1!==j&&i==="\r"&&f[k+1]==="\n"&&k++,d.emit("line",h)}if(a.encoding&&g!==j){var l=g===0?f:f.slice(g);b=b?b.concat(l):l}}d.emit("buffer",f),d._interrupted&&(d._interrupted=!1,a.destroy(),d.emit("end"))}),a.on("end",function(){d._interrupted=!1,e&&b&&d.emit("line",b),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));a+=this._settings.start;if(a>=this._settings.end+1)this._isEOF=!0;else{this._isEOF=!1;var c=this._fileOffset-this._buffer.length;a>=c&&a<this._fileOffset?(this._bufferOffset=a-c,this._realOffset=a):(this._needRead=!0,this._noMoreBuffers=!1,this._fileOffset=a,this._bufferOffset=0,this._realOffset=a)}b(null)},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};BufferedReader.prototype=Object.create(EVENTS.EventEmitter.prototype),BufferedReader.prototype.constructor=BufferedReader,BufferedReader.prototype.interrupt=function(){this._interrupted=!0},BufferedReader.prototype.read=function(){var a=FS.createReadStream(this._fileName,this._settings),b,c,d=this,e=this.listeners("character").length!==0||this.listeners("line").length!==0||this.listeners("byte").length!==0;a.on("data",function(f){c=f;var g=0,h,i,j=f.length;if(e){for(var k=0;k<j;k++){if(d._interrupted)break;i=f[k];if(!a.encoding){d.emit("byte",i);continue}d.emit("character",i==="\r"?"\n":i);if(i==="\n"||i==="\r")h=f.slice(g,k),g=k+1,b&&(h=b.concat(h),b=null),k+1!==j&&i==="\r"&&f[k+1]==="\n"&&k++,d.emit("line",h)}if(a.encoding&&g!==j){var l=g===0?f:f.slice(g);b=b?b.concat(l):l}}d.emit("buffer",f),d._interrupted&&(d._interrupted=!1,a.destroy(),d.emit("end"))}),a.on("end",function(){d._interrupted=!1,e&&b&&d.emit("line",b),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
15 examples/seek.js
@@ -0,0 +1,15 @@
+var BufferedReader = require ("../src/buffered-reader");
+
+new BufferedReader ("file", { start: 3, end: 6 }).seek (1, function (error){
+ if (error) return console.log (error);
+
+ this.readBytes (10, function (error, bytes, bytesRead){
+ if (error) return console.log (error);
+ console.log (bytes);
+ console.log ("bytes read: " + bytesRead);
+
+ this.close (function (error){
+ if (error) console.log (error);
+ });
+ });
+});
View
43 src/buffered-reader.js
@@ -328,24 +328,37 @@ BufferedReader.prototype.seek = function (offset, cb){
cb = cb.bind (this);
if (offset < 0) return cb (new Error (INVALID_SEEK_OFFSET));
- offset += this._settings.start;
- if (offset >= this._settings.end + 1){
- this._isEOF = true;
- }else{
- this._isEOF = false;
- var start = this._fileOffset - this._buffer.length;
- if (offset >= start && offset < this._fileOffset){
- this._bufferOffset = offset - start;
- this._realOffset = offset;
+ var seek = function (){
+ offset += me._settings.start;
+ if (offset >= me._settings.end + 1){
+ me._isEOF = true;
}else{
- this._needRead = true;
- this._noMoreBuffers = false;
- this._fileOffset = offset;
- this._bufferOffset = 0;
- this._realOffset = offset;
+ me._isEOF = false;
+ var start = me._fileOffset - (me._buffer ? me._buffer.length : 0);
+ if (offset >= start && offset < me._fileOffset){
+ me._bufferOffset = offset - start;
+ me._realOffset = offset;
+ }else{
+ me._needRead = me._fd ? true : false;
+ me._noMoreBuffers = false;
+ me._fileOffset = offset;
+ me._bufferOffset = 0;
+ me._realOffset = offset;
+ }
}
+ cb (null);
+ };
+
+ var me = this;
+ if (!this._initialized){
+ this._init (function (error){
+ if (error) return cb (error, null);
+ me._initialized = true;
+ seek ();
+ });
+ }else{
+ seek ();
}
- cb (null);
};
BufferedReader.prototype.skip = function (bytes, cb){
Please sign in to comment.
Something went wrong with that request. Please try again.