Code

The code is broken out into two separate functions, one of which deals with the XHR request and the second function then loops through the incoming raw data and copies the masked low-bytes into the bytebuffer array. This allows for more flexibility during implementation, since it is quite likely that only the extract_binary function will need your attention to adjust the code to your needs.

As-is the code will only work with binaries on the local server - there are hacks for crossdomain access or it would be possible to use a server proxy script to access other domains.


function load_binary_data(url) { 
	var req = new XMLHttpRequest();  
	req.open('GET', url, false);  
	if (req.overrideMimeType)
		req.overrideMimeType('text/plain; charset=x-user-defined');  
	req.send(null);  
	if (req.status != 200) return '';
	return req.responseText;  
} 

function extract_binary(input) {
	var inx = 0;
	while(inx < input.length) {
		for(jnx = 0; jnx < input.length; jnx++) {
			if(inx < input.length)
				bytebuffer[jnx] = input.charCodeAt(inx++) & 0xff;
			else
				bytebuffer[jnx] = 0;
		}
	}
}

// usage: 
bytebuffer = new Array();	// global array to hold binary data
var url = 'some_local_binary.bin';
var data = load_binary_data(url);
extract_binary(data);		// stores data in bytebuffer array
						

Example

Here is a working example of the code - it has been tested in Firefox 3.6.x and Safari 4.0.5. Two jpegs in the same directory are being loaded and all low-ascii characters in the first 1024 bytes are being displayed.

Load "test.jpg"
Load "test2.jpg"

Loading...

Discussion

Name *:        Email *:        URL:
SUBMIT
no comments yet...