Detecting Character Encoding In Coldfusion

Most of the time I only work with UTF-8 or UTF-16 character encodings. With that said, recently I was working on an old coldfusion project. Do to database restrictions, required all inputs from the user into the application to be Latin-1 (ISO-8859-1) character encoded.

Coldfusion does not have a good way of checking the character encoding of a string, so I had to pull out some of my Java skillz.

Dropping into Java from ColdFusion is a lot easier then it sounds. Check it out

   
   //use this to test character encoding. We only want Latin-1
    encoder = createObject("java", "java.nio.charset.Charset").forName("ISO-8859-1").newEncoder();

    if(encoder.canEncode(testVar))
    {
        //its Latin-1
    }
    else
    {
        /NOT Latin-1
    }

The Java layer of Coldfusion is very powerful and under utilized by many developers.

If you want to see some true ColdFusion magic, check out the ColdBox Framework, http://ColdBoxFramework.com