001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.cldchi.tools.memoryprofiler.jdwp;
028:
029: import java.util.Vector;
030: import java.util.StringTokenizer;
031:
032: /**
033: * This class contains some auxillary functions that are used by
034: * classes of <code>jdwp</code> package.
035: */
036: class Tools {
037:
038: /**
039: * Returns a hex representation of <code>long</code> value with
040: * leading zeros.
041: *
042: * @param b an integer value
043: * @param length a size of the integer value (2 for <code>short</code>,
044: * 4 for <code>int</code>, 8 for <code>long</code>)
045: * @return a hex representation of <code>long</code> value with
046: * leading zeros
047: */
048: public static String Hex(long b, int length) {
049: return Right(Long.toHexString(b), length).replace(' ', '0');
050: }
051:
052: /**
053: * Addes the specified substring to the beginning of the specified string
054: * a few times until the length of the string becomes not less than the
055: * specified length. If the length of the string is already more than
056: * the specified length the resulting value if a string of asterisks with
057: * the specified length.
058: *
059: * @param source a source string to align
060: * @param length a desired length of the result string
061: * @param what a substring that must be appended to the beginning of the
062: * string
063: * @return an aligned string
064: */
065: public static String PadL(String source, int length, String what) {
066:
067: if (length <= 0)
068: return "";
069:
070: if (source.length() > length)
071: return PadL("", length, "*");
072:
073: while (source.length() < length)
074: source = what + source;
075:
076: return source;
077: }
078:
079: /**
080: * Addes spaces to the beginning of the specified string
081: * a few times until the length of the string becomes not less than the
082: * specified length. If the length of the string is already more than
083: * the specified length the resulting value if a string of asterisks with
084: * the specified length.
085: *
086: * @param source a source string to align
087: * @param length a desired length of the result string
088: * @return an aligned string
089: */
090: public static String PadL(String source, int length) {
091: return PadL(source, length, " ");
092: }
093:
094: /**
095: * Appends the specified substring to the end of the specified string
096: * a few times until the length of the string becomes not less than the
097: * specified length. If the length of the string is already more than
098: * the specified length the resulting value if a string of asterisks with
099: * the specified length.
100: *
101: * @param source a source string to align
102: * @param length a desired length of the result string
103: * @param what a substring that must be appended to the end of the
104: * string
105: * @return an aligned string
106: */
107: public static String PadR(String source, int length, String what) {
108:
109: if (length <= 0)
110: return "";
111:
112: if (source.length() > length)
113: return PadR("", length, "*");
114:
115: while (source.length() < length)
116: source = source + what;
117:
118: return source;
119: }
120:
121: /**
122: * Appends spaces to the end of the specified string
123: * a few times until the length of the string becomes not less than the
124: * specified length. If the length of the string is already more than
125: * the specified length the resulting value if a string of asterisks with
126: * the specified length.
127: *
128: * @param source a source string to align
129: * @param length a desired length of the result string
130: * @return an aligned string
131: */
132: public static String PadR(String source, int length) {
133: return PadR(source, length, " ");
134: }
135:
136: /**
137: * Truncates or increases the specified string to the specified
138: * length. If the source string is less that the desired length the
139: * necessary number of spaces is added to the end of the string.
140: * If the source string is more
141: * than the desired length the necessary number of last characters is
142: * removed.
143: *
144: * @param source a sring to be aligned
145: * @param length a desired length of the string
146: * @return an aligned string
147: */
148: public static String Left(String source, int length) {
149:
150: if (length <= 0)
151: return "";
152:
153: if (length <= source.length())
154: return source.substring(0, length);
155: else
156: return PadR(source, length);
157: }
158:
159: /**
160: * Truncates or increases the specified string to the specified
161: * length. If the source string is less that the desired length the
162: * necessary number of spaces is added to the beginning of the string.
163: * If the source string is more
164: * than the desired length the necessary number of first characters is
165: * removed.
166: *
167: * @param source a sring to be aligned
168: * @param length a desired length of the string
169: * @return an aligned string
170: */
171: public static String Right(String source, int length) {
172:
173: if (length <= 0)
174: return "";
175:
176: if (length <= source.length())
177: return source.substring(source.length() - length, source
178: .length());
179: else
180: return PadL(source, length);
181: }
182:
183: /**
184: * Returns a content of the vector as a string where elements of the vector
185: * are separated by tabs.
186: *
187: * @param v a vector that should be represented as a string
188: * @return a string representation of the vector
189: */
190: public static String listVector(Vector v) {
191: String s = "";
192: for (int i = 0; i < v.size(); i++)
193: s = s + (i + 1) + "\t" + v.elementAt(i) + "\n";
194: return s;
195: }
196:
197: /**
198: * Returns a number of tokens in the string. Token delimiters are
199: * spaces, colons, commas and tab characters.
200: *
201: * @param a string that consist on tokens and delimiteres.
202: * @return a number of tokens in the string
203: */
204: public static int countBytes(String s) {
205: return (new StringTokenizer(s, " :,\t")).countTokens();
206: }
207:
208: /**
209: * Converts a string that is a list of bytes separated by
210: * spaces, colons, commas and tab characters
211: * (for example, <code>01,15,44,135</code>) to byte array.
212: *
213: * @param s a string to be converted
214: * @return an array of bytes that corresponds to the source string
215: */
216: public static byte[] str2bytes(String s) throws Exception {
217:
218: StringTokenizer t = new StringTokenizer(s, " :,\t");
219:
220: byte[] buf = new byte[t.countTokens()];
221:
222: int k = 0;
223: while (t.hasMoreTokens())
224: buf[k++] = Integer.decode(t.nextToken()).byteValue();
225:
226: return buf;
227: }
228:
229: /**
230: * Suspends the current thread for the specified number of
231: * milliseconds.
232: *
233: * @param delay suspend time in milliseconds
234: */
235: public static void wait(int delay) {
236: try {
237: Thread.sleep(delay);
238: } catch (Exception e) {
239: }
240: }
241: }
|