2 import java.io.UnsupportedEncodingException;
5 static String base64alphabet =
6 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
7 static byte[] revBase64;
9 revBase64 =
new byte[
'z'+1];
10 for (
int i = 0; i < 64; i++) {
11 revBase64[base64alphabet.charAt(i)] = (byte) i;
15 public static String base64encode(String str) {
18 utf8 = str.getBytes(
"UTF-8");
19 }
catch (UnsupportedEncodingException ex) {
20 throw new InternalError(ex.toString());
22 StringBuffer sb =
new StringBuffer((utf8.length+2)/3 * 4);
24 int len = utf8.length;
25 for (i = 0; i < len - 2; i += 3) {
26 int value = (utf8[i ] & 0xff) << 16
27 | (utf8[i+1] & 0xff) << 8
29 sb.append(base64alphabet.charAt(value >> 18))
30 .append(base64alphabet.charAt((value >> 12) & 0x3f))
31 .append(base64alphabet.charAt((value >> 6) & 0x3f))
32 .append(base64alphabet.charAt(value & 0x3f));
35 int value = utf8[i++] & 0xff;
36 sb.append(base64alphabet.charAt(value >> 2));
38 value = (value << 8) + (utf8[i++] & 0xff);
39 sb.append(base64alphabet.charAt((value >> 4) & 0x3f));
40 sb.append(base64alphabet.charAt((value << 2) & 0x3f));
42 sb.append(base64alphabet.charAt((value << 4) & 0x3f));
50 public static String base64decode(String str) {
51 int len = str.length();
53 throw new IllegalArgumentException(
"Not Base64: "+str);
54 int padstart = str.indexOf(
'=', len - 3);
55 int padding = padstart == -1 ? 0 : len - padstart;
56 byte[] utf8 =
new byte[len / 4 * 3];
58 for (
int i = 0; i < len / 4; i++) {
59 int value = (revBase64[str.charAt(4*i)] << 18)
60 + (revBase64[str.charAt(4*i+1)] << 12)
61 + (revBase64[str.charAt(4*i+2)] << 6)
62 + revBase64[str.charAt(4*i+3)];
63 utf8[3*i+0] = (byte) (value >> 16);
64 utf8[3*i+1] = (byte) (value >> 8);
65 utf8[3*i+2] = (byte) (value );
68 return new String(utf8, 0, utf8.length - padding,
"UTF-8");
69 }
catch (UnsupportedEncodingException ex) {
70 throw new InternalError(ex.toString());
77 public static String
quote(String str) {
78 StringBuffer result =
new StringBuffer(
"\"");
79 for (
int i=0; i< str.length(); i++) {
81 switch (c = str.charAt(i)) {
86 result.append(
"\\\\");
89 result.append(
"\\\"");
92 result.append(str.charAt(i));
95 return result.append(
"\"").toString();
98 public static String nodeToString(
BaseNode node) {
99 return node.getType() +
"{" +
"}";
static String quote(String str)
This is the static method, that quotes a string.