1 | Mad.ArrayBuffers.AjaxStream = Mad.ArrayBuffers.ByteStream.extend({
|
---|
2 | init: function (url) {
|
---|
3 | this.offset = 0;
|
---|
4 |
|
---|
5 | var request = new XMLHttpRequest();
|
---|
6 | request.open('GET', url);
|
---|
7 | request.responseType = "arraybuffer";
|
---|
8 |
|
---|
9 | this.request = request;
|
---|
10 | this.amountRead = 0;
|
---|
11 | this.inProgress = true;
|
---|
12 | this.finalAmount = false;
|
---|
13 | this.callbacks = [];
|
---|
14 | this.buffer = new Uint8Array(0);
|
---|
15 |
|
---|
16 | var self = this;
|
---|
17 |
|
---|
18 | var iteration = 0;
|
---|
19 |
|
---|
20 | var ochange = function () {
|
---|
21 | console.log("ochange! callbacks.length = " + self.callbacks.length + ", iteration = " + iteration);
|
---|
22 | console.log("readyState = " + request.readyState + ", amountRead = " + self.amountRead + ", contentLength = " + self.contentLength);
|
---|
23 | iteration += 1;
|
---|
24 | if ((self.callbacks.length > 0 && iteration % 32 === 0) || iteration % 256 === 0) {
|
---|
25 | self.updateBuffer();
|
---|
26 |
|
---|
27 | var newCallbacks = [];
|
---|
28 |
|
---|
29 | for (var i = 0; i < self.callbacks.length; i++) {
|
---|
30 | console.log("Handling callback " + i);
|
---|
31 | var callback = self.callbacks[i];
|
---|
32 |
|
---|
33 | if (callback[0] < self.amountRead) {
|
---|
34 | try {
|
---|
35 | callback[1]();
|
---|
36 | } catch (e) {
|
---|
37 | console.log(e);
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | newCallbacks.push(callback);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | self.callbacks = newCallbacks;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (request.readyState === 4) {
|
---|
48 | self.updateBuffer();
|
---|
49 | self.amountRead = self.contentLength;
|
---|
50 | for (var i = 0; i < self.callbacks.length; i++) {
|
---|
51 | var callback = self.callbacks[i];
|
---|
52 | callback[1]();
|
---|
53 | }
|
---|
54 |
|
---|
55 | window.clearInterval(self.timer);
|
---|
56 |
|
---|
57 | self.inProgress = false;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | request.onprogress = ochange;
|
---|
62 |
|
---|
63 | this.timer = window.setInterval(ochange, 250);
|
---|
64 |
|
---|
65 | request.send(null);
|
---|
66 | },
|
---|
67 |
|
---|
68 | updateBuffer: function() {
|
---|
69 | console.log("updateBuffer!");
|
---|
70 | if (!this.finalAmount && this.request.response) {
|
---|
71 | this.arrayBuffer = this.request.response;
|
---|
72 | this.buffer = new Uint8Array(this.arrayBuffer);
|
---|
73 | this.amountRead = this.buffer.length;
|
---|
74 | console.log("typeof buffer = " + typeof(this.arrayBuffer) + ", amountRead = " + this.amountRead);
|
---|
75 | this.contentLength = this.request.getResponseHeader('Content-Length');
|
---|
76 | if(!this.contentLength) {
|
---|
77 | // if the server doesn't send a Content-Length Header, just use amountRead instead
|
---|
78 | // it's less precise at first, but once everything is buffered it becomes accurate.
|
---|
79 | this.contentLength = this.amountRead;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (!this.inProgress) {
|
---|
83 | this.finalAmount = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | return true;
|
---|
87 | } else {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 | },
|
---|
91 |
|
---|
92 | absoluteAvailable: function(n, updated) {
|
---|
93 | if (n > this.amountRead) {
|
---|
94 | if (updated) {
|
---|
95 | throw new Error("buffer underflow with absoluteAvailable!");
|
---|
96 | } else if (this.updateBuffer()) {
|
---|
97 | return this.absoluteAvailable(n, true);
|
---|
98 | } else {
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | } else {
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 | },
|
---|
105 |
|
---|
106 | seek: function(n) {
|
---|
107 | this.offset += n;
|
---|
108 | },
|
---|
109 |
|
---|
110 | read: function(n) {
|
---|
111 | var result = this.peek(n);
|
---|
112 | this.seek(n);
|
---|
113 | return result;
|
---|
114 | },
|
---|
115 |
|
---|
116 | peek: function(n) {
|
---|
117 | if (this.available(n)) {
|
---|
118 | var offset = this.offset;
|
---|
119 |
|
---|
120 | var result = this.get(offset, n);
|
---|
121 |
|
---|
122 | return result;
|
---|
123 | } else {
|
---|
124 | throw new Error("buffer underflow with peek!");
|
---|
125 | }
|
---|
126 | },
|
---|
127 |
|
---|
128 | get: function(offset, length) {
|
---|
129 | if (offset + length < this.amountRead) {
|
---|
130 | var subarr = this.buffer.subarray(offset, offset + length);
|
---|
131 | return subarr;
|
---|
132 | } else {
|
---|
133 | throw 'TODO: THROW GET ERROR!';
|
---|
134 | }
|
---|
135 | },
|
---|
136 |
|
---|
137 | getU8: function(offset, bigEndian) {
|
---|
138 | return this.buffer[offset];
|
---|
139 | },
|
---|
140 |
|
---|
141 | substream: function (offset, length) {
|
---|
142 | return new Mad.ArrayBuffers.SubStream(this, offset, length);
|
---|
143 | },
|
---|
144 |
|
---|
145 | requestAbsolute: function(n, callback) {
|
---|
146 | console.log("requestAbsolute(" + n + ", ...), amountRead = " + this.amountRead);
|
---|
147 | if (n < this.amountRead) {
|
---|
148 | console.log("all fine, calling callback :)");
|
---|
149 | callback();
|
---|
150 | } else {
|
---|
151 | console.log("pushing callback on queue");
|
---|
152 | this.callbacks.push([n, callback]);
|
---|
153 | }
|
---|
154 | },
|
---|
155 |
|
---|
156 | request: function(n, callback) {
|
---|
157 | this.requestAbsolute(this.offset + n, callback);
|
---|
158 | }
|
---|
159 | });
|
---|