diff options
author | Frank Wuerthwein <fkw@ucsd.edu> | 2009-02-06 23:05:27 +0000 |
---|---|---|
committer | Frank Wuerthwein <fkw@ucsd.edu> | 2009-02-06 23:05:27 +0000 |
commit | dd018257e73d9e5c57938b1e15c7ccc0f2fff4b8 (patch) | |
tree | 473c12dab05f3cbe62fed245f2dfe12f55a9ccab | |
parent | fcd7fa1d7a3d621b2788057b53f3c4878ff7b9c4 (diff) |
BinaryFile over XMLHttpRequest
-rw-r--r-- | binaryajax.js | 235 | ||||
-rw-r--r-- | src/lib/binaryXHR.js | 232 |
2 files changed, 232 insertions, 235 deletions
diff --git a/binaryajax.js b/binaryajax.js deleted file mode 100644 index f674d9c..0000000 --- a/binaryajax.js +++ /dev/null @@ -1,235 +0,0 @@ - -/* - * Binary Ajax 0.1.5 - * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ - * MIT License [http://www.opensource.org/licenses/mit-license.php] - */ - - -var BinaryFile = function(strData, iDataOffset, iDataLength) { - var data = strData; - var dataOffset = iDataOffset || 0; - var dataLength = 0; - - this.getRawData = function() { - return data; - } - - if (typeof strData == "string") { - dataLength = iDataLength || data.length; - - this.getByteAt = function(iOffset) { - return data.charCodeAt(iOffset + dataOffset) & 0xFF; - } - } else if (typeof strData == "unknown") { - dataLength = iDataLength || IEBinary_getLength(data); - - this.getByteAt = function(iOffset) { - return IEBinary_getByteAt(data, iOffset + dataOffset); - } - } - - this.getLength = function() { - return dataLength; - } - - this.getSByteAt = function(iOffset) { - var iByte = this.getByteAt(iOffset); - if (iByte > 127) - return iByte - 256; - else - return iByte; - } - - this.getShortAt = function(iOffset, bBigEndian) { - var iShort = bBigEndian ? - (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1) - : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) - if (iShort < 0) iShort += 65536; - return iShort; - } - this.getSShortAt = function(iOffset, bBigEndian) { - var iUShort = this.getShortAt(iOffset, bBigEndian); - if (iUShort > 32767) - return iUShort - 65536; - else - return iUShort; - } - this.getLongAt = function(iOffset, bBigEndian) { - var iByte1 = this.getByteAt(iOffset), - iByte2 = this.getByteAt(iOffset + 1), - iByte3 = this.getByteAt(iOffset + 2), - iByte4 = this.getByteAt(iOffset + 3); - - var iLong = bBigEndian ? - (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4 - : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; - if (iLong < 0) iLong += 4294967296; - return iLong; - } - this.getSLongAt = function(iOffset, bBigEndian) { - var iULong = this.getLongAt(iOffset, bBigEndian); - if (iULong > 2147483647) - return iULong - 4294967296; - else - return iULong; - } - this.getStringAt = function(iOffset, iLength) { - var aStr = []; - for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { - aStr[j] = String.fromCharCode(this.getByteAt(i)); - } - return aStr.join(""); - } - - this.getCharAt = function(iOffset) { - return String.fromCharCode(this.getByteAt(iOffset)); - } - this.toBase64 = function() { - return window.btoa(data); - } - this.fromBase64 = function(strBase64) { - data = window.atob(strBase64); - } -} - - -var BinaryAjax = (function() { - - function createRequest() { - var oHTTP = null; - if (window.XMLHttpRequest) { - oHTTP = new XMLHttpRequest(); - } else if (window.ActiveXObject) { - oHTTP = new ActiveXObject("Microsoft.XMLHTTP"); - } - return oHTTP; - } - - function getHead(strURL, fncCallback, fncError) { - var oHTTP = createRequest(); - if (oHTTP) { - if (fncCallback) { - if (typeof(oHTTP.onload) != "undefined") { - oHTTP.onload = function() { - if (oHTTP.status == "200") { - fncCallback(this); - } else { - if (fncError) fncError(); - } - oHTTP = null; - }; - } else { - oHTTP.onreadystatechange = function() { - if (oHTTP.readyState == 4) { - if (oHTTP.status == "200") { - fncCallback(this); - } else { - if (fncError) fncError(); - } - oHTTP = null; - } - }; - } - } - oHTTP.open("HEAD", strURL, true); - oHTTP.send(null); - } else { - if (fncError) fncError(); - } - } - - function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) { - var oHTTP = createRequest(); - if (oHTTP) { - - var iDataOffset = 0; - if (aRange && !bAcceptRanges) { - iDataOffset = aRange[0]; - } - var iDataLen = 0; - if (aRange) { - iDataLen = aRange[1]-aRange[0]+1; - } - - if (fncCallback) { - if (typeof(oHTTP.onload) != "undefined") { - oHTTP.onload = function() { - - if (oHTTP.status == "200" || oHTTP.status == "206") { - this.binaryResponse = new BinaryFile(this.responseText, iDataOffset, iDataLen); - this.fileSize = iFileSize || this.getResponseHeader("Content-Length"); - fncCallback(this); - } else { - if (fncError) fncError(); - } - oHTTP = null; - }; - } else { - oHTTP.onreadystatechange = function() { - if (oHTTP.readyState == 4) { - if (oHTTP.status == "200" || oHTTP.status == "206") { - this.binaryResponse = new BinaryFile(oHTTP.responseBody, iDataOffset, iDataLen); - this.fileSize = iFileSize || this.getResponseHeader("Content-Length"); - fncCallback(this); - } else { - if (fncError) fncError(); - } - oHTTP = null; - } - }; - } - } - oHTTP.open("GET", strURL, true); - - if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined'); - - if (aRange && bAcceptRanges) { - oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]); - } - - oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT"); - - oHTTP.send(null); - } else { - if (fncError) fncError(); - } - } - - return function(strURL, fncCallback, fncError, aRange) { - - if (aRange) { - getHead( - strURL, - function(oHTTP) { - var iLength = parseInt(oHTTP.getResponseHeader("Content-Length"),10); - var strAcceptRanges = oHTTP.getResponseHeader("Accept-Ranges"); - - var iStart, iEnd; - iStart = aRange[0]; - if (aRange[0] < 0) - iStart += iLength; - iEnd = iStart + aRange[1] - 1; - - sendRequest(strURL, fncCallback, fncError, [iStart, iEnd], (strAcceptRanges == "bytes"), iLength); - } - ); - - } else { - sendRequest(strURL, fncCallback, fncError); - } - } - -}()); - - -document.write( - "<script type='text/vbscript'>\r\n" - + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" - + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n" - + "End Function\r\n" - + "Function IEBinary_getLength(strBinary)\r\n" - + " IEBinary_getLength = LenB(strBinary)\r\n" - + "End Function\r\n" - + "</script>\r\n" -); diff --git a/src/lib/binaryXHR.js b/src/lib/binaryXHR.js new file mode 100644 index 0000000..14d06fe --- /dev/null +++ b/src/lib/binaryXHR.js @@ -0,0 +1,232 @@ +
+/*
+ * BinaryFile over XMLHttpRequest
+ * Part of the javascriptRRD package
+ * Copyright (c) 2009 Frank Wuerthwein, fkw@ucsd.edu
+ * MIT License [http://www.opensource.org/licenses/mit-license.php]
+ *
+ * Original repository: http://javascriptrrd.sourceforge.net/
+ *
+ * Based on:
+ * Binary Ajax 0.1.5
+ * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
+ * MIT License [http://www.opensource.org/licenses/mit-license.php]
+ */
+
+// ============================================================
+// Exception class
+function InvalidBinaryFile(msg) {
+ this.message=msg;
+ this.name="Invalid BinaryFile";
+}
+
+// pretty print
+InvalidBinaryFile.prototype.toString = function() {
+ return this.name + ': "' + this.message + '"';
+}
+
+// =====================================================================
+// BinaryFile class
+// Allows access to element inside a binary stream
+function BinaryFile(strData, iDataOffset, iDataLength) {
+ var data = strData;
+ var dataOffset = iDataOffset || 0;
+ var dataLength = 0;
+ // added
+ var doubleMantExpHi=Math.pow(2,-28);
+ var doubleMantExpLo=Math.pow(2,-52);
+ var doubleMantExpFast=Math.pow(2,-20);
+
+ this.getRawData = function() {
+ return data;
+ }
+
+ if (typeof strData == "string") {
+ dataLength = iDataLength || data.length;
+
+ this.getByteAt = function(iOffset) {
+ return data.charCodeAt(iOffset + dataOffset) & 0xFF;
+ }
+ } else if (typeof strData == "unknown") {
+ dataLength = iDataLength || IEBinary_getLength(data);
+
+ this.getByteAt = function(iOffset) {
+ return IEBinary_getByteAt(data, iOffset + dataOffset);
+ }
+ } else {
+ throw new InvalidBinaryFile("Unsupported type " + (typeof strData));
+ }
+
+ this.getLength = function() {
+ return dataLength;
+ }
+
+ this.getSByteAt = function(iOffset) {
+ var iByte = this.getByteAt(iOffset);
+ if (iByte > 127)
+ return iByte - 256;
+ else
+ return iByte;
+ }
+
+ this.getShortAt = function(iOffset, bBigEndian) {
+ var iShort = bBigEndian ?
+ (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1)
+ : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset)
+ if (iShort < 0) iShort += 65536;
+ return iShort;
+ }
+ this.getSShortAt = function(iOffset, bBigEndian) {
+ var iUShort = this.getShortAt(iOffset, bBigEndian);
+ if (iUShort > 32767)
+ return iUShort - 65536;
+ else
+ return iUShort;
+ }
+ this.getLongAt = function(iOffset, bBigEndian) {
+ var iByte1 = this.getByteAt(iOffset),
+ iByte2 = this.getByteAt(iOffset + 1),
+ iByte3 = this.getByteAt(iOffset + 2),
+ iByte4 = this.getByteAt(iOffset + 3);
+
+ var iLong = bBigEndian ?
+ (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
+ : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
+ if (iLong < 0) iLong += 4294967296;
+ return iLong;
+ }
+ this.getSLongAt = function(iOffset, bBigEndian) {
+ var iULong = this.getLongAt(iOffset, bBigEndian);
+ if (iULong > 2147483647)
+ return iULong - 4294967296;
+ else
+ return iULong;
+ }
+ this.getStringAt = function(iOffset, iLength) {
+ var aStr = [];
+ for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) {
+ aStr[j] = String.fromCharCode(this.getByteAt(i));
+ }
+ return aStr.join("");
+ }
+
+ // Added
+ this.getCStringAt = function(iOffset, iMaxLength) {
+ var aStr = [];
+ for (var i=iOffset,j=0;(i<iOffset+iMaxLength) && (this.getByteAt(i)>0);i++,j++) {
+ aStr[j] = String.fromCharCode(this.getByteAt(i));
+ }
+ return aStr.join("");
+ }
+
+ // Added
+ this.getDoubleAt = function(iOffset) {
+ var iByte1 = this.getByteAt(iOffset),
+ iByte2 = this.getByteAt(iOffset + 1),
+ iByte3 = this.getByteAt(iOffset + 2),
+ iByte4 = this.getByteAt(iOffset + 3),
+ iByte5 = this.getByteAt(iOffset + 4),
+ iByte6 = this.getByteAt(iOffset + 5),
+ iByte7 = this.getByteAt(iOffset + 6),
+ iByte8 = this.getByteAt(iOffset + 7);
+ var iSign=iByte8 >> 7;
+ var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
+ var iMantHi=((((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5) << 8) + iByte4;
+ var iMantLo=((((iByte3) << 8) + iByte2) << 8) + iByte1;
+
+ if (iExpRaw==0) return 0.0;
+ if (iExpRaw==0x7ff) return false;
+
+ var iExp=(iExpRaw & 0x7FF)-1023;
+
+ var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMantLo*doubleMantExpLo + iMantHi*doubleMantExpHi);
+ return dDouble;
+ }
+ // added
+ // Extracts only 4 bytes out of 8, loosing in precision (20 bit mantissa)
+ this.getFastDoubleAt = function(iOffset) {
+ var iByte5 = this.getByteAt(iOffset + 4),
+ iByte6 = this.getByteAt(iOffset + 5),
+ iByte7 = this.getByteAt(iOffset + 6),
+ iByte8 = this.getByteAt(iOffset + 7);
+ var iSign=iByte8 >> 7;
+ var iExpRaw=((iByte8 & 0x7F)<< 4) + (iByte7 >> 4);
+ var iMant=((((iByte7 & 0x0F) << 8) + iByte6) << 8) + iByte5;
+
+ if (iExpRaw==0) return 0.0;
+ if (iExpRaw==0x7ff) return false;
+
+ var iExp=(iExpRaw & 0x7FF)-1023;
+
+ var dDouble = ((iSign==1)?-1:1)*Math.pow(2,iExp)*(1.0 + iMant*doubleMantExpFast);
+ return dDouble;
+ }
+
+ this.getCharAt = function(iOffset) {
+ return String.fromCharCode(this.getByteAt(iOffset));
+ }
+}
+
+
+document.write(
+ "<script type='text/vbscript'>\r\n"
+ + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
+ + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n"
+ + "End Function\r\n"
+ + "Function IEBinary_getLength(strBinary)\r\n"
+ + " IEBinary_getLength = LenB(strBinary)\r\n"
+ + "End Function\r\n"
+ + "</script>\r\n"
+);
+
+
+
+// ===============================================================
+// Load a binary file from the specified URL
+// Will return an object of type BinaryFile
+function FetchBinaryURL(url) {
+ var request = new XMLHttpRequest();
+ request.open("GET", url,false);
+ try {
+ request.overrideMimeType('text/plain; charset=x-user-defined');
+ } catch (err) {
+ // ignore any error, just to make both FF and IE work
+ }
+ request.send(null);
+
+ var response=request.responseBody;
+ if (response==undefined){ // responseBody is non standard, but the only way to make it work in IE
+ response=request.responseText;
+ }
+ var bf=new BinaryFile(response);
+ return bf;
+}
+
+
+// ===============================================================
+// Asyncronously load a binary file from the specified URL
+//
+// callback must be a function with a single argument:
+// bf = an object of type BinaryFile
+function FetchBinaryURLAsync(url, callback) {
+ var callback_wrapper = function() {
+ if(this.readyState == 4) {
+ var response=this.responseBody;
+ if (response==undefined){ // responseBody is non standard, but the only way to make it work in IE
+ response=this.responseText;
+ }
+ var bf=new BinaryFile(response);
+ callback(bf);
+ }
+ }
+
+ var request = new XMLHttpRequest();
+ request.onreadystatechange = callback_wrapper;
+ request.open("GET", url,true);
+ try {
+ request.overrideMimeType('text/plain; charset=x-user-defined');
+ } catch (err) {
+ // ignore any error, just to make both FF and IE work
+ }
+ request.send(null);
+}
|