1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 18 package com.gridsystems; 19 20 import java.io.UnsupportedEncodingException; 21 22 /** 23 * Common methods used within both the encoder and the decoder classes. 24 * 25 * @author rruiz 26 */ 27 public final class Base64Utils { 28 29 /** 30 * Private constructor. 31 */ 32 private Base64Utils() { } 33 /** 34 * Returns a byte array representing the contents of the given string, 35 * applying a UTF-8 encoding. 36 * 37 * @param s the string for which we want to obtain its internal byte array 38 * @return the string contents 39 */ 40 public static byte[] toByteArray(String s) { 41 try { 42 return s.getBytes("UTF-8"); 43 } catch (UnsupportedEncodingException e) { 44 e.printStackTrace(); 45 // Something really bad must be happening if UTF-8 is not supported, 46 // but paranoia is sometimes good 47 return s.getBytes(); 48 } 49 } 50 51 /** 52 * Returns a String representing the contents of the given byte array, 53 * applying a UTF-8 decoding. 54 * 55 * @param bytes the string in byte array format 56 * @return the string contents 57 */ 58 public static String toString(byte[] bytes) { 59 try { 60 return new String(bytes, "UTF-8"); 61 } catch (UnsupportedEncodingException e) { 62 e.printStackTrace(); 63 // Something really bad must be happening if UTF-8 is not supported, 64 // but paranoia is sometimes good 65 return new String(bytes); 66 } 67 } 68 }