Modifying Files With Binary Data in Coldfusion

I've had some crazy, unproductive distractions, but I did have the occasion to solve a problem with Coldfusion that turned out to be a bit of a pain in the butt. Specifically, I had a requirement to modify a text file by adding an additional line based on the occurrence of a marker. This looked to be somewhat easy, and my first version was produced rather quickly. The problem is that while it looked to be the solution, it didn't work.

The catch was that the file on which I needed to work contained binary data, including some taboo characters. I should have known something was amiss when I was examining the file as I was unable to cut-n-paste the file as it contained null characters. So, in manual tests, I was forced to copy the file in whole, and then make modifications.

The first attempt was a straightforward attempt at reading the file line by line, injecting the additional line when needed.

fileMarker = "some text to be sought and appended";
appendText = "whatever text that needs to be added";

targetFileAddress = "path\to\address\file.ext";
updatedFileAddress = "path\to\address\updatedFile.ext";
targetFile = FileOpen(targetFileAddress);
updatedFile = "";

while(NOT FileisEoF(targetFile)) {
line = FileReadLine(targetFile);
updatedFile &= line;
if (Trim(line) eq fileMarker) {
updatedFile &= chr(13) & chr(10); // new line
updatedFile &= appendText;
}
updatedFile &= chr(13) & chr(10);
}
FileClose(targetFile);
FileWrite(updatedFileAddress,updatedFile);
This works well for text files, but the problem is that binary content is corrupted by this.

So I investigated the idea of just modifying an existing file. Coldfusion does not provide any utility to do this, save appending. So I looked for any Java utilities that might do the trick. It took me a while before I found enough discouraging evidence to suggest that working on a single file, modifying and then saving was not going to be possible (perhaps there is a solution to do just this, but it eluded me). So, that left me seemingly no other choice: I have to treat the entire file as binary content.

FileReadBinary is good for this, but what you get is a byteArray that is a bit unwieldy. Luckily, I found the BinaryEncode and BinaryDecode functions. For my purposes, converting the binary content to Hex code gave me the ability to parse the text and make my examination and modifications. To examine the file so that I could figure out what my target code looked like in this format, I did a quick search to find a free Hex editor. The first one I found did the trick: Hex Editor Neo. This free hex editor enabled me to see the Hex and the decoded ASCII side by side so I could find the target Hex string. With that in hand, I modified my process, making it more efficient.

<cfscript>

targetFileAddress = "path\to\address\file.ext";
updatedFileAddress = "path\to\address\updatedFile.ext";

fileMarkerHex = "FFEEDDCC";
appendTextHex = "FFEEDDCC200D0ACCBBAA";

targetFile = FileReadBinary(targetFileAddress);

hexFile = BinaryEncode(targetFile,"Hex");
hexFile = Replace(hexFile,fileMarkerHex,appendTextHex,"All");
hexFile = BinaryDecode(hexFile,"Hex");

FileWrite(updatedFileAddress,hexFile);
</cfscript>

So this is a bit different. Essentially, I am reading the file as binary data, then converting it to a Hex representation. I then use the handy Replace function to make all my changes, and then convert it back to the straight binary representation. Then it is written as a new file.

I set the fileMarkerHex to the Hex value of what it is I am seeking. To make use of the Replace, I include the fleMarkerHex and attach to it the extra info I want to add. Since I want to line delimit the information, I include the Hex string "200D0A" to represent the line break.

And this works, yippee! So the lesson is that if there is any binary content in the file, the entire file needs to be treated as binary.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Rocky Madden's Gravatar Thanks for the idea of converting binary to hex and then back. Had a very similar issue and ColdFusion doesn't lend itself to bytearrays.
# Posted By Rocky Madden | 2/23/10 11:04 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.001. Contact Blog Owner
Watch the latest videos on YouTube.com