KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > Base64


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util;
19
20 import javax.mail.internet.MimeUtility JavaDoc;
21 import java.io.BufferedReader JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26
27 /**
28  * Simple Base64 string decoding function
29  *
30  * @version This is $Revision: 1.5.4.3 $
31  */

32
33 public class Base64 {
34
35     public static BufferedReader JavaDoc decode(String JavaDoc b64string) throws Exception JavaDoc {
36         return new BufferedReader JavaDoc(
37                    new InputStreamReader JavaDoc(
38                        MimeUtility.decode(
39                             new ByteArrayInputStream JavaDoc(
40                                 b64string.getBytes()), "base64")));
41     }
42
43     public static String JavaDoc decodeAsString(String JavaDoc b64string) throws Exception JavaDoc {
44         if (b64string == null) {
45             return b64string;
46         }
47         String JavaDoc returnString = decode(b64string).readLine();
48         if (returnString == null) {
49             return returnString;
50         }
51         return returnString.trim();
52     }
53
54     public static ByteArrayOutputStream JavaDoc encode(String JavaDoc plaintext)
55             throws Exception JavaDoc {
56         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
57         byte[] in = plaintext.getBytes();
58         ByteArrayOutputStream JavaDoc inStream = new ByteArrayOutputStream JavaDoc();
59         inStream.write(in, 0, in.length);
60         // pad
61
if ((in.length % 3 ) == 1){
62             inStream.write(0);
63             inStream.write(0);
64         } else if((in.length % 3 ) == 2){
65             inStream.write(0);
66         }
67         inStream.writeTo( MimeUtility.encode(out, "base64") );
68         return out;
69     }
70
71     public static String JavaDoc encodeAsString(String JavaDoc plaintext) throws Exception JavaDoc {
72         return encode(plaintext).toString();
73     }
74
75
76 }
77
Popular Tags