source: gs3-extensions/web-audio/trunk/js-mad/script/binaryajax.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 9.2 KB
Line 
1
2/*
3 * Binary Ajax 0.1.10
4 * Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
5 * Licensed under the MPL License [http://www.nihilogic.dk/licenses/mpl-license.txt]
6 */
7
8
9var BinaryFile = function(strData, iDataOffset, iDataLength) {
10 var data = strData;
11 var dataOffset = iDataOffset || 0;
12 var dataLength = 0;
13
14 this.getRawData = function() {
15 return data;
16 }
17
18 if (typeof strData === "string") {
19 dataLength = iDataLength || data.length;
20
21 this.getByteAt = function(iOffset) {
22 return data.charCodeAt(iOffset + dataOffset) & 0xFF;
23 }
24
25 this.getBytesAt = function(iOffset, iLength) {
26 var aBytes = [];
27
28 for (var i = 0; i < iLength; i++) {
29 aBytes[i] = data.charCodeAt((iOffset + i) + dataOffset) & 0xFF
30 };
31
32 return aBytes;
33 }
34 } else if (typeof strData === "unknown") {
35 dataLength = iDataLength || IEBinary_getLength(data);
36
37 this.getByteAt = function(iOffset) {
38 return IEBinary_getByteAt(data, iOffset + dataOffset);
39 }
40
41 this.getBytesAt = function(iOffset, iLength) {
42 return new VBArray(IEBinary_getBytesAt(data, iOffset + dataOffset, iLength)).toArray();
43 }
44 }
45
46 this.getLength = function() {
47 return dataLength;
48 }
49
50 this.getSByteAt = function(iOffset) {
51 var iByte = this.getByteAt(iOffset);
52 if (iByte > 127)
53 return iByte - 256;
54 else
55 return iByte;
56 }
57
58 this.getShortAt = function(iOffset, bBigEndian) {
59 var iShort = bBigEndian ?
60 (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1)
61 : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset)
62 if (iShort < 0) iShort += 65536;
63 return iShort;
64 }
65 this.getSShortAt = function(iOffset, bBigEndian) {
66 var iUShort = this.getShortAt(iOffset, bBigEndian);
67 if (iUShort > 32767)
68 return iUShort - 65536;
69 else
70 return iUShort;
71 }
72 this.getLongAt = function(iOffset, bBigEndian) {
73 var iByte1 = this.getByteAt(iOffset),
74 iByte2 = this.getByteAt(iOffset + 1),
75 iByte3 = this.getByteAt(iOffset + 2),
76 iByte4 = this.getByteAt(iOffset + 3);
77
78 var iLong = bBigEndian ?
79 (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
80 : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
81 if (iLong < 0) iLong += 4294967296;
82 return iLong;
83 }
84 this.getSLongAt = function(iOffset, bBigEndian) {
85 var iULong = this.getLongAt(iOffset, bBigEndian);
86 if (iULong > 2147483647)
87 return iULong - 4294967296;
88 else
89 return iULong;
90 }
91
92 this.getStringAt = function(iOffset, iLength) {
93 var aStr = [];
94
95 var aBytes = this.getBytesAt(iOffset, iLength);
96 for (var j=0; j < iLength; j++) {
97 aStr[j] = String.fromCharCode(aBytes[j]);
98 }
99 return aStr.join("");
100 }
101
102 this.getCharAt = function(iOffset) {
103 return String.fromCharCode(this.getByteAt(iOffset));
104 }
105 this.toBase64 = function() {
106 return window.btoa(data);
107 }
108 this.fromBase64 = function(strBase64) {
109 data = window.atob(strBase64);
110 }
111}
112
113
114var BinaryAjax = (function() {
115
116 function createRequest() {
117 var oHTTP = null;
118 if (window.ActiveXObject) {
119 oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
120 } else if (window.XMLHttpRequest) {
121 oHTTP = new XMLHttpRequest();
122 }
123 return oHTTP;
124 }
125
126 function getHead(strURL, fncCallback, fncError) {
127 var oHTTP = createRequest();
128 if (oHTTP) {
129 if (fncCallback) {
130 if (typeof(oHTTP.onload) != "undefined") {
131 oHTTP.onload = function() {
132 if (oHTTP.status === "200") {
133 fncCallback(this);
134 } else {
135 if (fncError) fncError();
136 }
137 oHTTP = null;
138 };
139 } else {
140 oHTTP.onreadchange = function() {
141 if (oHTTP.readyState === 4) {
142 if (oHTTP.status === "200") {
143 fncCallback(this);
144 } else {
145 if (fncError) fncError();
146 }
147 oHTTP = null;
148 }
149 };
150 }
151 }
152 oHTTP.open("HEAD", strURL, true);
153 oHTTP.send(null);
154 } else {
155 if (fncError) fncError();
156 }
157 }
158
159 function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) {
160 var oHTTP = createRequest();
161 if (oHTTP) {
162
163 var iDataOffset = 0;
164 if (aRange && !bAcceptRanges) {
165 iDataOffset = aRange[0];
166 }
167 var iDataLen = 0;
168 if (aRange) {
169 iDataLen = aRange[1]-aRange[0]+1;
170 }
171
172 if (fncCallback) {
173 if (typeof(oHTTP.onload) != "undefined") {
174 oHTTP.onload = function() {
175 if (oHTTP.status === "200" || oHTTP.status === "206" || oHTTP.status === "0") {
176 oHTTP.binaryResponse = new BinaryFile(oHTTP.responseText, iDataOffset, iDataLen);
177 oHTTP.fileSize = iFileSize || oHTTP.getResponseHeader("Content-Length");
178 fncCallback(oHTTP);
179 } else {
180 if (fncError) fncError();
181 }
182 oHTTP = null;
183 };
184 } else {
185 oHTTP.onreadchange = function() {
186 if (oHTTP.readyState === 4) {
187 if (oHTTP.status === "200" || oHTTP.status === "206" || oHTTP.status === "0") {
188 // IE6 craps if we try to extend the XHR object
189 var oRes = {
190status : oHTTP.status,
191 // IE needs responseBody, Chrome/Safari needs responseText
192 binaryResponse : new BinaryFile(
193 typeof oHTTP.responseBody === "unknown" ? oHTTP.responseBody : oHTTP.responseText, iDataOffset, iDataLen
194 ),
195 fileSize : iFileSize || oHTTP.getResponseHeader("Content-Length")
196 };
197 fncCallback(oRes);
198 } else {
199 if (fncError) fncError();
200 }
201 oHTTP = null;
202 }
203 };
204 }
205 }
206 oHTTP.open("GET", strURL, true);
207
208 if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined');
209
210 if (aRange && bAcceptRanges) {
211 oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]);
212 }
213
214 oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
215
216 oHTTP.send(null);
217 } else {
218 if (fncError) fncError();
219 }
220 }
221
222 return function(strURL, fncCallback, fncError, aRange) {
223
224 if (aRange) {
225 getHead(
226 strURL,
227 function(oHTTP) {
228 var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"),10);
229 var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges");
230
231 var iStart, iEnd;
232 iStart = aRange[0];
233 if (aRange[0] < 0)
234 iStart += iLength;
235 iEnd = iStart + aRange[1] - 1;
236
237 sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges === "bytes"), iLength);
238 }
239 );
240
241 } else {
242 sendRequest(strURL, fncCallback, fncError);
243 }
244 }
245
246}());
247
248/*
249 document.write(
250 "<script type='text/vbscript'>\r\n"
251 + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
252 + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n"
253 + "End Function\r\n"
254 + "Function IEBinary_getLength(strBinary)\r\n"
255 + " IEBinary_getLength = LenB(strBinary)\r\n"
256 + "End Function\r\n"
257 + "</script>\r\n"
258 );
259 */
260
261document.write(
262 "<script type='text/vbscript'>\r\n"
263 + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
264 + " IEBinary_getByteAt = AscB(MidB(strBinary, iOffset + 1, 1))\r\n"
265 + "End Function\r\n"
266 + "Function IEBinary_getBytesAt(strBinary, iOffset, iLength)\r\n"
267 + " Dim aBytes()\r\n"
268 + " ReDim aBytes(iLength - 1)\r\n"
269 + " For i = 0 To iLength - 1\r\n"
270 + " aBytes(i) = IEBinary_getByteAt(strBinary, iOffset + i)\r\n"
271 + " Next\r\n"
272 + " IEBinary_getBytesAt = aBytes\r\n"
273 + "End Function\r\n"
274 + "Function IEBinary_getLength(strBinary)\r\n"
275 + " IEBinary_getLength = LenB(strBinary)\r\n"
276 + "End Function\r\n"
277 + "</script>\r\n"
278 );
Note: See TracBrowser for help on using the repository browser.