KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > Base64Converter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 /**
21  * BASE 64 encoding of a String or an array of bytes.
22  *
23  * Based on RFC 1421.
24  *
25  **/

26 public class Base64Converter {
27
28     private static final char[] ALPHABET = {
29         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
30
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
31
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
32
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
33
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
34
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
35
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
36
'4', '5', '6', '7', '8', '9', '+', '/'}; // 56 to 63
37

38     // CheckStyle:ConstantNameCheck OFF - bc
39
/** Provided for BC purposes */
40     public static final char[] alphabet = ALPHABET;
41     // CheckStyle:ConstantNameCheck ON
42

43
44     /**
45      * Encode a string into base64 encoding.
46      * @param s the string to encode.
47      * @return the encoded string.
48      */

49     public String JavaDoc encode(String JavaDoc s) {
50         return encode(s.getBytes());
51     }
52
53     /**
54      * Encode a byte array into base64 encoding.
55      * @param octetString the byte array to encode.
56      * @return the encoded string.
57      */

58     public String JavaDoc encode(byte[] octetString) {
59         int bits24;
60         int bits6;
61
62         char[] out = new char[((octetString.length - 1) / 3 + 1) * 4];
63         int outIndex = 0;
64         int i = 0;
65
66         while ((i + 3) <= octetString.length) {
67             // store the octets
68
bits24 = (octetString[i++] & 0xFF) << 16;
69             bits24 |= (octetString[i++] & 0xFF) << 8;
70             bits24 |= octetString[i++];
71
72             bits6 = (bits24 & 0x00FC0000) >> 18;
73             out[outIndex++] = ALPHABET[bits6];
74             bits6 = (bits24 & 0x0003F000) >> 12;
75             out[outIndex++] = ALPHABET[bits6];
76             bits6 = (bits24 & 0x00000FC0) >> 6;
77             out[outIndex++] = ALPHABET[bits6];
78             bits6 = (bits24 & 0x0000003F);
79             out[outIndex++] = ALPHABET[bits6];
80         }
81         if (octetString.length - i == 2) {
82             // store the octets
83
bits24 = (octetString[i] & 0xFF) << 16;
84             bits24 |= (octetString[i + 1] & 0xFF) << 8;
85             bits6 = (bits24 & 0x00FC0000) >> 18;
86             out[outIndex++] = ALPHABET[bits6];
87             bits6 = (bits24 & 0x0003F000) >> 12;
88             out[outIndex++] = ALPHABET[bits6];
89             bits6 = (bits24 & 0x00000FC0) >> 6;
90             out[outIndex++] = ALPHABET[bits6];
91
92             // padding
93
out[outIndex++] = '=';
94         } else if (octetString.length - i == 1) {
95             // store the octets
96
bits24 = (octetString[i] & 0xFF) << 16;
97             bits6 = (bits24 & 0x00FC0000) >> 18;
98             out[outIndex++] = ALPHABET[bits6];
99             bits6 = (bits24 & 0x0003F000) >> 12;
100             out[outIndex++] = ALPHABET[bits6];
101
102             // padding
103
out[outIndex++] = '=';
104             out[outIndex++] = '=';
105         }
106         return new String JavaDoc(out);
107     }
108 }
109
Popular Tags