1 | Mad.BinaryStrings.AjaxStream = function(url) {
|
---|
2 | this.offset = 0;
|
---|
3 | var request = window.XMLHttpRequest ? new XMLHttpRequest() : ActiveXObject("Microsoft.XMLHTTP");
|
---|
4 |
|
---|
5 | // pseudo-binary XHR
|
---|
6 | request.overrideMimeType('text/plain; charset=x-user-defined');
|
---|
7 | request.open('GET', url);
|
---|
8 |
|
---|
9 | this.request = request;
|
---|
10 | this.amountRead = 0;
|
---|
11 | this.inProgress = true;
|
---|
12 | this.callbacks = [];
|
---|
13 |
|
---|
14 | var self = this;
|
---|
15 |
|
---|
16 | var iteration = 0;
|
---|
17 |
|
---|
18 | var ochange = function () {
|
---|
19 | iteration += 1;
|
---|
20 | if ((self.callbacks.length > 0 && iteration % 64 === 0) || iteration % 256 === 0) {
|
---|
21 | self.updateBuffer();
|
---|
22 |
|
---|
23 | var newCallbacks = [];
|
---|
24 |
|
---|
25 | for (var i = 0; i < self.callbacks.length; i++) {
|
---|
26 | var callback = self.callbacks[i];
|
---|
27 |
|
---|
28 | if (callback[0] < self.amountRead) {
|
---|
29 | try {
|
---|
30 | callback[1]();
|
---|
31 | } catch (e) {
|
---|
32 | console.log(e);
|
---|
33 | }
|
---|
34 | } else {
|
---|
35 | newCallbacks.push(callback);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | self.callbacks = newCallbacks;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (request.readyState === 4) {
|
---|
43 | self.amountRead = self.contentLength;
|
---|
44 | for (var i = 0; i < self.callbacks.length; i++) {
|
---|
45 | var callback = self.callbacks[i];
|
---|
46 | callback[1]();
|
---|
47 | }
|
---|
48 |
|
---|
49 | window.clearInterval(self.timer);
|
---|
50 |
|
---|
51 | self.inProgress = false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | request.onreadchange = ochange;
|
---|
56 |
|
---|
57 | this.timer = window.setInterval(ochange, 250);
|
---|
58 |
|
---|
59 | request.send(null);
|
---|
60 | }
|
---|
61 |
|
---|
62 | Mad.BinaryStrings.AjaxStream.prototype = new Mad.BinaryStrings.ByteStream();
|
---|
63 |
|
---|
64 | Mad.BinaryStrings.AjaxStream.prototype.updateBuffer = function() {
|
---|
65 | if (!this.finalAmount) {
|
---|
66 | this.arrayBuffer = this.request.mozResponseArrayBuffer;
|
---|
67 | if(this.arrayBuffer) {
|
---|
68 | this.byteBuffer = new Uint8Array(this.arrayBuffer);
|
---|
69 | this.amountRead = this.arrayBuffer.byteLength;
|
---|
70 | } else {
|
---|
71 | this.buffer = this.request.responseText
|
---|
72 | this.amountRead = this.buffer.length;
|
---|
73 | }
|
---|
74 |
|
---|
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 | Mad.BinaryStrings.AjaxStream.prototype.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 | Mad.BinaryStrings.AjaxStream.prototype.seek = function(n) {
|
---|
107 | this.offset += n;
|
---|
108 | }
|
---|
109 |
|
---|
110 | Mad.BinaryStrings.AjaxStream.prototype.read = function(n) {
|
---|
111 | var result = this.peek(n);
|
---|
112 |
|
---|
113 | this.seek(n);
|
---|
114 |
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | Mad.BinaryStrings.AjaxStream.prototype.peek = function(n) {
|
---|
119 | if (this.available(n)) {
|
---|
120 | var offset = this.offset;
|
---|
121 |
|
---|
122 | var result = this.get(offset, n);
|
---|
123 |
|
---|
124 | return result;
|
---|
125 | } else {
|
---|
126 | throw new Error("buffer underflow with peek!");
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | Mad.BinaryStrings.AjaxStream.prototype.get = function(offset, length) {
|
---|
131 | if (this.absoluteAvailable(offset + length)) {
|
---|
132 | var tmpbuffer = "";
|
---|
133 | if(this.byteBuffer) {
|
---|
134 | for(var i = offset; i < offset + length; i += 1) {
|
---|
135 | tmpbuffer = tmpbuffer + String.fromCharCode(this.byteBuffer[i]);
|
---|
136 | }
|
---|
137 | } else {
|
---|
138 | for(var i = offset; i < offset + length; i += 1) {
|
---|
139 | tmpbuffer = tmpbuffer + String.fromCharCode(this.buffer.charCodeAt(i) & 0xff);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return tmpbuffer;
|
---|
143 | } else {
|
---|
144 | throw new Error("buffer underflow with get!");
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | Mad.BinaryStrings.AjaxStream.prototype.getU8 = function(offset, bigEndian) {
|
---|
149 | if(this.byteBuffer) {
|
---|
150 | return this.byteBuffer[offset];
|
---|
151 | }
|
---|
152 |
|
---|
153 | return this.get(offset, 1).charCodeAt(0);
|
---|
154 | }
|
---|
155 |
|
---|
156 | Mad.BinaryStrings.AjaxStream.prototype.requestAbsolute = function(n, callback) {
|
---|
157 | if (n < this.amountRead) {
|
---|
158 | callback();
|
---|
159 | } else {
|
---|
160 | this.callbacks.push([n, callback]);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | Mad.BinaryStrings.AjaxStream.prototype.request = function(n, callback) {
|
---|
165 | this.requestAbsolute(this.offset + n, callback);
|
---|
166 | }
|
---|