KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > encoders > UrlBase64


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.util.encoders;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Convert binary data to and from UrlBase64 encoding. This is identical to
26  * Base64 encoding, except that the padding character is "." and the other
27  * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
28  * <p>
29  * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
30  * data that is safe for use as an URL parameter. Base64 encoding does not
31  * produce encoded values that are safe for use in URLs, since "/" can be
32  * interpreted as a path delimiter; "+" is the encoded form of a space; and
33  * "=" is used to separate a name from the corresponding value in an URL
34  * parameter.
35  */

36 public class UrlBase64
37 {
38     private static final Encoder encoder = new UrlBase64Encoder();
39
40     /**
41      * Encode the input data producing a URL safe base 64 encoded byte array.
42      *
43      * @return a byte array containing the URL safe base 64 encoded data.
44      */

45     public static byte[] encode(
46         byte[] data)
47     {
48         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
49
50         try
51         {
52             encoder.encode(data, 0, data.length, bOut);
53         }
54         catch (IOException JavaDoc e)
55         {
56             throw new RuntimeException JavaDoc("exception encoding URL safe base64 string: " + e);
57         }
58
59         return bOut.toByteArray();
60     }
61
62     /**
63      * Encode the byte data writing it to the given output stream.
64      *
65      * @return the number of bytes produced.
66      */

67     public static int encode(
68         byte[] data,
69         OutputStream JavaDoc out)
70         throws IOException JavaDoc
71     {
72         return encoder.encode(data, 0, data.length, out);
73     }
74
75     /**
76      * Decode the URL safe base 64 encoded input data - white space will be ignored.
77      *
78      * @return a byte array representing the decoded data.
79      */

80     public static byte[] decode(
81         byte[] data)
82     {
83         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
84
85         try
86         {
87             encoder.decode(data, 0, data.length, bOut);
88         }
89         catch (IOException JavaDoc e)
90         {
91             throw new RuntimeException JavaDoc("exception decoding URL safe base64 string: " + e);
92         }
93
94         return bOut.toByteArray();
95     }
96
97     /**
98      * decode the URL safe base 64 encoded byte data writing it to the given output stream,
99      * whitespace characters will be ignored.
100      *
101      * @return the number of bytes produced.
102      */

103     public static int decode(
104         byte[] data,
105         OutputStream JavaDoc out)
106         throws IOException JavaDoc
107     {
108         return encoder.decode(data, 0, data.length, out);
109     }
110
111     /**
112      * decode the URL safe base 64 encoded String data - whitespace will be ignored.
113      *
114      * @return a byte array representing the decoded data.
115      */

116     public static byte[] decode(
117         String JavaDoc data)
118     {
119         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
120
121         try
122         {
123             encoder.decode(data, bOut);
124         }
125         catch (IOException JavaDoc e)
126         {
127             throw new RuntimeException JavaDoc("exception decoding URL safe base64 string: " + e);
128         }
129
130         return bOut.toByteArray();
131     }
132
133     /**
134      * Decode the URL safe base 64 encoded String data writing it to the given output stream,
135      * whitespace characters will be ignored.
136      *
137      * @return the number of bytes produced.
138      */

139     public static int decode(
140         String JavaDoc data,
141         OutputStream JavaDoc out)
142         throws IOException JavaDoc
143     {
144         return encoder.decode(data, out);
145     }
146 }
147
Popular Tags