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 /**
26 * Convert binary data to and from UrlBase64 encoding. This is identical to
27 * Base64 encoding, except that the padding character is "." and the other
28 * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
29 * <p>
30 * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
31 * data that is safe for use as an URL parameter. Base64 encoding does not
32 * produce encoded values that are safe for use in URLs, since "/" can be
33 * interpreted as a path delimiter; "+" is the encoded form of a space; and
34 * "=" is used to separate a name from the corresponding value in an URL
35 * parameter.
36 */
37 public class UrlBase64
38 {
39 private static final Encoder encoder = new UrlBase64Encoder();
40
41 /**
42 * Encode the input data producing a URL safe base 64 encoded byte array.
43 *
44 * @return a byte array containing the URL safe base 64 encoded data.
45 */
46 public static byte[] encode(
47 byte[] data)
48 {
49 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
50
51 try
52 {
53 encoder.encode(data, 0, data.length, bOut);
54 }
55 catch (IOException e)
56 {
57 throw new RuntimeException("exception encoding URL safe base64 string: " + e);
58 }
59
60 return bOut.toByteArray();
61 }
62
63 /**
64 * Encode the byte data 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 OutputStream out)
71 throws IOException
72 {
73 return encoder.encode(data, 0, data.length, out);
74 }
75
76 /**
77 * Decode the URL safe base 64 encoded input data - white space will be ignored.
78 *
79 * @return a byte array representing the decoded data.
80 */
81 public static byte[] decode(
82 byte[] data)
83 {
84 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
85
86 try
87 {
88 encoder.decode(data, 0, data.length, bOut);
89 }
90 catch (IOException e)
91 {
92 throw new RuntimeException("exception decoding URL safe base64 string: " + e);
93 }
94
95 return bOut.toByteArray();
96 }
97
98 /**
99 * decode the URL safe base 64 encoded byte data writing it to the given output stream,
100 * whitespace characters will be ignored.
101 *
102 * @return the number of bytes produced.
103 */
104 public static int decode(
105 byte[] data,
106 OutputStream out)
107 throws IOException
108 {
109 return encoder.decode(data, 0, data.length, out);
110 }
111
112 /**
113 * decode the URL safe base 64 encoded String data - whitespace will be ignored.
114 *
115 * @return a byte array representing the decoded data.
116 */
117 public static byte[] decode(
118 String data)
119 {
120 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
121
122 try
123 {
124 encoder.decode(data, bOut);
125 }
126 catch (IOException e)
127 {
128 throw new RuntimeException("exception decoding URL safe base64 string: " + e);
129 }
130
131 return bOut.toByteArray();
132 }
133
134 /**
135 * Decode the URL safe base 64 encoded String data writing it to the given output stream,
136 * whitespace characters will be ignored.
137 *
138 * @return the number of bytes produced.
139 */
140 public static int decode(
141 String data,
142 OutputStream out)
143 throws IOException
144 {
145 return encoder.decode(data, out);
146 }
147 }