00001
00024 package org.objectweb.jonas.security.realm.lib;
00025
00036 public class Base64 {
00037
00041 private static char[] alphabet =
00042 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
00043 .toCharArray();
00044
00048 private static byte[] codes = new byte[256];
00049
00050 static {
00051 for (int i = 0; i < 256; i++) {
00052 codes[i] = -1;
00053 }
00054 for (int i = 'A'; i <= 'Z'; i++) {
00055 codes[i] = (byte) (i - 'A');
00056 }
00057 for (int i = 'a'; i <= 'z'; i++) {
00058 codes[i] = (byte) (26 + i - 'a');
00059 }
00060 for (int i = '0'; i <= '9'; i++) {
00061 codes[i] = (byte) (52 + i - '0');
00062 }
00063 codes['+'] = 62;
00064 codes['/'] = 63;
00065 }
00066
00067
00068
00076 public static char[] encode(byte[] data) {
00077 char[] out = new char[((data.length + 2) / 3) * 4];
00078
00079
00080
00081
00082
00083 for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
00084 boolean quad = false;
00085 boolean trip = false;
00086
00087 int val = (0xFF & (int) data[i]);
00088 val <<= 8;
00089 if ((i + 1) < data.length) {
00090 val |= (0xFF & (int) data[i + 1]);
00091 trip = true;
00092 }
00093 val <<= 8;
00094 if ((i + 2) < data.length) {
00095 val |= (0xFF & (int) data[i + 2]);
00096 quad = true;
00097 }
00098 out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
00099 val >>= 6;
00100 out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
00101 val >>= 6;
00102 out[index + 1] = alphabet[val & 0x3F];
00103 val >>= 6;
00104 out[index + 0] = alphabet[val & 0x3F];
00105 }
00106 return out;
00107 }
00108
00122 public static byte[] decode(char[] data) {
00123
00124
00125
00126
00127
00128
00129
00130 int tempLen = data.length;
00131 for (int ix = 0; ix < data.length; ix++) {
00132 if ((data[ix] > 255) || codes[ data[ix] ] < 0) {
00133 --tempLen;
00134 }
00135 }
00136
00137
00138
00139
00140
00141 int len = (tempLen / 4) * 3;
00142 if ((tempLen % 4) == 3) {
00143 len += 2;
00144 }
00145 if ((tempLen % 4) == 2) {
00146 len += 1;
00147 }
00148
00149 byte[] out = new byte[len];
00150
00151
00152
00153 int shift = 0;
00154 int accum = 0;
00155 int index = 0;
00156
00157
00158 for (int ix = 0; ix < data.length; ix++) {
00159 int value = (data[ix] > 255) ? -1 : codes[ data[ix] ];
00160
00161 if (value >= 0) {
00162 accum <<= 6;
00163 shift += 6;
00164 accum |= value;
00165 if (shift >= 8) {
00166 shift -= 8;
00167 out[index++] =
00168 (byte) ((accum >> shift) & 0xff);
00169 }
00170 }
00171
00172
00173
00174
00175
00176
00177 }
00178
00179
00180 if (index != out.length) {
00181 throw new Error("Miscalculated data length (wrote " + index + " instead of " + out.length + ")");
00182 }
00183
00184 return out;
00185 }
00186
00187
00188
00189 }