KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > Base64


1 /*
2  * $Id: Base64.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util;
26
27 /**
28  * Base64 implements Base64 encoding and Base 64 decoding.
29  *
30  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
31  *@author UPS XPCI Sample Code
32  *@version $Rev: 5462 $
33  *@since 2.2
34  */

35
36 public class Base64 {
37
38     private static byte[] Base64EncMap, Base64DecMap;
39     static {
40         // rfc-2045: Base64 Alphabet
41
byte[] map =
42             {
43                 (byte) 'A',
44                 (byte) 'B',
45                 (byte) 'C',
46                 (byte) 'D',
47                 (byte) 'E',
48                 (byte) 'F',
49                 (byte) 'G',
50                 (byte) 'H',
51                 (byte) 'I',
52                 (byte) 'J',
53                 (byte) 'K',
54                 (byte) 'L',
55                 (byte) 'M',
56                 (byte) 'N',
57                 (byte) 'O',
58                 (byte) 'P',
59                 (byte) 'Q',
60                 (byte) 'R',
61                 (byte) 'S',
62                 (byte) 'T',
63                 (byte) 'U',
64                 (byte) 'V',
65                 (byte) 'W',
66                 (byte) 'X',
67                 (byte) 'Y',
68                 (byte) 'Z',
69                 (byte) 'a',
70                 (byte) 'b',
71                 (byte) 'c',
72                 (byte) 'd',
73                 (byte) 'e',
74                 (byte) 'f',
75                 (byte) 'g',
76                 (byte) 'h',
77                 (byte) 'i',
78                 (byte) 'j',
79                 (byte) 'k',
80                 (byte) 'l',
81                 (byte) 'm',
82                 (byte) 'n',
83                 (byte) 'o',
84                 (byte) 'p',
85                 (byte) 'q',
86                 (byte) 'r',
87                 (byte) 's',
88                 (byte) 't',
89                 (byte) 'u',
90                 (byte) 'v',
91                 (byte) 'w',
92                 (byte) 'x',
93                 (byte) 'y',
94                 (byte) 'z',
95                 (byte) '0',
96                 (byte) '1',
97                 (byte) '2',
98                 (byte) '3',
99                 (byte) '4',
100                 (byte) '5',
101                 (byte) '6',
102                 (byte) '7',
103                 (byte) '8',
104                 (byte) '9',
105                 (byte) '+',
106                 (byte) '/' };
107         Base64EncMap = map;
108         Base64DecMap = new byte[128];
109         for (int idx = 0; idx < Base64EncMap.length; idx++) {
110             Base64DecMap[Base64EncMap[idx]] = (byte) idx;
111         }
112     }
113     
114     /**
115      * This method decodes the given byte[] using the base64-encoding
116      * specified in RFC-2045 (Section 6.8).
117      *
118      * @param data the base64-encoded data.
119      * @return the decoded data.
120      */

121     public final static byte[] base64Decode(byte[] data) {
122         if (data == null) {
123             return null;
124         }
125
126         int tail = data.length;
127         while (data[tail - 1] == '=') {
128             tail--;
129         }
130
131         byte dest[] = new byte[tail - data.length / 4];
132
133         // ascii printable to 0-63 conversion
134
for (int idx = 0; idx < data.length; idx++) {
135             data[idx] = Base64DecMap[data[idx]];
136         }
137
138         // 4-byte to 3-byte conversion
139
int sidx, didx;
140         for (sidx = 0, didx = 0; didx < dest.length - 2; sidx += 4, didx += 3) {
141             dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
142             dest[didx + 1] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
143             dest[didx + 2] = (byte) (((data[sidx + 2] << 6) & 255) | (data[sidx + 3] & 077));
144         }
145         if (didx < dest.length) {
146             dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
147         }
148         if (++didx < dest.length) {
149             dest[didx] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
150         }
151
152         return dest;
153     }
154     
155     /**
156      * This method decodes the given string using the base64-encoding
157      * specified in RFC-2045 (Section 6.8).
158      *
159      * @param str the base64-encoded string.
160      * @return the decoded str.
161      */

162     public final static String JavaDoc base64Decode(String JavaDoc str) {
163         if (str == null) {
164             return null;
165         }
166         
167         byte data[] = new byte[str.length()];
168         data = str.getBytes();
169         return new String JavaDoc(base64Decode(data));
170     }
171     
172     /**
173      * This method encodes the given byte[] using the base64-encoding
174      * specified in RFC-2045 (Section 6.8).
175      *
176      * @param data the data
177      * @return the base64-encoded data
178      */

179     public final static byte[] base64Encode(byte[] data) {
180         if (data == null) {
181             return null;
182         }
183
184         int sidx, didx;
185         byte dest[] = new byte[((data.length + 2) / 3) * 4];
186
187         // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
188
for (sidx = 0, didx = 0; sidx < data.length - 2; sidx += 3) {
189             dest[didx++] = Base64EncMap[(data[sidx] >>> 2) & 077];
190             dest[didx++] = Base64EncMap[(data[sidx + 1] >>> 4) & 017 | (data[sidx] << 4) & 077];
191             dest[didx++] = Base64EncMap[(data[sidx + 2] >>> 6) & 003 | (data[sidx + 1] << 2) & 077];
192             dest[didx++] = Base64EncMap[data[sidx + 2] & 077];
193         }
194         if (sidx < data.length) {
195             dest[didx++] = Base64EncMap[(data[sidx] >>> 2) & 077];
196             if (sidx < data.length - 1) {
197                 dest[didx++] = Base64EncMap[(data[sidx + 1] >>> 4) & 017 | (data[sidx] << 4) & 077];
198                 dest[didx++] = Base64EncMap[(data[sidx + 1] << 2) & 077];
199             } else
200                 dest[didx++] = Base64EncMap[(data[sidx] << 4) & 077];
201         }
202
203         // add padding
204
for (; didx < dest.length; didx++) {
205             dest[didx] = (byte) '=';
206         }
207
208         return dest;
209     }
210     
211     /**
212      * This method encodes the given string using the base64-encoding
213      * specified in RFC-2045 (Section 6.8).
214      *
215      * @param str the string
216      * @return the base64-encoded str
217      */

218     public final static String JavaDoc base64Encode(String JavaDoc str) {
219         if (str == null) {
220             return null;
221         }
222         byte data[] = new byte[str.length()];
223
224         data = str.getBytes();
225         return new String JavaDoc(base64Encode(data));
226     }
227 }
228
Popular Tags