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 021import java.io.UnsupportedEncodingException; 022 023import lucee.commons.digest.Base64Encoder; 024import lucee.runtime.exp.ExpressionException; 025import lucee.runtime.op.Caster; 026 027/** 028 * Util class to handle Base 64 Encoded Strings 029 */ 030public final class Base64Coder { 031 032 /** 033 * decodes a Base64 String to a Plain String 034 * @param encoded 035 * @return 036 * @throws ExpressionException 037 */ 038 public static String decodeToString(String encoded,String charset) throws CoderException, UnsupportedEncodingException { 039 byte[] dec = decode(Caster.toString(encoded,null)); 040 return new String(dec,charset); 041 } 042 043 /** 044 * encodes a String to Base64 String 045 * @param plain String to encode 046 * @return encoded String 047 * @throws CoderException 048 * @throws UnsupportedEncodingException 049 */ 050 public static String encodeFromString(String plain,String charset) throws CoderException, UnsupportedEncodingException { 051 return encode(plain.getBytes(charset)); 052 } 053 054 /** 055 * encodes a byte array to Base64 String 056 * @param barr byte array to encode 057 * @return encoded String 058 * @throws CoderException 059 */ 060 public static String encode(byte[] barr) { 061 return Base64Encoder.encode(barr); 062 } 063 064 /** 065 * decodes a Base64 String to a Plain String 066 * @param encoded 067 * @return decoded binary data 068 * @throws CoderException 069 */ 070 public static byte[] decode(String encoded) throws CoderException { 071 return Base64Encoder.decode(encoded); 072 } 073}