package railo.commons.io;

import java.nio.charset.Charset;

import railo.print;
import railo.runtime.exp.PageException;
import railo.runtime.op.Caster;

public class CharsetUtil {
	public static final Charset UTF8;
	public static final Charset ISO88591;
	public static final Charset UTF16BE;
	public static final Charset UTF16LE;
	
	static {
		UTF8=toCharset("utf-8",null);
		ISO88591=toCharset("iso-8859-1",null);
		UTF16BE=toCharset("utf-16BE",null);
		UTF16LE=toCharset("UTF-16LE",null);
		
		
	}

	public static Charset toCharset(String charset) {
		return Charset.forName(charset.trim());
	}

	public static Charset toCharset(String charset,Charset defaultValue) {
		try{
			return Charset.forName(charset);
		}
		catch(Throwable t){
			return defaultValue;
		}
	}

}

@@ -74,16 +74,54 @@
 .print;%0A
+import railo.commons.lang.StringUtil;%0A
 import r
@@ -584,16 +584,68 @@
 rset) %7B%0A
+%09%09if(StringUtil.isEmpty(charset,true)) return null;%0A
 %09%09return
@@ -754,16 +754,71 @@
 alue) %7B%0A
+%09%09if(StringUtil.isEmpty(charset)) return defaultValue;%0A
 %09%09try%7B%0A%09

make sure the method toCharset works with empty strings