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=="));
	}
}

@@ -365,22 +365,22 @@
  static 
-String
+byte%5B%5D
  toBase6
@@ -468,22 +468,26 @@
 es();%0A%09%09
-return
+String s =
  new BAS
@@ -513,16 +513,39 @@
 (buff);%0A
+%09%09return s.getBytes();%0A
 %09%7D%0A%09%0A%09/*
@@ -610,30 +610,30 @@
 e64-encoded 
-string
+binary
 %0A%09 * @return
@@ -690,22 +690,22 @@
 mBase64(
-String
+byte%5B%5D
  base64)
@@ -751,16 +751,50 @@
 %09%09try %7B%0A
+%09%09%09String s = new String(base64);%0A
 %09%09%09byte%5B
@@ -837,22 +837,17 @@
 eBuffer(
-base64
+s
 );%0A%09%09%09re
@@ -975,187 +975,7 @@
 %7D%0A%09%7D
-%0A%0A%09public static void main(String%5B%5D args)%0A%09%7B%0A%09%09System.out.println(new String(Base64.toBase64(%22Hello, World!%22)));%0A%09%09System.out.println(Base64.fromBase64(%22SGVsbG8sIFdvcmxkIQ==%22));%0A%09%7D
 %0A%7D%0A

Use bytes for base-64 encoded data instead of strings