1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You 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 implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.mail.util;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 public class Base64
25 {
26 private static final Encoder encoder = new Base64Encoder();
27
28 /**
29 * encode the input data producing a base 64 encoded byte array.
30 *
31 * @return a byte array containing the base 64 encoded data.
32 */
33 public static byte[] encode(
34 byte[] data)
35 {
36 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
37
38 try
39 {
40 encoder.encode(data, 0, data.length, bOut);
41 }
42 catch (IOException e)
43 {
44 throw new RuntimeException("exception encoding base64 string: " + e);
45 }
46
47 return bOut.toByteArray();
48 }
49
50 /**
51 * Encode the byte data to base 64 writing it to the given output stream.
52 *
53 * @return the number of bytes produced.
54 */
55 public static int encode(
56 byte[] data,
57 OutputStream out)
58 throws IOException
59 {
60 return encoder.encode(data, 0, data.length, out);
61 }
62
63 /**
64 * Encode the byte data to base 64 writing it to the given output stream.
65 *
66 * @return the number of bytes produced.
67 */
68 public static int encode(
69 byte[] data,
70 int off,
71 int length,
72 OutputStream out)
73 throws IOException
74 {
75 return encoder.encode(data, off, length, out);
76 }
77
78 /**
79 * decode the base 64 encoded input data. It is assumed the input data is valid.
80 *
81 * @return a byte array representing the decoded data.
82 */
83 public static byte[] decode(
84 byte[] data)
85 {
86 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
87
88 try
89 {
90 encoder.decode(data, 0, data.length, bOut);
91 }
92 catch (IOException e)
93 {
94 throw new RuntimeException("exception decoding base64 string: " + e);
95 }
96
97 return bOut.toByteArray();
98 }
99
100 /**
101 * decode the base 64 encoded String data - whitespace will be ignored.
102 *
103 * @return a byte array representing the decoded data.
104 */
105 public static byte[] decode(
106 String data)
107 {
108 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
109
110 try
111 {
112 encoder.decode(data, bOut);
113 }
114 catch (IOException e)
115 {
116 throw new RuntimeException("exception decoding base64 string: " + e);
117 }
118
119 return bOut.toByteArray();
120 }
121
122 /**
123 * decode the base 64 encoded String data writing it to the given output stream,
124 * whitespace characters will be ignored.
125 *
126 * @return the number of bytes produced.
127 */
128 public static int decode(
129 String data,
130 OutputStream out)
131 throws IOException
132 {
133 return encoder.decode(data, out);
134 }
135
136 /**
137 * decode the base 64 encoded String data writing it to the given output stream,
138 * whitespace characters will be ignored.
139 *
140 * @param data The array data to decode.
141 * @param out The output stream for the data.
142 *
143 * @return the number of bytes produced.
144 * @exception IOException
145 */
146 public static int decode(byte [] data, OutputStream out) throws IOException
147 {
148 return encoder.decode(data, 0, data.length, out);
149 }
150 }