package railo.runtime.coder;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

import railo.runtime.exp.ExpressionException;
import railo.runtime.op.Caster;

/**
 * Util class to handle Base 64 Encoded Strings
 */
public final class Base64Coder {

    /**
     * decodes a Base64 String to a Plain String
     * @param encoded
     * @return
     * @throws ExpressionException
     */
    public static String decodeToString(String encoded,String charset) throws CoderException, UnsupportedEncodingException {
    	byte[] dec = decode(Caster.toString(encoded,null),charset);
    	return new String(dec,charset);
    }
    
    /**
     * decodes a Base64 String to a Plain String
     * @param encoded
     * @return decoded binary data 
     * @throws CoderException 
     */
    public static byte[] decode(String encoded, String charset) throws CoderException {
        try {
	    	return Base64.decodeBase64(encoded.getBytes(charset));
        }
        catch(Throwable t) {
        	throw new CoderException("can't decode input");
        }
    } 
    /**
     * encodes a String to Base64 String
     * @param plain String to encode
     * @return encoded String
     * @throws CoderException 
     * @throws UnsupportedEncodingException 
     */
    public static String encodeFromString(String plain,String charset) throws CoderException, UnsupportedEncodingException {
    	return encode(plain.getBytes(charset),charset);
    }
    
    /**
     * encodes a byte array to Base64 String
     * @param barr byte array to encode
     * @return encoded String
     * @throws CoderException 
     */
    public static String encode(byte[] barr, String charset) throws CoderException {
		try {
			return new String(Base64.encodeBase64(barr),charset);
		}
        catch(Throwable t) {
        	throw new CoderException("can't encode input");
        }
    } 
}
@@ -1030,53 +1030,30 @@
    %09
-throw new CoderException(%22can't decode input%22
+return _decode(encoded
 );%0A 
@@ -1794,32 +1794,409 @@
 (Throwable t) %7B%0A
+        %09return _encode(barr);%0A        %7D%0A    %7D %0A  %0A    private static byte%5B%5D _decode(String encoded) throws CoderException %7B%0A        try %7B%0A    %09char%5B%5D chars;%0A%09%09chars = encoded.toCharArray();%0A%09%09%0A%09%09byte%5B%5D bytes=new byte%5Bchars.length%5D;%0A%09%09for(int i=0;i%3Cchars.length;i++) %7B%0A%09%09%09bytes%5Bi%5D=(byte)chars%5Bi%5D;%0A%09%09%7D%0A%09%09return Base64.decodeBase64(bytes);%0A        %7D%0A        catch(Throwable t) %7B%0A
         %09throw n
@@ -2220,18 +2220,18 @@
 (%22can't 
+d
 e
-n
 code inp
@@ -2236,21 +2236,270 @@
 nput
-%22);%0A        %7D
+ %5B%22+encoded+%22%5D%22);%0A        %7D%0A    %7D %0A    %0A    private static String _encode(byte%5B%5D barr) %7B%0A%09%09byte%5B%5D bytes=Base64.encodeBase64(barr);%0A%09%09StringBuffer sb=new StringBuffer();%0A%09%09for(int i=0;i%3Cbytes.length;i++) %7B%0A%09%09%09sb.append((char)bytes%5Bi%5D);%0A%09%09%7D%0A%09%09return sb.toString();
 %0A   

improve binaryEncoder