KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > Base64Binary


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 public class Base64Binary
22 {
23     /**
24      ** Convert from byte array to base 64 encoded string.
25      **/

26     public static String toString(byte[] bytes)
27     {
28         return toString(bytes, 0, bytes.length);
29     }
30
31     public static String toString(byte[] bytes, int offset, int length)
32     {
33         StringBuffer s = new StringBuffer((length * 4) / 3 + 1);
34         int n = offset + length;
35         for (int i = offset; i < n; i += 3)
36         {
37             /* TODO: make this optional???
38             if (i != 0 && i % 18 == 0)
39             {
40                 // Must have at most 76 characters per line.
41                 s.append('\n');
42             }
43             */

44             int value;
45             int chars;
46             if (i < n - 2)
47             {
48                 value = (0x00FF0000 & (bytes[i] << 16))
49                     | (0x0000FF00 & (bytes[i + 1] << 8))
50                     | (0x000000FF & bytes[i + 2]);
51                 chars = 4;
52             }
53             else if (i < n - 1)
54             {
55                 value = (0x00FF0000 & (bytes[i] << 16))
56                     | (0x0000FF00 & (bytes[i + 1] << 8));
57                 chars = 3;
58             }
59             else
60             {
61                 value = (0x00FF0000 & (bytes[i] << 16));
62                 chars = 2;
63             }
64             while (chars-- > 0)
65             {
66                 int x = (0x00FC0000 & value) >> 18;
67                 char c = getChar(x);
68                 s.append(c);
69                 value = value << 6;
70             }
71             if (i == n - 1)
72             {
73                 s.append("==");
74             }
75             else if (i == n - 2)
76             {
77                 s.append('=');
78             }
79         }
80         return s.toString();
81     }
82
83     private static char getChar(int c)
84     {
85         if (c < 26)
86         {
87             return (char)('A' + c);
88         }
89         else if (c < 52)
90         {
91             return (char)('a' + (c - 26));
92         }
93         else if (c < 62)
94         {
95             return (char)('0' + (c - 52));
96         }
97         else if (c == 62)
98         {
99             return '+';
100         }
101         else // c == 63
102
{
103             return '/';
104         }
105     }
106 }
107
Popular Tags