package edu.usc.glidein.util;

import java.io.IOException;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

/**
 * Convert to/from Base64-encoded data
 *
 * @author Gideon Juve <juve@usc.edu>
 */
public class Base64
{
	
	/**
	 * Base64-encode a string
	 * @param string The string to encode
	 * @return The base64 encoding of the string
	 */
	public static String toBase64(String string)
	{
		if(string==null) return null;
		byte[] buff = string.getBytes();
		return new BASE64Encoder().encode(buff);
	}
	
	/**
	 * Decode a base64-encoded string
	 * @param base64 Base64-encoded string
	 * @return The decoded string
	 */
	public static String fromBase64(String base64)
	{
		if(base64==null) return null;
		
		try {
			byte[] output = new BASE64Decoder().decodeBuffer(base64);
			return new String(output);
		} 
		catch(IOException ioe) {
			return null; /* I don't think this will actually happen */
		}
	}

	public static void main(String[] args)
	{
		System.out.println(new String(Base64.toBase64("Hello, World!")));
		System.out.println(Base64.fromBase64("SGVsbG8sIFdvcmxkIQ=="));
	}
}

package edu.usc.glidein.util;

import java.io.IOException;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

/**
 * Convert to/from Base64-encoded data
 *
 * @author Gideon Juve <juve@usc.edu>
 */
public class Base64
{
	
	/**
	 * Base64-encode a string
	 * @param string The string to encode
	 * @return The base64 encoding of the string
	 */
	public static byte[] toBase64(String string)
	{
		if(string==null) return null;
		byte[] buff = string.getBytes();
		String s = new BASE64Encoder().encode(buff);
		return s.getBytes();
	}
	
	/**
	 * Decode a base64-encoded string
	 * @param base64 Base64-encoded binary
	 * @return The decoded string
	 */
	public static String fromBase64(byte[] base64)
	{
		if(base64==null) return null;
		
		try {
			String s = new String(base64);
			byte[] output = new BASE64Decoder().decodeBuffer(s);
			return new String(output);
		} 
		catch(IOException ioe) {
			return null; /* I don't think this will actually happen */
		}
	}
}

Use bytes for base-64 encoded data instead of strings
Use bytes for base-64 encoded data instead of strings

git-svn-id: bdb5f82b9b83a1400e05d69d262344b821646179@1345 e217846f-e12e-0410-a4e5-89ccaea66ff7
