001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: /**
021: * BASE 64 encoding of a String or an array of bytes.
022: *
023: * Based on RFC 1421.
024: *
025: **/
026: public class Base64Converter {
027:
028: private static final char[] ALPHABET = { 'A', 'B', 'C', 'D', 'E',
029: 'F', 'G', 'H', // 0 to 7
030: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
031: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
032: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
033: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
034: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
035: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
036: '4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63
037:
038: // CheckStyle:ConstantNameCheck OFF - bc
039: /** Provided for BC purposes */
040: public static final char[] alphabet = ALPHABET;
041:
042: // CheckStyle:ConstantNameCheck ON
043:
044: /**
045: * Encode a string into base64 encoding.
046: * @param s the string to encode.
047: * @return the encoded string.
048: */
049: public String encode(String s) {
050: return encode(s.getBytes());
051: }
052:
053: /**
054: * Encode a byte array into base64 encoding.
055: * @param octetString the byte array to encode.
056: * @return the encoded string.
057: */
058: public String encode(byte[] octetString) {
059: int bits24;
060: int bits6;
061:
062: char[] out = new char[((octetString.length - 1) / 3 + 1) * 4];
063: int outIndex = 0;
064: int i = 0;
065:
066: while ((i + 3) <= octetString.length) {
067: // store the octets
068: bits24 = (octetString[i++] & 0xFF) << 16;
069: bits24 |= (octetString[i++] & 0xFF) << 8;
070: bits24 |= octetString[i++];
071:
072: bits6 = (bits24 & 0x00FC0000) >> 18;
073: out[outIndex++] = ALPHABET[bits6];
074: bits6 = (bits24 & 0x0003F000) >> 12;
075: out[outIndex++] = ALPHABET[bits6];
076: bits6 = (bits24 & 0x00000FC0) >> 6;
077: out[outIndex++] = ALPHABET[bits6];
078: bits6 = (bits24 & 0x0000003F);
079: out[outIndex++] = ALPHABET[bits6];
080: }
081: if (octetString.length - i == 2) {
082: // store the octets
083: bits24 = (octetString[i] & 0xFF) << 16;
084: bits24 |= (octetString[i + 1] & 0xFF) << 8;
085: bits6 = (bits24 & 0x00FC0000) >> 18;
086: out[outIndex++] = ALPHABET[bits6];
087: bits6 = (bits24 & 0x0003F000) >> 12;
088: out[outIndex++] = ALPHABET[bits6];
089: bits6 = (bits24 & 0x00000FC0) >> 6;
090: out[outIndex++] = ALPHABET[bits6];
091:
092: // padding
093: out[outIndex++] = '=';
094: } else if (octetString.length - i == 1) {
095: // store the octets
096: bits24 = (octetString[i] & 0xFF) << 16;
097: bits6 = (bits24 & 0x00FC0000) >> 18;
098: out[outIndex++] = ALPHABET[bits6];
099: bits6 = (bits24 & 0x0003F000) >> 12;
100: out[outIndex++] = ALPHABET[bits6];
101:
102: // padding
103: out[outIndex++] = '=';
104: out[outIndex++] = '=';
105: }
106: return new String(out);
107: }
108: }
|