Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created October 9, 2013 13:07
Show Gist options
  • Select an option

  • Save bennadel/6900974 to your computer and use it in GitHub Desktop.

Select an option

Save bennadel/6900974 to your computer and use it in GitHub Desktop.
Creating Typed Java Arrays In ColdFusion Using JavaCast()
<cfscript>
// Byte arrays. Binary data is represented by an array of bytes.
// And, each byte is represented by an integer. For simple values,
// we don't have to manually cast each value - we can just pass-in
// the array and ColdFusion will perform the cast.
// Test 1 - Spell "SARAH" with ColdFusion numbers.
bytes = javaCast(
"byte[]",
[ 83, 65, 82, 65, 72 ]
);
writeOutput( charsetEncode( bytes, "utf-8" ) & "<br />" );
// Test 2 - Spell "SARAH" with Java bytes.
bytes = javaCast(
"byte[]",
[
javaCast( "byte", 83 ),
javaCast( "byte", 65 ),
javaCast( "byte", 82 ),
javaCast( "byte", 65 ),
javaCast( "byte", 72 )
]
);
writeOutput( charsetEncode( bytes, "utf-8" ) & "<br />" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Char arrays. Character arrays work the same way that byte
// arrays work: you don't have to perform the cast manually
// for each character - ColdFusion will do that for you. To
// test this, we'll use a StringBuffer which can accept char[]
// as an append() argument.
buffer = createObject( "java", "java.lang.StringBuffer" ).init();
// Test 1 - Add "TRICIA" with ColdFusion strings.
buffer.append(
javaCast( "char[]", [ "T", "R", "I", "C", "I", "A" ] )
);
// Test 2 - Add "TRICIA" with Java chars.
buffer.append(
javaCast(
"char[]",
[
javaCast( "char", "T" ),
javaCast( "char", "R" ),
javaCast( "char", "I" ),
javaCast( "char", "C" ),
javaCast( "char", "I" ),
javaCast( "char", "A" )
]
)
);
// Since we appended twice, should output "TRICIATRICIA".
writeOutput( buffer.toString() & "<br />" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// String arrays. Although a slightly more complex data type,
// strings work the same way that bytes and chars work. You don't
// have to cast the individual values manually. To test this, we'll
// use the common string utils which can accepts String[] as an
// argument in some methods.
utils = createObject( "java", "org.apache.commons.lang.StringUtils" );
// Test 1 - Use ColdFusion strings.
inputs = javaCast(
"string[]",
[
"Hello world",
"Hello there!",
"Hell yis!"
]
);
writeOutput( utils.getCommonPrefix( inputs ) & "<br />" );
// Test 2 - Use Java strings (which are really the same thing).
inputs = javaCast(
"string[]",
[
javaCast( "string", "Hello world" ),
javaCast( "string", "Hello cutie pie!" ),
javaCast( "string", "Hell yis!" )
]
);
writeOutput( utils.getCommonPrefix( inputs ) & "<br />" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// With simple values like integer, char, string, float, long,
// etc., ColdFusion will create a types array and ensure that
// each item within that array is properly casted. With complex
// Java Class instances, things are a little less flexible. If
// you need a typed array of some set of class instances, you
// have to make sure that each item is *alread* in the proper
// data type.
// In ColdFusion, a common example of this is the need to create
// a collection of URL[] instances for a Java class loader. First,
// we have to create a ColdFusion array that is populated with
// actual Java URL instances:
urls = [
createObject( "java", "java.net.URL" ).init(
javaCast( "string", "file:///foo" )
),
createObject( "java", "java.net.URL" ).init(
javaCast( "string", "file:///bar" )
)
];
// Once we have the ColdFusion array populated with URL instances,
// we can then cast the ColdFusion array to a typed Java array in
// order to instantiate the class loader.
loader = createObject( "java", "java.net.URLClassLoader" ).init(
javaCast( "java.net.URL[]", urls )
);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment