License:
GPLv3

Base64-encoding data is found in places all over the IT world and in home users’ systems as well. Being able to encode data in Java or ECMAScript/JavaScript can be valuable when those are the languages available to you. This sample shows how to convert from one format to another and eventually encode data for use within a directory like eDirectory.

In a recent article I mentioned the possibilities of ECMAScript/Javascript within Java for scripting within Java applications or just for your own system. While working on an issue recently the need to convert a string of characters representing binary to a Base64-encoded representation of that binary value was manifest. As the issue was for use within Novell Identity Manager the following was able to be used to accept the string of binary and then convert it to a valid Base64-encoded value. The same could be done within any application with access to the com.novell.xml.util package (or a suitable replacement to do the Base64-encoding) and a Rhino implementation to run the rest of the conversion code from string to binary. ECMAScript/JavaScript Development Without a Web Browser

<code interpreter="rhino">
function b64encbinstring(binstring) {
	importPackage(java.io);
	importPackage(java.lang);
	importPackage(Packages.com.novell.xml.util);
	var mylong0 = java.lang.Long.parseLong(binstring, 2);
	var bos = new java.io.ByteArrayOutputStream();
	var dos = new java.io.DataOutputStream(bos);
	dos.writeLong(mylong0);
	dos.flush();
	var bytedata = bos.toByteArray();
	var base64c = new Packages.com.novell.xml.util.Base64Codec();
	var base64string=new Packages.java.lang.String(base64c.encode(bytedata, (8-Math.ceil(binstring.length/8)), Math.ceil(binstring.length/8), false));
	return base64string;
}

b64encbinstring('00101010110111100001010101000101101');    //Line to Base64-encode a string of zeros and ones and return the appropriate string.
</quote>

This may not be the most-elegant method of doing what is needed but it was something that otherwise would have required custom Java code to do the same compiled into the application in one way or another. If somebody can condense the code above to make is simpler or avoid unnecessary steps please feel free to do so in the comments section.

0 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 5 (0 votes, average: 0.00 out of 5)
You need to be a registered member to rate this post.
Loading...Loading...

Disclaimer: As with everything else at NetIQ Cool Solutions, this content is definitely not supported by NetIQ, so Customer Support will not be able to help you if it has any adverse effect on your environment.  It just worked for at least one person, and perhaps it will be useful for you too.  Be sure to test in a non-production environment.

Leave a Reply

No Comments
By: ab
Nov 18, 2009
11:30 am
Reads:
1,411
Score:
Unrated