View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.util.codec;
23  
24  import javax.mail.MessagingException;
25  import javax.mail.internet.MimeUtility;
26  import java.io.BufferedReader;
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  import java.io.InputStreamReader;
31  
32  
33  /**
34   * Performs simple Base64 encoding and decode suitable for authentication.
35   * Note that this is not a general purpose codec.
36   *
37   * @version This is $Revision: 718010 $
38   */
39  
40  public class Base64 {
41  
42      /**
43       * Decode base64 encoded String
44       * 
45       * @param b64string base64 String
46       * @return reader the BufferedReader which holds the decoded base64 text
47       * @throws MessagingException get thrown when an error was detected while trying to decode the String
48       */
49      public static BufferedReader decode(String b64string) throws MessagingException {
50          return new BufferedReader(new InputStreamReader(MimeUtility.decode(
51                              new ByteArrayInputStream(b64string.getBytes()), "base64")));
52      }
53  
54      /**
55       * Decode base64 encoded String
56       * 
57       * @param b64string base64 Sting
58       * @return returnString the String which holds the docoded base64 text
59       * @throws MessagingException get thrown when an error was detected while trying to decode the String
60       * @throws IOException get thrown when I/O error was detected
61       */
62      public static String decodeAsString(String b64string) throws IOException, MessagingException {
63          if (b64string == null) {
64              return b64string;
65          }
66          String returnString = decode(b64string).readLine();
67          if (returnString == null) {
68              return returnString;
69          }
70          return returnString.trim();
71      }
72  
73      /**
74       * Encode String to base64
75       * 
76       * @param plaintext the plaintext to encode 
77       * @return out the ByteArrayOutputStream holding the encoded given text
78       * @throws IOException get thrown when I/O error was detected
79       * @throws MessagingException get thrown when an error was detected while trying to encode the String
80       */
81      public static ByteArrayOutputStream encode(String plaintext) throws IOException, MessagingException {
82          ByteArrayOutputStream out = new ByteArrayOutputStream();
83          byte[] in = plaintext.getBytes();
84          ByteArrayOutputStream inStream = new ByteArrayOutputStream();
85          inStream.write(in, 0, in.length);
86          // pad
87          if ((in.length % 3 ) == 1){
88              inStream.write(0);
89              inStream.write(0);
90          } else if((in.length % 3 ) == 2){
91              inStream.write(0);
92          }
93          inStream.writeTo( MimeUtility.encode(out, "base64")  );
94          return out;
95      }
96  
97      /**
98       * Encode String to base64
99       * 
100      * @param plaintext the plaintext to decode
101      * @return base64String the encoded String 
102      * @throws IOException get thrown when I/O error was detected
103      * @throws MessagingException get thrown when an error was detected while trying to encode the String
104      */
105     public static String encodeAsString(String plaintext) throws IOException, MessagingException {
106         return  encode(plaintext).toString();
107     }
108 
109 
110 }