KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 package org.apache.jetspeed.util;
17
18 import javax.mail.internet.MimeUtility JavaDoc;
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 /**
24  * Simple Base64 string decoding function
25  * @author Jason Borden <jborden@javasense.com>
26  *
27  * This class was copied from the jakarta-james project.
28  * The only change made, besides comments, is the package name.
29  * This class orgininated in org.apache.james.util as version 1.3
30  * which was committed by darrel on 2002/01/18 02:48:39
31  *
32  * $Id: Base64.java,v 1.5 2004/02/23 03:23:42 jford Exp $
33  * Committed on $Date: 2004/02/23 03:23:42 $ by: $Name: $
34  */

35
36 public class Base64 {
37
38     public static String JavaDoc decodeAsString(String JavaDoc b64string) throws Exception JavaDoc
39     {
40         return new String JavaDoc(decodeAsByteArray(b64string));
41     }
42
43     public static byte[] decodeAsByteArray(String JavaDoc b64string) throws Exception JavaDoc
44     {
45         InputStream JavaDoc in = MimeUtility.decode(new ByteArrayInputStream JavaDoc(
46                          b64string.getBytes()), "base64");
47
48         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
49         
50         while(true)
51         {
52             int b = in.read();
53             if (b == -1) break;
54             else out.write(b);
55         }
56     
57         return out.toByteArray();
58     }
59
60     public static String JavaDoc encodeAsString(String JavaDoc plaintext) throws Exception JavaDoc
61     {
62         return encodeAsString(plaintext.getBytes());
63     }
64
65     public static String JavaDoc encodeAsString(byte[] plaindata) throws Exception JavaDoc
66     {
67         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
68         ByteArrayOutputStream JavaDoc inStream = new ByteArrayOutputStream JavaDoc();
69
70         inStream.write(plaindata, 0, plaindata.length);
71
72         // pad
73
if ((plaindata.length % 3 ) == 1)
74         {
75             inStream.write(0);
76             inStream.write(0);
77         }
78         else if((plaindata.length % 3 ) == 2)
79         {
80             inStream.write(0);
81         }
82
83         inStream.writeTo(MimeUtility.encode(out, "base64"));
84         return out.toString();
85     }
86
87 }
88
89
Popular Tags