KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > Base64Binary


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 public class Base64Binary {
21     /**
22      * * Convert from byte array to base 64 encoded string.
23      */

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

39             int value;
40             int chars;
41             if (i < n - 2) {
42                 value = (0x00FF0000 & (bytes[i] << 16))
43                         | (0x0000FF00 & (bytes[i + 1] << 8))
44                         | (0x000000FF & bytes[i + 2]);
45                 chars = 4;
46             } else if (i < n - 1) {
47                 value = (0x00FF0000 & (bytes[i] << 16))
48                         | (0x0000FF00 & (bytes[i + 1] << 8));
49                 chars = 3;
50             } else {
51                 value = (0x00FF0000 & (bytes[i] << 16));
52                 chars = 2;
53             }
54             while (chars-- > 0) {
55                 int x = (0x00FC0000 & value) >> 18;
56                 char c = getChar(x);
57                 s.append(c);
58                 value = value << 6;
59             }
60             if (i == n - 1) {
61                 s.append("==");
62             } else if (i == n - 2) {
63                 s.append('=');
64             }
65         }
66         return s.toString();
67     }
68
69     private static char getChar(int c) {
70         if (c < 26) {
71             return (char) ('A' + c);
72         } else if (c < 52) {
73             return (char) ('a' + (c - 26));
74         } else if (c < 62) {
75             return (char) ('0' + (c - 52));
76         } else if (c == 62) {
77             return '+';
78         } else // c == 63
79
{
80             return '/';
81         }
82     }
83 }
84
Popular Tags