001    package railo.runtime.coder;
002    
003    
004    /**
005     * 
006     */
007    public final class Coder {
008    
009            /**
010             * Field <code>ENCODING_UU</code>
011             */
012            public static final short ENCODING_UU=0;
013            /**
014             * Field <code>ENCODING_HEX</code>
015             */
016            public static final short ENCODING_HEX=1;
017            /**
018             * Field <code>ENCODING_BASE64</code>
019             */
020            public static final short ENCODING_BASE64=2;
021            
022    
023            /**
024             * @param type
025             * @param value
026             * @return
027             * @throws CoderException
028             */
029            public static byte[] decode(String type, String value) throws CoderException {
030                    type=type.toLowerCase().trim();
031                    if(type.equals("hex"))          return decode(ENCODING_HEX,value);
032                    if(type.equals("uu"))           return decode(ENCODING_UU,value);
033                    if(type.equals("base64"))       return decode(ENCODING_BASE64,value);
034                    throw new CoderException("invalid encoding definition ["+type+"] valid encodingd are [hex, uu, base64]");
035            }
036            
037            /**
038             * @param type
039             * @param value
040             * @return
041             * @throws CoderException
042             */
043            public static byte[] decode(short type, String value) throws CoderException {
044                    if(type==ENCODING_UU)                   return UUCoder.decode(value);
045                    else if(type==ENCODING_HEX)             return HexCoder.decode(value);
046                    else if(type==ENCODING_BASE64)  return Base64Coder.decode(value);
047                    throw new CoderException("invalid encoding definition");
048            }
049    
050            /**
051             * @param type
052             * @param value
053             * @return
054             * @throws CoderException
055             */
056            public static String encode(String type, byte[] value) throws CoderException {
057                    type=type.toLowerCase().trim();
058                    if(type.equals("hex"))          return encode(ENCODING_HEX,value);
059                    if(type.equals("uu"))           return encode(ENCODING_UU,value);
060                    if(type.equals("base64"))       return encode(ENCODING_BASE64,value);
061                    throw new CoderException("invalid encoding definition ["+type+"] valid encodingd are [hex, uu, base64]");
062            }
063            
064            /**
065             * @param type
066             * @param value
067             * @return
068             * @throws CoderException
069             */
070            public static String encode(short type, byte[] value) throws CoderException {
071                    if(type==ENCODING_UU)                   return UUCoder.encode(value);
072                    else if(type==ENCODING_HEX)             return HexCoder.encode(value);
073                    else if(type==ENCODING_BASE64)  return Base64Coder.encode(value);
074                    throw new CoderException("invalid encoding definition");
075            }
076    }