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