001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.proxy;
043:
044: import java.io.ByteArrayOutputStream;
045:
046: /**
047: * Bas64 encode utility class.
048: *
049: * @author Maros Sandor
050: */
051: public class Base64Encoder {
052:
053: private static final char[] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
054: .toCharArray();
055:
056: private Base64Encoder() {
057: }
058:
059: public static String encode(byte[] data) {
060: return encode(data, false);
061: }
062:
063: public static String encode(byte[] data, boolean useNewlines) {
064: int length = data.length;
065: StringBuffer sb = new StringBuffer(data.length * 3 / 2);
066:
067: int end = length - 3;
068: int i = 0;
069: int lineCount = 0;
070:
071: while (i <= end) {
072: int d = ((((int) data[i]) & 0xFF) << 16)
073: | ((((int) data[i + 1]) & 0xFF) << 8)
074: | (((int) data[i + 2]) & 0xFF);
075: sb.append(characters[(d >> 18) & 0x3F]);
076: sb.append(characters[(d >> 12) & 0x3F]);
077: sb.append(characters[(d >> 6) & 0x3F]);
078: sb.append(characters[d & 0x3F]);
079: i += 3;
080:
081: if (useNewlines && lineCount++ >= 14) {
082: lineCount = 0;
083: sb.append(System.getProperty("line.separator"));
084: }
085: }
086:
087: if (i == length - 2) {
088: int d = ((((int) data[i]) & 0xFF) << 16)
089: | ((((int) data[i + 1]) & 0xFF) << 8);
090: sb.append(characters[(d >> 18) & 0x3F]);
091: sb.append(characters[(d >> 12) & 0x3F]);
092: sb.append(characters[(d >> 6) & 0x3F]);
093: sb.append("=");
094: } else if (i == length - 1) {
095: int d = (((int) data[i]) & 0xFF) << 16;
096: sb.append(characters[(d >> 18) & 0x3F]);
097: sb.append(characters[(d >> 12) & 0x3F]);
098: sb.append("==");
099: }
100: return sb.toString();
101: }
102:
103: public static byte[] decode(String s) {
104: ByteArrayOutputStream bos = new ByteArrayOutputStream();
105: decode(s, bos);
106: return bos.toByteArray();
107: }
108:
109: private static void decode(String s, ByteArrayOutputStream bos) {
110: int i = 0;
111: int len = s.length();
112: for (;;) {
113: while (i < len && s.charAt(i) <= ' ')
114: i++;
115: if (i == len)
116: break;
117: int tri = (decode(s.charAt(i)) << 18)
118: + (decode(s.charAt(i + 1)) << 12)
119: + (decode(s.charAt(i + 2)) << 6)
120: + (decode(s.charAt(i + 3)));
121: bos.write((tri >> 16) & 255);
122: if (s.charAt(i + 2) == '=')
123: break;
124: bos.write((tri >> 8) & 255);
125: if (s.charAt(i + 3) == '=')
126: break;
127: bos.write(tri & 255);
128: i += 4;
129: }
130: }
131:
132: private static int decode(char c) {
133: if (c >= 'A' && c <= 'Z') {
134: return ((int) c) - 65;
135: } else if (c >= 'a' && c <= 'z') {
136: return ((int) c) - 97 + 26;
137: } else if (c >= '0' && c <= '9') {
138: return ((int) c) - 48 + 26 + 26;
139: } else
140: switch (c) {
141: case '+':
142: return 62;
143: case '/':
144: return 63;
145: case '=':
146: return 0;
147: default:
148: throw new RuntimeException("Base64: unexpected code: "
149: + c);
150: }
151: }
152: }
|