01: /* ===========================================================================
02: * $RCSfile: Tools.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
08: *
09: * This program can be redistributed and/or modified under the terms of the
10: * Version 2 of the GNU General Public License as published by the Free
11: * Software Foundation.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: */
19:
20: package COM.rl.util;
21:
22: import java.io.*;
23: import java.util.*;
24:
25: /**
26: * A Tools class containing generally useful, miscellaneous static methods.
27: *
28: * @author Mark Welsh
29: */
30: public class Tools {
31: // Constants -------------------------------------------------------------
32:
33: // Fields ----------------------------------------------------------------
34:
35: // Class Methods ---------------------------------------------------------
36: /**
37: * Is the string one of the ones in the array?
38: */
39: public static boolean isInArray(String s, String[] list) {
40: for (int i = 0; i < list.length; i++)
41: if (s.equals(list[i]))
42: return true;
43: return false;
44: }
45:
46: /** Encode a byte[] as a Base64 (see RFC1521, Section 5.2) String. */
47: private static final char[] base64 = { 'A', 'B', 'C', 'D', 'E',
48: 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
49: 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
50: 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
51: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
52: '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
53: private static final char pad = '=';
54:
55: public static String toBase64(byte[] b) {
56: StringBuffer sb = new StringBuffer();
57: for (int ptr = 0; ptr < b.length; ptr += 3) {
58: sb.append(base64[(b[ptr] >> 2) & 0x3F]);
59: if (ptr + 1 < b.length) {
60: sb.append(base64[((b[ptr] << 4) & 0x30)
61: | ((b[ptr + 1] >> 4) & 0x0F)]);
62: if (ptr + 2 < b.length) {
63: sb.append(base64[((b[ptr + 1] << 2) & 0x3C)
64: | ((b[ptr + 2] >> 6) & 0x03)]);
65: sb.append(base64[b[ptr + 2] & 0x3F]);
66: } else {
67: sb.append(base64[(b[ptr + 1] << 2) & 0x3C]);
68: sb.append(pad);
69: }
70: } else {
71: sb.append(base64[((b[ptr] << 4) & 0x30)]);
72: sb.append(pad);
73: sb.append(pad);
74: }
75: }
76: return sb.toString();
77: }
78: }
|