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 package org.apache.geronimo.mail.util;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26 public class Base64
27 {
28 private static final Encoder encoder = new Base64Encoder();
29
30 /**
31 * encode the input data producing a base 64 encoded byte array.
32 *
33 * @return a byte array containing the base 64 encoded data.
34 */
35 public static byte[] encode(
36 byte[] data)
37 {
38 // just forward to the general array encoder.
39 return encode(data, 0, data.length);
40 }
41
42 /**
43 * encode the input data producing a base 64 encoded byte array.
44 *
45 * @param data The data array to encode.
46 * @param offset The starting offset within the data array.
47 * @param length The length of the data to encode.
48 *
49 * @return a byte array containing the base 64 encoded data.
50 */
51 public static byte[] encode(
52 byte[] data,
53 int offset,
54 int length)
55 {
56 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
57
58 try
59 {
60 encoder.encode(data, 0, data.length, bOut);
61 }
62 catch (IOException e)
63 {
64 throw new RuntimeException("exception encoding base64 string: " + e);
65 }
66
67 return bOut.toByteArray();
68 }
69
70 /**
71 * Encode the byte data to base 64 writing it to the given output stream.
72 *
73 * @return the number of bytes produced.
74 */
75 public static int encode(
76 byte[] data,
77 OutputStream out)
78 throws IOException
79 {
80 return encoder.encode(data, 0, data.length, out);
81 }
82
83 /**
84 * Encode the byte data to base 64 writing it to the given output stream.
85 *
86 * @return the number of bytes produced.
87 */
88 public static int encode(
89 byte[] data,
90 int off,
91 int length,
92 OutputStream out)
93 throws IOException
94 {
95 return encoder.encode(data, off, length, out);
96 }
97
98 /**
99 * decode the base 64 encoded input data. It is assumed the input data is valid.
100 *
101 * @return a byte array representing the decoded data.
102 */
103 public static byte[] decode(
104 byte[] data)
105 {
106 // just decode the entire array of data.
107 return decode(data, 0, data.length);
108 }
109
110
111 /**
112 * decode the base 64 encoded input data. It is assumed the input data is valid.
113 *
114 * @param data The data array to decode.
115 * @param offset The offset of the data array.
116 * @param length The length of data to decode.
117 *
118 * @return a byte array representing the decoded data.
119 */
120 public static byte[] decode(
121 byte[] data,
122 int offset,
123 int length)
124 {
125 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
126
127 try
128 {
129 encoder.decode(data, offset, length, bOut);
130 }
131 catch (IOException e)
132 {
133 throw new RuntimeException("exception decoding base64 string: " + e);
134 }
135
136 return bOut.toByteArray();
137 }
138
139 /**
140 * decode the base 64 encoded String data - whitespace will be ignored.
141 *
142 * @return a byte array representing the decoded data.
143 */
144 public static byte[] decode(
145 String data)
146 {
147 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
148
149 try
150 {
151 encoder.decode(data, bOut);
152 }
153 catch (IOException e)
154 {
155 throw new RuntimeException("exception decoding base64 string: " + e);
156 }
157
158 return bOut.toByteArray();
159 }
160
161 /**
162 * decode the base 64 encoded String data writing it to the given output stream,
163 * whitespace characters will be ignored.
164 *
165 * @return the number of bytes produced.
166 */
167 public static int decode(
168 String data,
169 OutputStream out)
170 throws IOException
171 {
172 return encoder.decode(data, out);
173 }
174
175 /**
176 * decode the base 64 encoded String data writing it to the given output stream,
177 * whitespace characters will be ignored.
178 *
179 * @param data The array data to decode.
180 * @param out The output stream for the data.
181 *
182 * @return the number of bytes produced.
183 * @exception IOException
184 */
185 public static int decode(byte [] data, OutputStream out) throws IOException
186 {
187 return encoder.decode(data, 0, data.length, out);
188 }
189 }