Skip to content

Instantly share code, notes, and snippets.

@twelverobots
Created April 14, 2014 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twelverobots/10674905 to your computer and use it in GitHub Desktop.
Save twelverobots/10674905 to your computer and use it in GitHub Desktop.
Breaking up a byte array into smaller chunks in ColdFusion using Java
<cfset text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." />
<!--- Get the byteArray of the text to be handled --->
<cfset byteArray = text.getBytes() />
<!--- An array to store the broken up chunks --->
<cfset bytes = [] />
<!--- THe java array utility --->
<cfset javaArray = createObject("java", "java.util.Arrays") />
<!--- The number of bytes for each chunk --->
<cfset numberOfBytes = 117 />
<!--- loop over the byte array --->
<cfloop from="0" to="#arrayLen(byteArray)#" index="byteIndex" step="#numberOfBytes#">
<!--- Check to see how many bytes are left --->
<cfif byteIndex+numberofBytes GT arrayLen(byteArray)>
<cfset numberOfbytes = arrayLen(byteArray) - byteIndex />
</cfif>
<!--- Get the next N Bytes --->
<cfset arrayAppend(bytes, javaArray.copyOfRange(byteArray, byteIndex, byteIndex+numberOfBytes)) />
<!--- Output the size of the last byteArray Added --->
<cfoutput>
ArrayLen: #arrayLen(bytes[arrayLen(bytes)])#<br />
</cfoutput>
</cfloop>
<!--- Output the array of byteArrays --->
<cfdump var="#bytes#" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment