KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > bussinessproxy > remote > http > util > Base64


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.bussinessproxy.remote.http.util;
17
18 public class Base64 {
19
20    protected static char getChar(int sixbit) {
21       if (sixbit >= 0 && sixbit <= 25) {
22          return (char)(65 + sixbit);
23       }
24
25       if (sixbit >= 26 && sixbit <= 51) {
26          return (char)(97 + (sixbit - 26));
27       }
28
29       if (sixbit >= 52 && sixbit <= 61) {
30          return (char)(48 + (sixbit - 52));
31       }
32
33       if (sixbit == 62) {
34          return '+';
35       }
36
37       return sixbit != 63 ? '?' : '/';
38    }
39
40    protected static int getValue(char c) {
41       if (c >= 'A' && c <= 'Z') {
42          return c - 65;
43       }
44
45       if (c >= 'a' && c <= 'z') {
46          return (c - 97) + 26;
47       }
48
49       if (c >= '0' && c <= '9') {
50          return (c - 48) + 52;
51       }
52
53       if (c == '+') {
54          return 62;
55       }
56
57       if (c == '/') {
58          return 63;
59       }
60
61       return c != '=' ? -1 : 0;
62    }
63
64    public static String JavaDoc encode(byte raw[]) {
65       StringBuffer JavaDoc encoded = new StringBuffer JavaDoc();
66
67       for (int i = 0; i < raw.length; i += 3) {
68          encoded.append(encodeBlock(raw, i));
69       }
70
71       return encoded.toString();
72    }
73
74    protected static char[] encodeBlock(byte raw[], int offset) {
75       int block = 0;
76       int slack = raw.length - offset - 1;
77       int end = slack < 2 ? slack : 2;
78
79       for (int i = 0; i <= end; i++) {
80          byte b = raw[offset + i];
81
82          int neuter = b >= 0 ? ((int) (b)) : b + 256;
83          block += neuter << 8 * (2 - i);
84       }
85
86       char base64[] = new char[4];
87
88       for (int i = 0; i < 4; i++) {
89          int sixbit = block >>> 6 * (3 - i) & 0x3f;
90          base64[i] = getChar(sixbit);
91       }
92
93       if (slack < 1) {
94          base64[2] = '=';
95       }
96
97       if (slack < 2) {
98          base64[3] = '=';
99       }
100
101       return base64;
102    }
103
104    public static byte[] decode(String JavaDoc base64) {
105       int pad = 0;
106
107       for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
108          pad++;
109       }
110
111       int length = (base64.length() * 6) / 8 - pad;
112       byte raw[] = new byte[length];
113       int rawindex = 0;
114
115       for (int i = 0; i < base64.length(); i += 4) {
116          int block = (getValue(base64.charAt(i)) << 18) +
117                   (getValue(base64.charAt(i + 1)) << 12) +
118                   (getValue(base64.charAt(i + 2)) << 6) +
119                   getValue(base64.charAt(i + 3));
120
121          for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
122             raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
123          }
124
125          rawindex += 3;
126       }
127
128       return raw;
129    }
130
131 }
132
133
Popular Tags