summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--binaryajax.js304
1 files changed, 132 insertions, 172 deletions
diff --git a/binaryajax.js b/binaryajax.js
index f7b5b48..5323034 100644
--- a/binaryajax.js
+++ b/binaryajax.js
@@ -1,112 +1,96 @@
/*
- * Binary Ajax 0.1.10
+ * Binary Ajax 0.2
* Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/
* Licensed under the MPL License [http://www.nihilogic.dk/licenses/mpl-license.txt]
*/
-var BinaryFile = function(strData, iDataOffset, iDataLength) {
- var data = strData;
- var dataOffset = iDataOffset || 0;
+var BinaryFile = function(data, dataOffset, dataLength) {
+ var dataOffset = dataOffset || 0;
var dataLength = 0;
this.getRawData = function() {
return data;
}
- if (typeof strData == "string") {
- dataLength = iDataLength || data.length;
+ if (typeof data == "string") {
+ dataLength = dataLength || data.length;
- this.getByteAt = function(iOffset) {
- return data.charCodeAt(iOffset + dataOffset) & 0xFF;
+ this.getByteAt = function(offset) {
+ return data.charCodeAt(offset + dataOffset) & 0xFF;
}
+ } else if (typeof data == "unknown") {
+ dataLength = dataLength || IEBinary_getLength(data);
- this.getBytesAt = function(iOffset, iLength) {
- var aBytes = [];
-
- for (var i = 0; i < iLength; i++) {
- aBytes[i] = data.charCodeAt((iOffset + i) + dataOffset) & 0xFF
- };
-
- return aBytes;
- }
- } else if (typeof strData == "unknown") {
- dataLength = iDataLength || IEBinary_getLength(data);
-
- this.getByteAt = function(iOffset) {
- return IEBinary_getByteAt(data, iOffset + dataOffset);
+ this.getByteAt = function(offset) {
+ return IEBinary_getByteAt(data, offset + dataOffset);
}
+ } else {
- this.getBytesAt = function(iOffset, iLength) {
- return new VBArray(IEBinary_getBytesAt(data, iOffset + dataOffset, iLength)).toArray();
- }
}
this.getLength = function() {
return dataLength;
}
- this.getSByteAt = function(iOffset) {
- var iByte = this.getByteAt(iOffset);
- if (iByte > 127)
- return iByte - 256;
+ this.getSByteAt = function(offset) {
+ var byte = this.getByteAt(offset);
+ if (byte > 127)
+ return byte - 256;
else
- return iByte;
+ return byte;
}
- 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.getShortAt = function(offset, bigEndian) {
+ var short = bigEndian ?
+ (this.getByteAt(offset) << 8) + this.getByteAt(offset + 1)
+ : (this.getByteAt(offset + 1) << 8) + this.getByteAt(offset)
+ if (short < 0) short += 65536;
+ return short;
}
- this.getSShortAt = function(iOffset, bBigEndian) {
- var iUShort = this.getShortAt(iOffset, bBigEndian);
- if (iUShort > 32767)
- return iUShort - 65536;
+ this.getSShortAt = function(offset, bigEndian) {
+ var ushort = this.getShortAt(offset, bigEndian);
+ if (ushort > 32767)
+ return ushort - 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;
+ return ushort;
+ }
+ this.getLongAt = function(offset, bigEndian) {
+ var byte1 = this.getByteAt(offset),
+ byte2 = this.getByteAt(offset + 1),
+ byte3 = this.getByteAt(offset + 2),
+ byte4 = this.getByteAt(offset + 3);
+
+ var long = bigEndian ?
+ (((((byte1 << 8) + byte2) << 8) + byte3) << 8) + byte4
+ : (((((byte4 << 8) + byte3) << 8) + byte2) << 8) + byte1;
+ if (long < 0) long += 4294967296;
+ return long;
+ }
+ this.getSLongAt = function(offset, bigEndian) {
+ var ulong = this.getLongAt(offset, bigEndian);
+ if (ulong > 2147483647)
+ return ulong - 4294967296;
else
- return iULong;
+ return ulong;
}
-
- this.getStringAt = function(iOffset, iLength) {
- var aStr = [];
-
- var aBytes = this.getBytesAt(iOffset, iLength);
- for (var j=0; j < iLength; j++) {
- aStr[j] = String.fromCharCode(aBytes[j]);
+ this.getStringAt = function(offset, length) {
+ var chars = [];
+ for (var i=offset,j=0;i<offset+length;i++,j++) {
+ chars[j] = String.fromCharCode(this.getByteAt(i));
}
- return aStr.join("");
+ return chars.join("");
}
- this.getCharAt = function(iOffset) {
- return String.fromCharCode(this.getByteAt(iOffset));
+ this.getCharAt = function(offset) {
+ return String.fromCharCode(this.getByteAt(offset));
}
this.toBase64 = function() {
return window.btoa(data);
}
- this.fromBase64 = function(strBase64) {
- data = window.atob(strBase64);
+ this.fromBase64 = function(str) {
+ data = window.atob(str);
}
}
@@ -114,162 +98,138 @@ var BinaryFile = function(strData, iDataOffset, iDataLength) {
var BinaryAjax = (function() {
function createRequest() {
- var oHTTP = null;
- if (window.ActiveXObject) {
- oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
- } else if (window.XMLHttpRequest) {
- oHTTP = new XMLHttpRequest();
+ var http = null;
+ if (window.XMLHttpRequest) {
+ http = new XMLHttpRequest();
+ } else if (window.ActiveXObject) {
+ http = new ActiveXObject("Microsoft.XMLHTTP");
}
- return oHTTP;
+ return http;
}
- 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);
+ function getHead(url, callback, error) {
+ var http = createRequest();
+ if (http) {
+ if (callback) {
+ if (typeof(http.onload) != "undefined") {
+ http.onload = function() {
+ if (http.status == "200") {
+ callback(this);
} else {
- if (fncError) fncError();
+ if (error) error();
}
- oHTTP = null;
+ http = null;
};
} else {
- oHTTP.onreadystatechange = function() {
- if (oHTTP.readyState == 4) {
- if (oHTTP.status == "200") {
- fncCallback(this);
+ http.onreadystatechange = function() {
+ if (http.readyState == 4) {
+ if (http.status == "200") {
+ callback(this);
} else {
- if (fncError) fncError();
+ if (error) error();
}
- oHTTP = null;
+ http = null;
}
};
}
}
- oHTTP.open("HEAD", strURL, true);
- oHTTP.send(null);
+ http.open("HEAD", url, true);
+ http.send(null);
} else {
- if (fncError) fncError();
+ if (error) error();
}
}
- function sendRequest(strURL, fncCallback, fncError, aRange, bAcceptRanges, iFileSize) {
- var oHTTP = createRequest();
- if (oHTTP) {
+ function sendRequest(url, callback, error, range, acceptRanges, fileSize) {
+ var http = createRequest();
+ if (http) {
- var iDataOffset = 0;
- if (aRange && !bAcceptRanges) {
- iDataOffset = aRange[0];
+ var dataOffset = 0;
+ if (range && !acceptRanges) {
+ dataOffset = range[0];
}
- var iDataLen = 0;
- if (aRange) {
- iDataLen = aRange[1]-aRange[0]+1;
+ var dataLen = 0;
+ if (range) {
+ dataLen = range[1]-range[0]+1;
}
- if (fncCallback) {
- if (typeof(oHTTP.onload) != "undefined") {
- oHTTP.onload = function() {
- if (oHTTP.status == "200" || oHTTP.status == "206" || oHTTP.status == "0") {
- oHTTP.binaryResponse = new BinaryFile(oHTTP.responseText, iDataOffset, iDataLen);
- oHTTP.fileSize = iFileSize || oHTTP.getResponseHeader("Content-Length");
- fncCallback(oHTTP);
+ if (callback) {
+ if (typeof(http.onload) != "undefined") {
+ http.onload = function() {
+ if (http.status == "200" || http.status == "206" || http.status == "0") {
+ http.binaryResponse = new BinaryFile(http.responseText, dataOffset, dataLen);
+ http.fileSize = fileSize || http.getResponseHeader("Content-Length");
+ callback(http);
} else {
- if (fncError) fncError();
+ if (error) error();
}
- oHTTP = null;
+ http = null;
};
} else {
- oHTTP.onreadystatechange = function() {
- if (oHTTP.readyState == 4) {
- if (oHTTP.status == "200" || oHTTP.status == "206" || oHTTP.status == "0") {
+ http.onreadystatechange = function() {
+ if (http.readyState == 4) {
+ if (http.status == "200" || http.status == "206" || http.status == "0") {
// IE6 craps if we try to extend the XHR object
- var oRes = {
- status : oHTTP.status,
+ var res = {
+ status : http.status,
// IE needs responseBody, Chrome/Safari needs responseText
- binaryResponse : new BinaryFile(
- typeof oHTTP.responseBody == "unknown" ? oHTTP.responseBody : oHTTP.responseText, iDataOffset, iDataLen
- ),
- fileSize : iFileSize || oHTTP.getResponseHeader("Content-Length")
+ binaryResponse : new BinaryFile(http.responseBody || http.responseText, dataOffset, dataLen),
+ fileSize : fileSize || http.getResponseHeader("Content-Length")
};
- fncCallback(oRes);
+ callback(res);
} else {
- if (fncError) fncError();
+ if (error) error();
}
- oHTTP = null;
+ http = null;
}
};
}
}
- oHTTP.open("GET", strURL, true);
+ http.open("GET", url, true);
- if (oHTTP.overrideMimeType) oHTTP.overrideMimeType('text/plain; charset=x-user-defined');
+ if (http.overrideMimeType) http.overrideMimeType('text/plain; charset=x-user-defined');
- if (aRange && bAcceptRanges) {
- oHTTP.setRequestHeader("Range", "bytes=" + aRange[0] + "-" + aRange[1]);
+ if (range && acceptRanges) {
+ http.setRequestHeader("Range", "bytes=" + range[0] + "-" + range[1]);
}
- oHTTP.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
+ http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
- oHTTP.send(null);
+ http.send(null);
} else {
- if (fncError) fncError();
+ if (error) error();
}
}
- return function(strURL, fncCallback, fncError, aRange) {
-
- if (aRange) {
+ return function(url, callback, error, range) {
+ if (range) {
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);
+ url,
+ function(http) {
+ var length = parseInt(http.getResponseHeader("Content-Length"),10);
+ var acceptRanges = http.getResponseHeader("Accept-Ranges");
+
+ var start, end;
+ start = range[0];
+ if (range[0] < 0)
+ start += length;
+ end = start + range[1] - 1;
+
+ sendRequest(url, callback, error, [start, end], (acceptRanges == "bytes"), length);
}
);
-
} else {
- sendRequest(strURL, fncCallback, fncError);
+ sendRequest(url, callback, error);
}
}
}());
-/*
-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"
-);
-*/
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_getBytesAt(strBinary, iOffset, iLength)\r\n"
- + " Dim aBytes()\r\n"
- + " ReDim aBytes(iLength - 1)\r\n"
- + " For i = 0 To iLength - 1\r\n"
- + " aBytes(i) = IEBinary_getByteAt(strBinary, iOffset + i)\r\n"
- + " Next\r\n"
- + " IEBinary_getBytesAt = aBytes\r\n"
+ + "Function IEBinary_getByteAt(strBinary, offset)\r\n"
+ + " IEBinary_getByteAt = AscB(MidB(strBinary,offset+1,1))\r\n"
+ "End Function\r\n"
+ "Function IEBinary_getLength(strBinary)\r\n"
+ " IEBinary_getLength = LenB(strBinary)\r\n"