[Service] Integrate DeviceHome and SignalingServer
[platform/framework/web/wrtjs.git] / device_home / node_modules / pngjs / lib / sync-reader.js
1 'use strict';
2
3 var SyncReader = module.exports = function(buffer) {
4
5   this._buffer = buffer;
6   this._reads = [];
7 };
8
9 SyncReader.prototype.read = function(length, callback) {
10
11   this._reads.push({
12     length: Math.abs(length), // if length < 0 then at most this length
13     allowLess: length < 0,
14     func: callback
15   });
16 };
17
18 SyncReader.prototype.process = function() {
19
20   // as long as there is any data and read requests
21   while (this._reads.length > 0 && this._buffer.length) {
22
23     var read = this._reads[0];
24
25     if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) {
26
27       // ok there is any data so that we can satisfy this request
28       this._reads.shift(); // == read
29
30       var buf = this._buffer;
31
32       this._buffer = buf.slice(read.length);
33
34       read.func.call(this, buf.slice(0, read.length));
35
36     }
37     else {
38       break;
39     }
40
41   }
42
43   if (this._reads.length > 0) {
44     return new Error('There are some read requests waitng on finished stream');
45   }
46
47   if (this._buffer.length > 0) {
48     return new Error('unrecognised content at end of stream');
49   }
50
51 };