01: package com.sun.portal.proxylet.client.common.server.util;
02:
03: public class Base64 {
04: private static byte[] Base64DecMap;
05:
06: // Class Initializer
07: static {
08: // rfc-2045: Base64 Alphabet
09: byte[] map = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
10: (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
11: (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
12: (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
13: (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
14: (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
15: (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b',
16: (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
17: (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',
18: (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
19: (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r',
20: (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
21: (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
22: (byte) '0', (byte) '1', (byte) '2', (byte) '3',
23: (byte) '4', (byte) '5', (byte) '6', (byte) '7',
24: (byte) '8', (byte) '9', (byte) '+', (byte) '/' };
25: Base64DecMap = new byte[128];
26: for (int idx = 0; idx < map.length; idx++)
27: Base64DecMap[map[idx]] = (byte) idx;
28: }
29:
30: /**
31: * This class isn't meant to be instantiated.
32: */
33: private Base64() {
34: }
35:
36: /**
37: * This method decodes the given string using the base64-encoding
38: * specified in RFC-2045(Section 6.8).
39: *
40: * @param str the base64-encoded string.
41: * @return the decoded <var>str</var>.
42: */
43: public final static String base64Decode(String str) {
44: if (str == null)
45: return null;
46:
47: // byte data[] = new byte[str.length()];
48: // str.getBytes(0, str.length(), data, 0);
49: byte data[] = str.getBytes();
50:
51: return new String(base64Decode(data));
52: }
53:
54: /**
55: * This method decodes the given byte[] using the base64-encoding
56: * specified in RFC-2045(Section 6.8).
57: *
58: * @param data the base64-encoded data.
59: * @return the decoded <var>data</var>.
60: */
61: public final static byte[] base64Decode(byte[] data) {
62: if (data == null)
63: return null;
64:
65: int tail = data.length;
66: while (data[tail - 1] == '=')
67: tail--;
68:
69: byte dest[] = new byte[tail - data.length / 4];
70:
71: // ascii printable to 0-63 conversion
72: for (int idx = 0; idx < data.length; idx++)
73: data[idx] = Base64DecMap[data[idx]];
74:
75: // 4-byte to 3-byte conversion
76: int sidx, didx;
77: for (sidx = 0, didx = 0; didx < dest.length - 2; sidx += 4, didx += 3) {
78: dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
79: dest[didx + 1] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
80: dest[didx + 2] = (byte) (((data[sidx + 2] << 6) & 255) | (data[sidx + 3] & 077));
81: }
82: if (didx < dest.length)
83: dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
84: if (++didx < dest.length)
85: dest[didx] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
86:
87: return dest;
88: }
89: }
|