/src/main/chrome/imageinfo/imageinfo/binaryajax.js

https://bitbucket.org/fabricehuet/thumbstore · JavaScript · 235 lines · 198 code · 32 blank · 5 comment · 67 complexity · eb6359c9006eb5c39c1f4fad0bbad53a MD5 · raw file

  1. /*
  2. * Binary Ajax 0.1.5
  3. * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
  4. * MIT License [http://www.opensource.org/licenses/mit-license.php]
  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. } else if (typeof strData == "unknown") {
  19. dataLength = iDataLength || IEBinary_getLength(data);
  20. this.getByteAt = function(iOffset) {
  21. return IEBinary_getByteAt(data, iOffset + dataOffset);
  22. }
  23. }
  24. this.getLength = function() {
  25. return dataLength;
  26. }
  27. this.getSByteAt = function(iOffset) {
  28. var iByte = this.getByteAt(iOffset);
  29. if (iByte > 127)
  30. return iByte - 256;
  31. else
  32. return iByte;
  33. }
  34. this.getShortAt = function(iOffset, bBigEndian) {
  35. var iShort = bBigEndian ?
  36. (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1)
  37. : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset)
  38. if (iShort < 0) iShort += 65536;
  39. return iShort;
  40. }
  41. this.getSShortAt = function(iOffset, bBigEndian) {
  42. var iUShort = this.getShortAt(iOffset, bBigEndian);
  43. if (iUShort > 32767)
  44. return iUShort - 65536;
  45. else
  46. return iUShort;
  47. }
  48. this.getLongAt = function(iOffset, bBigEndian) {
  49. var iByte1 = this.getByteAt(iOffset),
  50. iByte2 = this.getByteAt(iOffset + 1),
  51. iByte3 = this.getByteAt(iOffset + 2),
  52. iByte4 = this.getByteAt(iOffset + 3);
  53. var iLong = bBigEndian ?
  54. (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
  55. : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
  56. if (iLong < 0) iLong += 4294967296;
  57. return iLong;
  58. }
  59. this.getSLongAt = function(iOffset, bBigEndian) {
  60. var iULong = this.getLongAt(iOffset, bBigEndian);
  61. if (iULong > 2147483647)
  62. return iULong - 4294967296;
  63. else
  64. return iULong;
  65. }
  66. this.getStringAt = function(iOffset, iLength) {
  67. var aStr = [];
  68. for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
  69. aStr[j] = String.fromCharCode(this.getByteAt(i));
  70. }
  71. return aStr.join("");
  72. }
  73. this.getCharAt = function(iOffset) {
  74. return String.fromCharCode(this.getByteAt(iOffset));
  75. }
  76. this.toBase64 = function() {
  77. return window.btoa(data);
  78. }
  79. this.fromBase64 = function(strBase64) {
  80. data = window.atob(strBase64);
  81. }
  82. }
  83. var BinaryAjax = (function() {
  84. function createRequest() {
  85. var oHTTP = null;
  86. if (window.XMLHttpRequest) {
  87. oHTTP = new XMLHttpRequest();
  88. } else if (window.ActiveXObject) {
  89. oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  90. }
  91. return oHTTP;
  92. }
  93. function getHead(strURL, fncCallback, fncError) {
  94. var oHTTP = createRequest();
  95. if (oHTTP) {
  96. if (fncCallback) {
  97. if (typeof(oHTTP.onload) != "undefined") {
  98. oHTTP.onload = function() {
  99. if (oHTTP.status == "200") {
  100. fncCallback(this);
  101. } else {
  102. if (fncError) fncError();
  103. }
  104. oHTTP = null;
  105. };
  106. } else {
  107. oHTTP.onreadystatechange = function() {
  108. if (oHTTP.readyState == 4) {
  109. if (oHTTP.status == "200") {
  110. fncCallback(this);
  111. } else {
  112. if (fncError) fncError();
  113. }
  114. oHTTP = null;
  115. }
  116. };
  117. }
  118. }
  119. oHTTP.open("HEAD", strURL, true);
  120. oHTTP.send(null);
  121. } else {
  122. if (fncError) fncError();
  123. }
  124. }
  125. function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) {
  126. var oHTTP = createRequest();
  127. if (oHTTP) {
  128. var iDataOffset = 0;
  129. if (aRange && !bAcceptRanges) {
  130. iDataOffset = aRange[0];
  131. }
  132. var iDataLen = 0;
  133. if (aRange) {
  134. iDataLen = aRange[1]-aRange[0]+1;
  135. }
  136. if (fncCallback) {
  137. if (typeof(oHTTP.onload) != "undefined") {
  138. oHTTP.onload = function() {
  139. if (oHTTP.status == "200" || oHTTP.status == "206") {
  140. this.binaryResponse = new BinaryFile(this.responseText, iDataOffset, iDataLen);
  141. this.fileSize = iFileSize || this.getResponseHeader("Content-Length");
  142. fncCallback(this);
  143. } else {
  144. if (fncError) fncError();
  145. }
  146. oHTTP = null;
  147. };
  148. } else {
  149. oHTTP.onreadystatechange = function() {
  150. if (oHTTP.readyState == 4) {
  151. if (oHTTP.status == "200" || oHTTP.status == "206") {
  152. this.binaryResponse = new BinaryFile(oHTTP.responseBody, iDataOffset, iDataLen);
  153. this.fileSize = iFileSize || this.getResponseHeader("Content-Length");
  154. fncCallback(this);
  155. } else {
  156. if (fncError) fncError();
  157. }
  158. oHTTP = null;
  159. }
  160. };
  161. }
  162. }
  163. oHTTP.open("GET", strURL, true);
  164. if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined');
  165. if (aRange && bAcceptRanges) {
  166. oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]);
  167. }
  168. oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
  169. oHTTP.send(null);
  170. } else {
  171. if (fncError) fncError();
  172. }
  173. }
  174. return function(strURL, fncCallback, fncError, aRange) {
  175. if (aRange) {
  176. getHead(
  177. strURL,
  178. function(oHTTP) {
  179. var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"),10);
  180. var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges");
  181. var iStart, iEnd;
  182. iStart = aRange[0];
  183. if (aRange[0] < 0)
  184. iStart += iLength;
  185. iEnd = iStart + aRange[1] - 1;
  186. sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges == "bytes"), iLength);
  187. }
  188. );
  189. } else {
  190. sendRequest(strURL, fncCallback, fncError);
  191. }
  192. }
  193. }());
  194. document.write(
  195. "<script type='text/vbscript'>\r\n"
  196. + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
  197. + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n"
  198. + "End Function\r\n"
  199. + "Function IEBinary_getLength(strBinary)\r\n"
  200. + " IEBinary_getLength = LenB(strBinary)\r\n"
  201. + "End Function\r\n"
  202. + "</script>\r\n"
  203. );