Macromedia ColdFusion has been 100% Java since the release of MX6. CFML is really just a high-level Java scripting language. ColdFusion provides so many features and functions to the developer that there is rarely a need for special functionality.
But when the need for special functionality arises, Java is a natural fit for CF. What kind of special functionality?
- Building large strings using append (or concatenate)
- Reading large files, incrementally
- Writing out large files, including XML files
- Special SQL functions to save trips to the SQL database
Writing large files, reading large files, and building large strings are not strong points for ColdFusion. But for smaller concatenate or append, and file operations, ColdFusion does a great job.
ColdFusion really makes it easy to use Java. As most CF developers know, ColdFusion is a loosely typed language. Java is not. Anytime you pass data from ColdFusion to Java, you should always javaCast() your variables to the appropriate Java type. A CFC works well as a wrapper to the Java classes.
Below is a list of ColdFusion Java CFCs, and what they're used for. If you find them useful, feel free to download as many as you like.
For a more detailed description of each ColdFusion CFC and what it does, check out my
'CF_This_Helps' blog for ColdFusion and Java CFC.
FileWriter.cfc
The ColdFusion Java CFC FileWriter can be used to write files fast, and with low memory usage. It's especially fast for large files. It works much better than CFFILE for medium to large files. FileWriter.cfc uses the Java classes FileOutputStream, OutputStreamWriter, and BufferedWriter, to implement a very fast file writer that is also light on server memory. When the BufferedWriter class is initialized, you can tell it how much of a cache you would like. Default is 8k. You can set it higher it you know you're doing a lot of output. I found that a 32k or 64k buffer can help speed things up, but I didn't see much improvement going much higher. With Java, you open the file, perform all the writes you need, and then close the file. Java determines when the buffer is full and when an actual I/O needs to occur.
FileReader.cfc
This ColdFusion Java CFC File Reader can be used to read files incrementally. Great for large files. Reading in a large file with ColdFusion CFFILE can be a little scary. When using the CFFILE read tag in CF, the entire file is read into a variable. Then it's up to you to parse through the data in the variable.
The FileReader.cfc program utilizes the Java classes FileInputStream, InputStreamReader, and BufferedReader which enable you to read large files, line by line, with ColdFusion.
StringBuffer.cfc
The ColdFusion Java CFC StringBuffer is used to build large strings lightening fast! If you have string append or string concatenate intensive application, this will improve performance greatly.
ColdFusion string variables are immutable, meaning they cannot change. In CF, when you append one string to another string, you get an entirely new string. As the string grows in size, each concatenation operation takes more RAM, and more CPU. At some point, your server will lock up.
The Java StringBuffer is mutable, meaning it can change. When you append data, it really does just add to the end of an existing string. Much faster. Especially when you're working with larger data sets.
StringBuilder.cfc
The ColdFusion Java StringBuilder CFC requires Java 1.5 or newer. Same as StringBuffer, but even faster. This is only available on CF servers using Java 1.5 or better.
SqlStringBufferArray.cfc
The ColdFusion Java CFC SQL StringBuffer Array is used for building and processing 100's of SQL database statements. Greatly recudes DB roundtrips, as well as provides much faster string and query building.
StringZip.cfc
The ColdFusion Java CFC StringZip is used to compress strings, not files. StringZip.cfc uses the Java String, Deflater, Inflater, and ByteArrayOutputStream classes to create a string zipper/unzipper.
StringZip.cfc returns a Java ByteArray, which you must handle as binary in CF, or image if using SQLServer.
Do not zip any text you may need to search, index, or display very frequently. If it's in the database, it'll be in an image type field.
StringZip would work best for single record retrieval operations. If you are querying and returning dozens of records for display, it would not make sense to zip the "high-traffic" data; as you would be constantly unzipping strings.
Performance appears to be blazingly fast. And compression looks good too. Of course, the amount of compression will vary depending on the amount, type, and contents of the data to be compressed. In simple testing with 5-10K XML strings, I found the resulting compressed size was about 10-15% the size of the original string. Of course mileage varies depending on the type of data you're compressing, the amount of data, etc.
ExportDbToXml.cfc
A ColdFusion Java CFC SQL Server Database Export Tool, to export a database schema and/or data. Generates a UTF-8 encoded XML file containing a SQLServer database schema, or a schema and data. It currently only works for SQLServer because the ColdFusion CFC queries the information_schema views in SQLServer in order to get the database metadata. It could be easily modified to query the appropriate system tables or views for Oracle, PostgreSQL, or just about any other major DB out there.
In order to insure safe data export for XML, all textual data is wrapped with the CDATA tag. For obvious reasons, binary data is not exported to the XML file. However, the schema is still generated for binary fields, it's just the contents of the fields in the XML file are blank.
