KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > yworks > yguard > obf > Tools


1 /* ===========================================================================
2  * $RCSfile: Tools.java,v $
3  * ===========================================================================
4  *
5  * RetroGuard -- an obfuscation package for Java classfiles.
6  *
7  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * The author may be contacted at markw@retrologic.com
24  *
25  *
26  * $Date: 2002/10/16 08:14:35 $
27  * $Revision: 1.1.1.1 $
28  */

29 package com.yworks.yguard.obf;
30
31 import java.io.*;
32 import java.util.*;
33
34 /**
35  * A Tools class containing generally useful, miscellaneous static methods.
36  *
37  * @author Mark Welsh
38  */

39 public class Tools
40 {
41     // Constants -------------------------------------------------------------
42

43
44     // Fields ----------------------------------------------------------------
45

46
47     // Class Methods ---------------------------------------------------------
48
/**
49      * Is the string one of the ones in the array?
50      */

51     public static boolean isInArray(String JavaDoc s, String JavaDoc[] list)
52     {
53         for (int i = 0; i < list.length; i++) if (s.equals(list[i])) return true;
54         return false;
55     }
56
57     /** Encode a byte[] as a Base64 (see RFC1521, Section 5.2) String. */
58     private static final char[] base64 = {
59         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
60         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
61         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
62         'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
63     private static final char pad = '=';
64     public static String JavaDoc toBase64(byte[] b)
65     {
66         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67         for (int ptr = 0; ptr < b.length; ptr += 3)
68         {
69             sb.append(base64[(b[ptr] >> 2) & 0x3F]);
70             if (ptr + 1 < b.length)
71             {
72                 sb.append(base64[((b[ptr] << 4) & 0x30) | ((b[ptr + 1] >> 4) & 0x0F)]);
73                 if (ptr + 2 < b.length)
74                 {
75                     sb.append(base64[((b[ptr + 1] << 2) & 0x3C) | ((b[ptr + 2] >> 6) & 0x03)]);
76                     sb.append(base64[b[ptr + 2] & 0x3F]);
77                 }
78                 else
79                 {
80                     sb.append(base64[(b[ptr + 1] << 2) & 0x3C]);
81                     sb.append(pad);
82                 }
83             }
84             else
85             {
86                 sb.append(base64[((b[ptr] << 4) & 0x30)]);
87                 sb.append(pad);
88                 sb.append(pad);
89             }
90         }
91         return sb.toString();
92     }
93 }
94
Popular Tags