Splitting And Joining A Binary File In ColdFusion

Posted October 31, 2007 at 11:46 AM by Ben Nadel

Tags: ColdFusion

I was just reading over on CF-Talk that Shane Trahan was trying to split a binary file, write both parts to disk, and then later read them in and re-join them. I have never done anything like this, so I thought I would give it a try.

At first, I was going to use a few ColdFusion arrays, but that didn't seem to work. For one, ColdFusion arrays are not really arrays, they are Java Collections. Additionally, I think there were some data type conversions taking place that I was not away of. I don't know enough about Java to full understand all the data type stuff.

After a little bit of Googling, I found the Java ByteBuffer. This finally solved the problem! The ByteBuffer does all the heavy lifting for splitting and then joining the underlying byte arrays of the binary file. Check out my solution below:

  • <!--- Read in the original binary file. --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './sexy.jpg' )#"
  • variable="binFile"
  • />
  •  
  •  
  • <!--- Get the length of the original binary file. --->
  • <cfset intLength = ArrayLen( binFile ) />
  •  
  • <!---
  • Get the mid point of the byte array. We are goint
  • to use this as the split point for our two future
  • binary files.
  • --->
  • <cfset intMid = Ceiling( intLength / 2 ) />
  •  
  •  
  • <!---
  • Now, we are going to use the Java ByteBuffer to do the
  • heavy lifting for us. We can't use ColdFusion arrays
  • directly for manipulation since they are not really
  • arrays, but rather Collections (and there's probably
  • other complications). The Java ByteBuffer will take
  • care of splitting and then later joining our files.
  •  
  • Create an instance of the static ByteBuffer class so
  • that we can refer to it multiple times.
  • --->
  • <cfset objByteBuffer = CreateObject(
  • "java",
  • "java.nio.ByteBuffer"
  • ) />
  •  
  •  
  • <!---
  • Using the ByteBuffer class, create two byte buffer
  • instances for our two file parts. Because we are
  • splitting the original file, we only need to allocate
  • space equal to half of the original file (since we
  • used Ceiling() in our split).
  • --->
  • <cfset objBufferA = objByteBuffer.Allocate(
  • JavaCast( "int", intMid )
  • ) />
  •  
  • <cfset objBufferB = objByteBuffer.Allocate(
  • JavaCast( "int", intMid )
  • ) />
  •  
  •  
  • <!---
  • Now that we have our two ByteBuffer instance, we are
  • going to store half of the original binary byte array
  • into each.
  • --->
  •  
  • <!--- Store first half. --->
  • <cfset objBufferA.Put(
  • binFile,
  • JavaCast( "int", 0 ),
  • JavaCast( "int", intMid )
  • ) />
  •  
  • <!--- Store second half. --->
  • <cfset objBufferB.Put(
  • binFile,
  • JavaCast( "int", intMid ),
  • JavaCast( "int", (intLength - intMid) )
  • ) />
  •  
  •  
  • <!---
  • Now, all we have to do is write the two byte arrays to
  • disk. In order to get the byte arrays from the ByteBuffer,
  • we just need to call its underlying Array() method.
  • --->
  •  
  • <!--- Write first half. --->
  • <cffile
  • action="write"
  • file="#ExpandPath( './sexy_a.jpg' )#"
  • output="#objBufferA.Array()#"
  • />
  •  
  • <!--- Write second half. --->
  • <cffile
  • action="write"
  • file="#ExpandPath( './sexy_b.jpg' )#"
  • output="#objBufferB.Array()#"
  • />
  •  
  •  
  •  
  • <!---
  • ASSERT: At this point, we have taken our original binary
  • file and split it up into two parts that have been written
  • back to disk. Now, we can go about testing this by reading
  • them in again, joinging the individual byte arrays, and
  • then streaming to the client.
  • --->
  •  
  •  
  • <!--- Read in the first part. --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './sexy_a.jpg' )#"
  • variable="binFileA"
  • />
  •  
  • <!--- Read in the second part. --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './sexy_b.jpg' )#"
  • variable="binFileB"
  • />
  •  
  •  
  •  
  • <!---
  • Again, we are going to create a Java ByteBuffer to do the
  • heavy lifting for us. This time, we need to allocate space
  • for the resultant byte array which will be equal to the
  • length of both binary files.
  • --->
  • <cfset arrBinFull = objByteBuffer.Allocate(
  • JavaCast(
  • "int",
  • (ArrayLen( binFileA ) + ArrayLen( binFileB ) )
  • )
  • ) />
  •  
  •  
  • <!---
  • Add the entire first file's byte array to the byte buffer
  • using a zero offset and full length.
  • --->
  • <cfset arrBinFull.Put(
  • binFileA,
  • JavaCast( "int", 0 ),
  • JavaCast( "int", ArrayLen( binFileA ) )
  • ) />
  •  
  • <!---
  • Add the entire second file's byte array to the byte buffer
  • using a zero offset and full length.
  • --->
  • <cfset arrBinFull.Put(
  • binFileB,
  • JavaCast( "int", 0 ),
  • JavaCast( "int", ArrayLen( binFileA ) )
  • ) />
  •  
  •  
  • <!---
  • At this point, our Java ByteBuffer should now contain
  • the entire byte array the we had in our original file.
  • To prove this, we are going to get the underlying byte
  • array and stream it to the client.
  • --->
  • <cfcontent
  • type="image/jpeg"
  • variable="#arrBinFull.Array()#"
  • />

Now, I am not sure if this is a good way to do it, but the code seems fairly straight forward.




Reader Comments

Oct 31, 2007 at 3:25 PM // reply »
211 Comments

I'm assuming the file splitting is for BLOBs?


Oct 31, 2007 at 3:28 PM // reply »
11,314 Comments

@Todd,

To be honest, I am not sure what the original intent was. However, I assume that it all shows up as a byte array in one form or another, so I guess whether it's a BLOB or binary file read, the same algorithm (or slightly modified) can be used.


Nov 1, 2007 at 2:11 PM // reply »
21 Comments

Another excellent example of ColdFusion and Java rocking like it's 1999. Much obliged!

Splitting and joining binary files is quite useful when you're uploading or downloading files. You can also build a file one byte at a time with classes like BufferedInputStream, but that's a whole other story. :)

You used ArrayLen to get the size of the binary file. Any idea on how exactly this works?


Nov 1, 2007 at 2:14 PM // reply »
21 Comments

I just noticed: your comment for the first CreateObject call says: "Create an instance of the static ByteBuffer class so that we can refer to it multiple times."

I think it's more accurate to say you are loading the ByteBuffer class so that you can call its static methods.

Static methods are the closest that Java comes to global functions. The methods are not called on a particular ByteBuffer object (or instance) - they're just there.


Nov 2, 2007 at 7:23 AM // reply »
11,314 Comments

@Dave,

ArrayLen() works because I think the binary file is loaded as a proper Java array of the bytes. I say "proper" array because it's not a ColdFusion array (Collection), it's a real byte array as in Byte[] ... I think :)

This is all just guess work for me, though. I defer to you for the better Java explanation as your Java experience eclipses mine.


Nov 2, 2007 at 10:19 AM // reply »
21 Comments

@Ben,

I just took a binary variable created with <cffile action="readbinary"> and looked under the hood with my handy getClassInfo() reflection function. The variable is an instance of this Java class:

[B

WTF? Anyone know what a [B is? Some sort of pointer? The class implements the Serializable and Cloneable interfaces, if that is any help.


Nov 2, 2007 at 10:27 AM // reply »
11,314 Comments

@Dave,

I believe the "[" indicates an array and whatever comes after it is the type of array. Like sometimes, I think I get "[String" which is an array of strings.


Feb 19, 2010 at 2:52 PM // reply »
11 Comments

@Ben,

I just wanted to say thanks for your example. Ended up using a similar method to parse a bytearray from getHttpRequestData().content when a file is uploaded. By default it includes all form elements in a bytearray and we needed to separate them out without converting the data to a string first


Feb 22, 2010 at 8:31 PM // reply »
11,314 Comments

@Rocky,

Oh cool. I haven't done too much with raw, binary form posts.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools