001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.coder;
020
021
022/**
023 * 
024 */
025public final class Coder {
026
027        /**
028         * Field <code>ENCODING_UU</code>
029         */
030        public static final short ENCODING_UU=0;
031        /**
032         * Field <code>ENCODING_HEX</code>
033         */
034        public static final short ENCODING_HEX=1;
035        /**
036         * Field <code>ENCODING_BASE64</code>
037         */
038        public static final short ENCODING_BASE64=2;
039        
040
041        /**
042         * @param type
043         * @param value
044         * @return
045         * @throws CoderException
046         */
047        public static byte[] decode(String type, String value) throws CoderException {
048                type=type.toLowerCase().trim();
049                if(type.equals("hex"))          return decode(ENCODING_HEX,value);
050                if(type.equals("uu"))           return decode(ENCODING_UU,value);
051                if(type.equals("base64"))       return decode(ENCODING_BASE64,value);
052                throw new CoderException("invalid encoding definition ["+type+"] valid encodingd are [hex, uu, base64]");
053        }
054        
055        /**
056         * @param type
057         * @param value
058         * @return
059         * @throws CoderException
060         */
061        public static byte[] decode(short type, String value) throws CoderException {
062                if(type==ENCODING_UU)                   return UUCoder.decode(value);
063                else if(type==ENCODING_HEX)             return HexCoder.decode(value);
064                else if(type==ENCODING_BASE64)  return Base64Coder.decode(value);
065                throw new CoderException("invalid encoding definition");
066        }
067
068        /**
069         * @param type
070         * @param value
071         * @return
072         * @throws CoderException
073         */
074        public static String encode(String type, byte[] value) throws CoderException {
075                type=type.toLowerCase().trim();
076                if(type.equals("hex"))          return encode(ENCODING_HEX,value);
077                if(type.equals("uu"))           return encode(ENCODING_UU,value);
078                if(type.equals("base64"))       return encode(ENCODING_BASE64,value);
079                throw new CoderException("invalid encoding definition ["+type+"] valid encodingd are [hex, uu, base64]");
080        }
081        
082        /**
083         * @param type
084         * @param value
085         * @return
086         * @throws CoderException
087         */
088        public static String encode(short type, byte[] value) throws CoderException {
089                if(type==ENCODING_UU)                   return UUCoder.encode(value);
090                else if(type==ENCODING_HEX)             return HexCoder.encode(value);
091                else if(type==ENCODING_BASE64)  return Base64Coder.encode(value);
092                throw new CoderException("invalid encoding definition");
093        }
094}