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 Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package org.jvnet.staxex;
038:
039: /**
040: * @author Kohsuke Kawaguchi
041: */
042: class Base64Encoder {
043: private static final char[] encodeMap = initEncodeMap();
044:
045: private static char[] initEncodeMap() {
046: char[] map = new char[64];
047: int i;
048: for (i = 0; i < 26; i++)
049: map[i] = (char) ('A' + i);
050: for (i = 26; i < 52; i++)
051: map[i] = (char) ('a' + (i - 26));
052: for (i = 52; i < 62; i++)
053: map[i] = (char) ('0' + (i - 52));
054: map[62] = '+';
055: map[63] = '/';
056:
057: return map;
058: }
059:
060: public static char encode(int i) {
061: return encodeMap[i & 0x3F];
062: }
063:
064: public static byte encodeByte(int i) {
065: return (byte) encodeMap[i & 0x3F];
066: }
067:
068: public static String print(byte[] input, int offset, int len) {
069: char[] buf = new char[((len + 2) / 3) * 4];
070: int ptr = print(input, offset, len, buf, 0);
071: assert ptr == buf.length;
072: return new String(buf);
073: }
074:
075: /**
076: * Encodes a byte array into a char array by doing base64 encoding.
077: *
078: * The caller must supply a big enough buffer.
079: *
080: * @return
081: * the value of {@code ptr+((len+2)/3)*4}, which is the new offset
082: * in the output buffer where the further bytes should be placed.
083: */
084: public static int print(byte[] input, int offset, int len,
085: char[] buf, int ptr) {
086: for (int i = offset; i < len; i += 3) {
087: switch (len - i) {
088: case 1:
089: buf[ptr++] = encode(input[i] >> 2);
090: buf[ptr++] = encode(((input[i]) & 0x3) << 4);
091: buf[ptr++] = '=';
092: buf[ptr++] = '=';
093: break;
094: case 2:
095: buf[ptr++] = encode(input[i] >> 2);
096: buf[ptr++] = encode(((input[i] & 0x3) << 4)
097: | ((input[i + 1] >> 4) & 0xF));
098: buf[ptr++] = encode((input[i + 1] & 0xF) << 2);
099: buf[ptr++] = '=';
100: break;
101: default:
102: buf[ptr++] = encode(input[i] >> 2);
103: buf[ptr++] = encode(((input[i] & 0x3) << 4)
104: | ((input[i + 1] >> 4) & 0xF));
105: buf[ptr++] = encode(((input[i + 1] & 0xF) << 2)
106: | ((input[i + 2] >> 6) & 0x3));
107: buf[ptr++] = encode(input[i + 2] & 0x3F);
108: break;
109: }
110: }
111: return ptr;
112: }
113: }
|