3 var SyncReader = module.exports = function(buffer) {
9 SyncReader.prototype.read = function(length, callback) {
12 length: Math.abs(length), // if length < 0 then at most this length
13 allowLess: length < 0,
18 SyncReader.prototype.process = function() {
20 // as long as there is any data and read requests
21 while (this._reads.length > 0 && this._buffer.length) {
23 var read = this._reads[0];
25 if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) {
27 // ok there is any data so that we can satisfy this request
28 this._reads.shift(); // == read
30 var buf = this._buffer;
32 this._buffer = buf.slice(read.length);
34 read.func.call(this, buf.slice(0, read.length));
43 if (this._reads.length > 0) {
44 return new Error('There are some read requests waitng on finished stream');
47 if (this._buffer.length > 0) {
48 return new Error('unrecognised content at end of stream');