001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.util;
026:
027: /**
028: *
029: * @author jitu
030: */
031: public class Base64Util {
032:
033: protected static final char encodeBase64[] = { 'A', 'B', 'C', 'D',
034: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
035: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
036: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
037: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
038: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
039:
040: protected static final int decodeBase64[] = {
041: /*'+'*/62, -1, -1, -1,
042: /*'/'*/63,
043: /*'0'*/52,
044: /*'1'*/53,
045: /*'2'*/54,
046: /*'3'*/55,
047: /*'4'*/56,
048: /*'5'*/57,
049: /*'6'*/58,
050: /*'7'*/59,
051: /*'8'*/60,
052: /*'9'*/61, -1, -1, -1, -1, -1, -1, -1,
053: /*'A'*/0,
054: /*'B'*/1,
055: /*'C'*/2,
056: /*'D'*/3,
057: /*'E'*/4,
058: /*'F'*/5,
059: /*'G'*/6,
060: /*'H'*/7,
061: /*'I'*/8,
062: /*'J'*/9,
063: /*'K'*/10,
064: /*'L'*/11,
065: /*'M'*/12,
066: /*'N'*/13,
067: /*'O'*/14,
068: /*'P'*/15,
069: /*'Q'*/16,
070: /*'R'*/17,
071: /*'S'*/18,
072: /*'T'*/19,
073: /*'U'*/20,
074: /*'V'*/21,
075: /*'W'*/22,
076: /*'X'*/23,
077: /*'Y'*/24,
078: /*'Z'*/25, -1, -1, -1, -1, -1, -1,
079: /*'a'*/26,
080: /*'b'*/27,
081: /*'c'*/28,
082: /*'d'*/29,
083: /*'e'*/30,
084: /*'f'*/31,
085: /*'g'*/32,
086: /*'h'*/33,
087: /*'i'*/34,
088: /*'j'*/35,
089: /*'k'*/36,
090: /*'l'*/37,
091: /*'m'*/38,
092: /*'n'*/39,
093: /*'o'*/40,
094: /*'p'*/41,
095: /*'q'*/42,
096: /*'r'*/43,
097: /*'s'*/44,
098: /*'t'*/45,
099: /*'u'*/46,
100: /*'v'*/47,
101: /*'w'*/48,
102: /*'x'*/49,
103: /*'y'*/50,
104: /*'z'*/51 };
105:
106: public static String encode(byte[] value) throws Exception {
107:
108: if (value == null) {
109: return null;
110: }
111: if (value.length == 0) {
112: return "";
113: }
114: int blockCount = value.length / 3;
115: int partialBlockLength = value.length % 3;
116:
117: if (partialBlockLength != 0) {
118: ++blockCount;
119: }
120:
121: int encodedLength = blockCount * 4;
122: StringBuffer encodedValue = new StringBuffer(encodedLength);
123:
124: int idx = 0;
125: for (int i = 0; i < blockCount; ++i) {
126: int b1 = value[idx++];
127: int b2 = (idx < value.length) ? value[idx++] : 0;
128: int b3 = (idx < value.length) ? value[idx++] : 0;
129:
130: if (b1 < 0) {
131: b1 += 256;
132: }
133: if (b2 < 0) {
134: b2 += 256;
135: }
136: if (b3 < 0) {
137: b3 += 256;
138: }
139:
140: char encodedChar;
141:
142: encodedChar = encodeBase64[b1 >> 2];
143: encodedValue.append(encodedChar);
144:
145: encodedChar = encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)];
146: encodedValue.append(encodedChar);
147:
148: encodedChar = encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)];
149: encodedValue.append(encodedChar);
150:
151: encodedChar = encodeBase64[b3 & 0x3f];
152: encodedValue.append(encodedChar);
153: }
154:
155: switch (partialBlockLength) {
156: case 0:
157: // do nothing
158: break;
159: case 1:
160: encodedValue.setCharAt(encodedLength - 1, '=');
161: encodedValue.setCharAt(encodedLength - 2, '=');
162: break;
163: case 2:
164: encodedValue.setCharAt(encodedLength - 1, '=');
165: break;
166: }
167:
168: return encodedValue.toString();
169: }
170:
171: }
|