Example of arraycopy() usage

| No Comments

Someone asked for a more complete example of copying byte arrays in ColdFusion MX using Java's System.arraycopy() method. (The subject originally came up on Christian Cantrell's weblog.)

Here's a slightly more detailed version of what I posted in comments over there.

 <cfscript>
  <!--- b1 and b2 are two Java byte arrays that I want to concatenate --->
  byteClass = createObject("java", "java.lang.Byte").TYPE;
  refArray = createObject("java","java.lang.reflect.Array");
  byteLen = b1.getLength() + b2.getLength();

  <!--- bdest is the byte array where I want to put the result --->
  bdest = refArray.newInstance(byteClass, javacast("int", byteLen));
  sys = createObject("java","java.lang.System");
  sys.arraycopy(b1, 0, bdest, 0, b1.getLength());
  sys.arraycopy(b2.getBytes(), 0, bdest, b1.getLength(), b2.getLength());
 </cfscript>

About this Entry

This page contains a single entry by Christian published on April 5, 2005 9:30 AM.

Exiting on Error in a Shell Script was the previous entry in this blog.

Transporting Chunks of Text to the Server is the next entry in this blog.

Find recent content on the main index or look in the archive to find all content.