001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.geronimo.mail.util;
021
022 import java.io.ByteArrayOutputStream;
023 import java.io.IOException;
024 import java.io.OutputStream;
025
026 public class Base64
027 {
028 private static final Encoder encoder = new Base64Encoder();
029
030 /**
031 * encode the input data producing a base 64 encoded byte array.
032 *
033 * @return a byte array containing the base 64 encoded data.
034 */
035 public static byte[] encode(
036 byte[] data)
037 {
038 // just forward to the general array encoder.
039 return encode(data, 0, data.length);
040 }
041
042 /**
043 * encode the input data producing a base 64 encoded byte array.
044 *
045 * @param data The data array to encode.
046 * @param offset The starting offset within the data array.
047 * @param length The length of the data to encode.
048 *
049 * @return a byte array containing the base 64 encoded data.
050 */
051 public static byte[] encode(
052 byte[] data,
053 int offset,
054 int length)
055 {
056 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
057
058 try
059 {
060 encoder.encode(data, 0, data.length, bOut);
061 }
062 catch (IOException e)
063 {
064 throw new RuntimeException("exception encoding base64 string: " + e);
065 }
066
067 return bOut.toByteArray();
068 }
069
070 /**
071 * Encode the byte data to base 64 writing it to the given output stream.
072 *
073 * @return the number of bytes produced.
074 */
075 public static int encode(
076 byte[] data,
077 OutputStream out)
078 throws IOException
079 {
080 return encoder.encode(data, 0, data.length, out);
081 }
082
083 /**
084 * Encode the byte data to base 64 writing it to the given output stream.
085 *
086 * @return the number of bytes produced.
087 */
088 public static int encode(
089 byte[] data,
090 int off,
091 int length,
092 OutputStream out)
093 throws IOException
094 {
095 return encoder.encode(data, off, length, out);
096 }
097
098 /**
099 * 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 }