KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > util > encoding > Base64


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.util.encoding;
35
36 /**
37  * This <code>Base64</code> is a utility class for encoding using the Base64
38  * encoding. </p>
39  *
40  * The Base64 encoding is designed to represent arbitrary sequences of octets in
41  * a form that need to be humanly readable. The encoding and decoding algorithms
42  * are simple, but the encoded data are consitently only about 33% larger than
43  * the unencoded data. </p>
44  *
45  * For more complete information about the Base64 encoding, please read <a
46  * HREF="http://www.ietf.org/rfc/rfc1521.txt" target="_top">MIME (Multipurpose
47  * Internet Mail Extensions) Part One</a> (Section 5.2). </p>
48  */

49 public class Base64 {
50     private static final byte[] BASE64_ALPHABET_ARRAY = {
51             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
52             'N',
53             'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
54             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
55             'n',
56             'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
57             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
58             '+', '/'
59     };
60     private static final byte[] BASE64_FOR_URL_ALPHABET_ARRAY = {
61             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
62             'N',
63             'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
64             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
65             'n',
66             'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
67             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
68             '-', '_'
69     };
70     private static final byte PAD = '=';
71
72     /**
73      * Encodes the specified <code>bytes</code> using the Base64 encoding. </p>
74      *
75      * @param bytes the bytes to be encoded.
76      * @return the Base64-encoded bytes.
77      * @see #encode(String)
78      */

79     public static byte[] encode(byte[] bytes) {
80         return encode(bytes, BASE64_ALPHABET_ARRAY, true);
81     }
82
83     /**
84      * Encodes the specified <code>string</code> using the Base64 encoding. </p>
85      *
86      * @param string the string to be encoded.
87      * @return the Base64-encoded string.
88      * @see #encode(byte[])
89      */

90     public static String JavaDoc encode(String JavaDoc string) {
91         if (string == null) {
92             return null;
93         }
94         return new String JavaDoc(encode(string.getBytes()));
95     }
96
97     /**
98      * Encodes the specified <code>bytes</code> using the Base64 encoding for
99      * URL usage. </p>
100      *
101      * @param bytes the bytes to be encoded.
102      * @return the Base64-encoded bytes for URL usage.
103      */

104     public static byte[] encodeForURL(byte[] bytes) {
105         return encode(bytes, BASE64_FOR_URL_ALPHABET_ARRAY, false);
106     }
107
108     private static byte[] encode(
109             byte[] bytes, byte[] alphabetArray, boolean usePadding) {
110
111         if (bytes == null) {
112             return null;
113         } else if (bytes.length == 0) {
114             return bytes;
115         }
116         int _length = bytes.length;
117         int _remainder = _length % 3;
118         byte[] _bytes;
119         if (usePadding) {
120             _bytes = new byte[(_length + 2) / 3 * 4];
121         } else {
122             _bytes =
123                     new byte[
124                             ((_length + 2) / 3 * 4) -
125                             (_remainder != 0 ? 3 - _remainder : 0)];
126         }
127         _length -= _remainder;
128         int _group;
129         int _i;
130         int _index = 0;
131         for (_i = 0; _i < _length;) {
132             _group =
133                     (bytes[_i++] & 0xFF) << 16 |
134                     (bytes[_i++] & 0xFF) << 8 |
135                     bytes[_i++] & 0xFF;
136             _bytes[_index++] = alphabetArray[_group >>> 18];
137             _bytes[_index++] = alphabetArray[_group >>> 12 & 0x3F];
138             _bytes[_index++] = alphabetArray[_group >>> 6 & 0x3F];
139             _bytes[_index++] = alphabetArray[_group & 0x3F];
140         }
141         switch (_remainder) {
142             case 0:
143                 break;
144             case 1:
145                 _group = (bytes[_i] & 0xFF) << 4;
146                 _bytes[_index++] = alphabetArray[_group >>> 6];
147                 if (usePadding) {
148                     _bytes[_index++] = alphabetArray[_group & 0x3F];
149                     _bytes[_index++] = PAD;
150                     _bytes[_index] = PAD;
151                 } else {
152                     _bytes[_index] = alphabetArray[_group & 0x3F];
153                 }
154                 break;
155             case 2:
156                 _group = ((bytes[_i++] & 0xFF) << 8 | (bytes[_i] & 0xFF)) << 2;
157                 _bytes[_index++] = alphabetArray[_group >>> 12];
158                 _bytes[_index++] = alphabetArray[_group >>> 6 & 0x3F];
159                 if (usePadding) {
160                     _bytes[_index++] = alphabetArray[_group & 0x3F];
161                     _bytes[_index] = PAD;
162                 } else {
163                     _bytes[_index] = alphabetArray[_group & 0x3F];
164                 }
165                 break;
166             default:
167                 // this should never happen.
168
}
169         return _bytes;
170     }
171 }
172
Popular Tags