BLOB Objects

A binary large object (BLOB) is used to represent binary data. It comes into play every time there need to transcode an abstract data living in the JavaScript runtime into a specific byte sequence and vice versa. It is also used to execute operations on blocks of binary data at bit level. Pass throught a BLOB is indispensable every time we must bring data outside or inside the JavaScript runtime. For example, if we want store a text on a file or we want encrypt or compress an object we must before specify a byte encoding. A BLOB defines how an abstract data can be represented as byte sequence.

Create a BLOB is a very simple operation:

//loads the Data module on the global //scope: var Data = jsl.require("data") var Blob = Data.Blob //creates a BLOB object containing some data //expressed as array of bytes: var blob = new Blob([0, 78, 90, 90, 0, ..]) //creates a BLOB object containing some data //expressed as string of bytes (every character //must have a code point < 256): blob = new Blob("This is a string of bytes") //creates a BLOB object containing some data //expressed as exadecimal string: blob = new Blob(Blob.HEX + "f533abcca35a3ff5")

A BLOB is a very flexible object beacuse allow to work with data expressed into different formats. To distinguish from a string of bytes and an exadecimal string of bytes we must use the exadecimal marker located as static property on the BLOB class.

A BLOB object can be also reported to a primitive form as an array or string of bytes in the same easy way:

//returns the data as array of bytes: var data = blob.toArray() //returns the data as string of bytes: data = blob + "" //returns the data as exadecimal string //(without the marker): data = blob.toString(16)

With a BLOB object we can also add, change, remove, inject single bits or long sequence of bytes with a few lines of code:

//appends some bytes at the end of the BLOB: blob.append([34, 56, 77, ..]) //appends the first lower three bits of a number at the //end of the BLOB: blob.append(7, 3) //moves the current position of ten bits: //reads the next 15 bits into a number: var n = //removes 1 bit from position 25: //all the next bits are shifted to the left //of one position: blob.remove(1) //injects the lower three 3 bit of the number //from the current position //all the next bits are shifted to the right //of three positions: blob.insert(7, 3)

A BLOB object saves the current position after the execution of every operation. Read, write or insert some data moves the current position forward. We can always reposition the current index to a custom bit-offset using the seek and rseek methods:

//absolute seek from the start of the BLOB: var ix = blob.getIndex() // -> 100 //absolute seek from the end of the BLOB //relative seek from the current position: blob.rseek(10)

In general it also possible execute an operation over the BLOB without change the current position:

//writes some data to a custom location: blob.write("ABCA", 100) //the above data are written at position 100 //without change the current position //reads 25 bits from the position 50 (in bits) //if the number of bits to read are less or //equals 32, the result is returned as number: var n =, 50) //reads 179 bits from the position 150 (in bits) //if the number of bits to read is greater //than 32, the result is returned as array of bytes: var data =, 150)

Read the data of a BLOB object can be accomplished also with an iterator or exporting the data into an array or into a string:

//gets an iterator over the object: var i, d, it = blob.getIterator() while(it.hasNext()) { i = //returns the next 3 bits d = //returns the next 10 bits } //exports all the object data to an array: var data = blob.toArray() //exports all the object data to a string: var data = blob + ""