View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package org.apache.james.util; 9 10 import javax.mail.internet.MimeUtility; 11 import java.io.BufferedReader; 12 import java.io.ByteArrayInputStream; 13 import java.io.ByteArrayOutputStream; 14 import java.io.InputStreamReader; 15 16 17 /*** 18 * Simple Base64 string decoding function 19 * @author Jason Borden <jborden@javasense.com> 20 * 21 * @version This is $Revision: 1.1 $ 22 */ 23 24 public class Base64 { 25 26 public static BufferedReader decode(String b64string) throws Exception { 27 return new BufferedReader( 28 new InputStreamReader( 29 MimeUtility.decode( 30 new ByteArrayInputStream( 31 b64string.getBytes()), "base64"))); 32 } 33 34 public static String decodeAsString(String b64string) throws Exception { 35 if (b64string == null) { 36 return b64string; 37 } 38 String returnString = decode(b64string).readLine(); 39 if (returnString == null) { 40 return returnString; 41 } 42 return returnString.trim(); 43 } 44 45 public static ByteArrayOutputStream encode(String plaintext) 46 throws Exception { 47 ByteArrayOutputStream out = new ByteArrayOutputStream(); 48 byte[] in = plaintext.getBytes(); 49 ByteArrayOutputStream inStream = new ByteArrayOutputStream(); 50 inStream.write(in, 0, in.length); 51 // pad 52 if ((in.length % 3 ) == 1){ 53 inStream.write(0); 54 inStream.write(0); 55 } else if((in.length % 3 ) == 2){ 56 inStream.write(0); 57 } 58 inStream.writeTo( MimeUtility.encode(out, "base64") ); 59 return out; 60 } 61 62 public static String encodeAsString(String plaintext) throws Exception { 63 return encode(plaintext).toString(); 64 } 65 66 67 }

This page was automatically generated by Maven