ServerJS/Binary/A

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Binary Data

JavaScript does not have a binary data type. However, in server-side scenarios, binary data needs to be often processed. That is why we need some working Binary() class.

Binary() object proposition

This is an interface of my proposed Binary object. I also created a sample implementation, see http://tmp.zarovi.cz/binary.js.

/**
 * Creates a new variable holding binary data.
 * @param {number} initialData Variable amount if initial arguments. 
   Every number represents a byte.
 * @returns {Binary}
 */
function Binary(initialData) {};
/**
 * Returns the number of bytes stored in this structure.
 * This is intentionally named "getLength" instead of "length" to indicate a method (and not a property).
 * @returns {number}
 */
Binary.prototype.getLength = function() {};
/**
 * Converts the binary variable to string by UTF-8 encoding its contents.
 * @returns {string}
 */
Binary.prototype.toString = function() {};
/**
 * Returns a byte value from a given index.
 * @param {number} index
 * @returns {number}
 */
Binary.prototype.getByte = function(index) {};
/**
 * Sets a byte value at given index.
 * @param {number} index
 */
Binary.prototype.setByte = function(index, value) {};
/**
 * These are essentialy copied from the Array class.
 */
Binary.prototype.pop = function() {};
Binary.prototype.push = function() {};
Binary.prototype.shift = function() {};
Binary.prototype.unshift = function() {};
/**
 * Encodes the data in Base64
 * @returns {string}
 */
Binary.prototype.base64encode = function() {};
/**
 * Calculates MD5 hash
 * @returns {string}
 */
Binary.prototype.md5 = function() {};
/**
 * Calculates SHA1 hash
 * @returns {string}
 */
Binary.prototype.sha1 = function() {};
/**
 * Decodes the Base64 encoded string
 * @returns {Binary}
 */
String.prototype.base64decode = function() {};
/**
 * Converts an UTF-8 encoded string into its binary (one byte per item) variant
 * @returns {Binary}
 */
String.prototype.toBinary = function() {};

Ondras 08:24, 5 February 2009 (UTC)