1
0

binaryajax.js 9.6 KB

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