Byte Arrays and ColdFusion

I’m looking for ways to dynamically create byte arrays of indeterminate length in ColdFusion. I have tried this:


sb = createObject("java", "java.lang.StringBuffer");
sb.init(someLength);
byteArray = sb.toString().getBytes();

// and this:

baos = createObject("java", "java.io.ByteArrayOutputStream");
baos.init(someLength);
byteArray = boas.toByteArray();

… but both return byte arrays of 0 length rather than someLength. The only technique I have found that works is this:


function getByteArray(someLength)
{
sb = createObject("java", "java.lang.StringBuffer");
for (i = 0; i lt someLength; i = i + 1)
{
sb.append("_");
}
return sb.toString().getBytes();
}

This works, but as you might expect, it’s not the most efficient technique. Any other ideas?