I'm working on project that involves us transferring large XML files between a client application and the server. In order to increase bandwidth efficiency, we're using gzip on the XML data to shrink the file size down. This works great as we're seeing about an 80% shrink in file size.
However, I was running into a problem trying to expand the GZIP file on the server. I wanted to expand the file directly to a string in memory—avoiding writing the file to disk. By using a ByteArrayOutputStream I read in the GZIP file using the java.io.FileInputStream and java.util.zip.GZIPInputStream. I then used the toString() method on the ByteArrayOutputStream to convert the OutputStream into a string ColdFusion could use.
On files under 10MBs, I wasn't having a problem but on some really large files I was getting a strange java.nio.BufferOverflowException error when trying to convert the OutputStream to a string. Turns out there appears to be some kind of threshold between CFMX and Java.
To workaround this issue, what I ended up doing is calling the size() method on the OutputStream. If the size of the array is greater than 10,000,000 then I'd invoke the toString() on the OutputStream and append it to my return string. Next, I invoke the reset() method to flush the OutputStream.
So far, this has worked with every size file I've thrown at it. Here's what the code looks like:
The key change was the addition of the if( outStream.size() gt 10000000 ){} statement.
I probably should have tested things farther to find out what exactly was the number that CF was having problems with, but I didn't have time to troubleshoot the problem any further.
9 Comments
Comments for this entry have been disabled.