encoder.js
function encodeStringBuffer( encoder, str, utf8 ) {

	// If we do not have an explicit utf8 or not decision, pick one now
	if (utf8 === undefined) {
		utf8 = false; // Assume false
		for (var i=0, strLen=str.length; i<strLen; ++i) {
			if (str.charCodeAt(i) > 255) {
				utf8 = true; break;
			}
		}
	}

	// Allocate buffer
	var buf, bufView, bufType;
	if (utf8) {
		buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
		bufView = new Uint16Array(buf);
		bufType = PRIM_BUFFER_TYPE.STRING_UTF8;
		encoder.log(LOG.STR,"string='"+str+"', encoding=utf-8, len="+str.length);
	} else {
		buf = new ArrayBuffer(str.length); // 1 byte for each char
		bufView = new Uint8Array(buf);
		bufType = PRIM_BUFFER_TYPE.STRING_LATIN;
		encoder.log(LOG.STR,"string='"+str+"', encoding=latin, len="+str.length);
	}

	// Copy into buffer
	for (var i=0, strLen=str.length; i<strLen; ++i) {
		bufView[i] = str.charCodeAt(i);
	}

	// Write down
	encodeBuffer( encoder, bufType, null, bufView );

}
function encodeStringBuffer( encoder, str, utf8 ) {

	// If we do not have an explicit utf8 or not decision, pick one now
	if (utf8 === undefined) {
		utf8 = false; // Assume false
		for (var i=0, strLen=str.length; i<strLen; ++i) {
			if (str.charCodeAt(i) > 255) {
				utf8 = true; break;
			}
		}
	}

	// Allocate buffer
	var buf, bufView, bufType;
	if (utf8) {
		buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
		bufView = new Uint16Array(buf);
		bufType = PRIM_BUFFER_TYPE.STRING_UTF8;
		encoder.log(LOG.STR,"string='"+str+"', encoding=utf-8, len="+str.length);
	} else {
		buf = new ArrayBuffer(str.length); // 1 byte for each char
		bufView = new Uint8Array(buf);
		bufType = PRIM_BUFFER_TYPE.STRING_LATIN;
		encoder.log(LOG.STR,"string='"+str+"', encoding=latin, len="+str.length);
	}

	// Copy into buffer
	for (var i=0, strLen=str.length; i<strLen; ++i) {
		bufView[i] = str.charCodeAt(i);
	}

	// Write down
	encodeBuffer( encoder, bufType, null, bufView );

}
function encodeStringBuffer ( encoder , str , utf8 ) { // If we do not have an explicit utf8 or not decision, pick one now if ( utf8 === undefined ) { utf8 = false ; // Assume false for ( var i = 0 , strLen = str . length ; i < strLen ; ++ i ) { if ( str . charCodeAt ( i ) > 255 ) { utf8 = true ; break ; } } } // Allocate buffer var buf , bufView , bufType ; if ( utf8 ) { buf = new ArrayBuffer ( str . length * 2 ) ; // 2 bytes for each char bufView = new Uint16Array ( buf ) ; bufType = PRIM_BUFFER_TYPE . STRING_UTF8 ; encoder . log ( LOG . STR , "string='" + str + "', encoding=utf-8, len=" + str . length ) ; } else { buf = new ArrayBuffer ( str . length ) ; // 1 byte for each char bufView = new Uint8Array ( buf ) ; bufType = PRIM_BUFFER_TYPE . STRING_LATIN ; encoder . log ( LOG . STR , "string='" + str + "', encoding=latin, len=" + str . length ) ; } // Copy into buffer for ( var i = 0 , strLen = str . length ; i < strLen ; ++ i ) { bufView [ i ] = str . charCodeAt ( i ) ; } // Write down encodeBuffer ( encoder , bufType , null , bufView ) ; }
Encode a string as buffer
Encode a string as buffer