1 /* Util.java -- Miscellaneous utility methods.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.net.ssl.provider;
41 import java.io.PrintWriter;
42 import java.io.StringWriter;
44 import java.lang.reflect.Array;
45 import java.lang.reflect.InvocationTargetException;
46 import java.lang.reflect.Method;
47 import java.math.BigInteger;
49 import java.nio.ByteBuffer;
51 import java.security.AccessController;
52 import java.security.PrivilegedAction;
53 import java.security.Security;
56 * A collection of useful class methods.
58 * @author Casey Marshall (rsdio@metastatic.org)
60 public final class Util
64 // -------------------------------------------------------------------------
66 static final String HEX = "0123456789abcdef";
68 // Static methods only.
72 // -------------------------------------------------------------------------
74 public static Object wrapBuffer(ByteBuffer buffer)
76 return wrapBuffer(buffer, "");
79 public static Object wrapBuffer(ByteBuffer buffer, String prefix)
81 return new WrappedBuffer(buffer, prefix);
84 private static class WrappedBuffer
86 private final ByteBuffer buffer;
87 private final String prefix;
89 WrappedBuffer(ByteBuffer buffer, String prefix)
95 public String toString()
97 return hexDump(buffer, prefix);
102 * Convert a hexadecimal string into its byte representation.
104 * @param hex The hexadecimal string.
105 * @return The converted bytes.
107 public static byte[] toByteArray(String hex)
109 hex = hex.toLowerCase();
110 byte[] buf = new byte[hex.length() / 2];
112 for (int i = 0; i < buf.length; i++)
114 buf[i] = (byte) ((Character.digit(hex.charAt(j++), 16) << 4) |
115 Character.digit(hex.charAt(j++), 16));
121 * Convert a byte array to a hexadecimal string, as though it were a
122 * big-endian arbitrarily-sized integer.
124 * @param buf The bytes to format.
125 * @param off The offset to start at.
126 * @param len The number of bytes to format.
127 * @return A hexadecimal representation of the specified bytes.
129 public static String toHexString(byte[] buf, int off, int len)
131 StringBuffer str = new StringBuffer();
132 for (int i = 0; i < len; i++)
134 str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
135 str.append(HEX.charAt(buf[i+off] & 0x0F));
137 return str.toString();
141 * See {@link #toHexString(byte[],int,int)}.
143 public static String toHexString(byte[] buf)
145 return Util.toHexString(buf, 0, buf.length);
149 * Convert a byte array to a hexadecimal string, separating octets
150 * with the given character.
152 * @param buf The bytes to format.
153 * @param off The offset to start at.
154 * @param len The number of bytes to format.
155 * @param sep The character to insert between octets.
156 * @return A hexadecimal representation of the specified bytes.
158 public static String toHexString(byte[] buf, int off, int len, char sep)
160 StringBuffer str = new StringBuffer();
161 for (int i = 0; i < len; i++)
163 str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
164 str.append(HEX.charAt(buf[i+off] & 0x0F));
168 return str.toString();
172 * See {@link #toHexString(byte[],int,int,char)}.
174 public static String toHexString(byte[] buf, char sep)
176 return Util.toHexString(buf, 0, buf.length, sep);
180 * Create a representation of the given byte array similar to the
181 * output of <code>`hexdump -C'</code>, which is
183 * <p><pre>OFFSET SIXTEEN-BYTES-IN-HEX PRINTABLE-BYTES</pre>
185 * <p>The printable bytes show up as-is if they are printable and
186 * not a newline character, otherwise showing as '.'.
188 * @param buf The bytes to format.
189 * @param off The offset to start at.
190 * @param len The number of bytes to encode.
191 * @param prefix A string to prepend to every line.
192 * @return The formatted string.
194 public static String hexDump(byte[] buf, int off, int len, String prefix)
196 String nl = getProperty("line.separator");
197 StringBuffer str = new StringBuffer();
203 str.append(Util.formatInt(i+off, 16, 8));
205 String s = Util.toHexString(buf, i+off, Math.min(16, len-i), ' ');
207 for (int j = s.length(); j < 49; j++)
209 for (int j = 0; j < Math.min(16, len - i); j++)
211 if ((buf[i+off+j] & 0xFF) < 0x20 || (buf[i+off+j] & 0xFF) > 0x7E)
214 str.append((char) (buf[i+off+j] & 0xFF));
219 return str.toString();
222 public static String hexDump (ByteBuffer buf)
224 return hexDump (buf, null);
227 public static String hexDump (ByteBuffer buf, String prefix)
229 buf = buf.duplicate();
230 StringWriter str = new StringWriter ();
231 PrintWriter out = new PrintWriter (str);
233 int len = buf.remaining();
234 byte[] line = new byte[16];
239 out.print(Util.formatInt (i, 16, 8));
241 int l = Math.min(16, len - i);
243 String s = Util.toHexString(line, 0, l, ' ');
245 for (int j = s.length(); j < 49; j++)
247 for (int j = 0; j < l; j++)
249 int c = line[j] & 0xFF;
250 if (c < 0x20 || c > 0x7E)
258 return str.toString();
262 * See {@link #hexDump(byte[],int,int,String)}.
264 public static String hexDump(byte[] buf, int off, int len)
266 return hexDump(buf, off, len, "");
270 * See {@link #hexDump(byte[],int,int,String)}.
272 public static String hexDump(byte[] buf, String prefix)
274 return hexDump(buf, 0, buf.length, prefix);
278 * See {@link #hexDump(byte[],int,int,String)}.
280 public static String hexDump(byte[] buf)
282 return hexDump(buf, 0, buf.length);
286 * Format an integer into the specified radix, zero-filled.
288 * @param i The integer to format.
289 * @param radix The radix to encode to.
290 * @param len The target length of the string. The string is
291 * zero-padded to this length, but may be longer.
292 * @return The formatted integer.
294 public static String formatInt(int i, int radix, int len)
296 String s = Integer.toString(i, radix);
297 StringBuffer buf = new StringBuffer();
298 for (int j = 0; j < len - s.length(); j++)
301 return buf.toString();
305 * Concatenate two byte arrays into one.
307 * @param b1 The first byte array.
308 * @param b2 The second byte array.
309 * @return The concatenation of b1 and b2.
311 public static byte[] concat(byte[] b1, byte[] b2)
313 byte[] b3 = new byte[b1.length+b2.length];
314 System.arraycopy(b1, 0, b3, 0, b1.length);
315 System.arraycopy(b2, 0, b3, b1.length, b2.length);
320 * See {@link #trim(byte[],int,int)}.
322 public static byte[] trim(byte[] buffer, int len)
324 return trim(buffer, 0, len);
328 * Returns a portion of a byte array, possibly zero-filled.
330 * @param buffer The byte array to trim.
331 * @param off The offset to begin reading at.
332 * @param len The number of bytes to return. This value can be larger
333 * than <i>buffer.length - off</i>, in which case the rest of the
334 * returned byte array will be filled with zeros.
335 * @throws IndexOutOfBoundsException If <i>off</i> or <i>len</i> is
336 * negative, or if <i>off</i> is larger than the byte array's
338 * @return The trimmed byte array.
340 public static byte[] trim(byte[] buffer, int off, int len)
342 if (off < 0 || len < 0 || off > buffer.length)
343 throw new IndexOutOfBoundsException("max=" + buffer.length +
344 " off=" + off + " len=" + len);
345 if (off == 0 && len == buffer.length)
347 byte[] b = new byte[len];
348 System.arraycopy(buffer, off, b, 0, Math.min(len, buffer.length - off));
353 * Returns the byte array representation of the given big integer with
354 * the leading zero byte (if any) trimmed off.
356 * @param bi The integer to trim.
357 * @return The byte representation of the big integer, with any leading
360 public static byte[] trim(BigInteger bi)
362 byte[] buf = bi.toByteArray();
363 if (buf[0] == 0x00 && !bi.equals(BigInteger.ZERO))
365 return trim(buf, 1, buf.length - 1);
374 * Returns the integer value of <code>{@link
375 * java.lang.System#currentTimeMillis()} / 1000</code>.
377 * @return The current time, in seconds.
379 public static int unixTime()
381 return (int) (System.currentTimeMillis() / 1000L);
385 * Transform an Object array into another by calling the given method
386 * on each object. The returned object array will have the runtime
387 * type of <i>returnType</i>. For example, the following will transform
388 * array of objects into their String representations, returning a String
389 * array. For example:
391 * <blockquote><p><code>
392 * String[] strings = (String[]) Util.transform(array, String.class,
394 * </code></p></blockquote>
396 * <p>If any element of the given array is <tt>null</tt>, then that
397 * entry in the returned array will also be <tt>null</tt>.
399 * @param array The array to transform. It does not need to be of
401 * @param returnType The desired return type of the returned array.
402 * This must by the <i>component</i> type, not the array type.
403 * @param method The name of the method to invoke from each object.
404 * @param args The arguments to pass to the method, or <tt>null</tt>
405 * if the method takes no arguments.
406 * @throws InvocationTargetException If an exception occurs while
407 * calling <i>method</i> of any object.
408 * @throws NoSuchMethodException If <i>method</i> is not the name of
409 * a valid method of any component of the array.
410 * @throws ClassCastException If the returned object from the method
411 * is not assignable to the return type.
412 * @throws IllegalArgumentException If <i>args</i> is not appropriate
414 * @throws IllegalAccessException If <i>method</i> is not accessible.
415 * @throws SecurityException If <i>method</i> is not accessible.
416 * @return An array containing the output of <i>method</i> called on
417 * each element of <i>array</i> with <i>args</i>. The return type
418 * of the array will be an array of <i>returnType</i>.
420 static Object[] transform(Object[] array, Class returnType,
421 String method, Object[] args)
422 throws InvocationTargetException, NoSuchMethodException,
423 IllegalAccessException
426 args = new Object[0];
427 Object[] result = (Object[]) Array.newInstance(returnType, array.length);
428 Class[] argsClasses = new Class[args.length];
429 for (int i = 0; i < args.length; i++)
431 argsClasses[i] = args[i].getClass();
433 for (int i = 0; i < array.length; i++)
435 if (array[i] == null)
440 Class objClass = array[i].getClass();
441 Method objMethod = objClass.getMethod(method, argsClasses);
442 Object o = objMethod.invoke(array[i], args);
443 if (!returnType.isAssignableFrom(o.getClass()))
444 throw new ClassCastException();
451 * Get a system property as a privileged action.
453 * @param name The name of the property to get.
454 * @return The property named <i>name</i>, or null if the property is
456 * @throws SecurityException If the Jessie code still does not have
457 * permission to read the property.
459 @Deprecated static String getProperty(final String name)
461 return (String) AccessController.doPrivileged(
462 new PrivilegedAction()
466 return System.getProperty(name);
473 * Get a security property as a privileged action.
475 * @param name The name of the property to get.
476 * @return The property named <i>name</i>, or null if the property is
478 * @throws SecurityException If the Jessie code still does not have
479 * permission to read the property.
481 @Deprecated static String getSecurityProperty(final String name)
483 return (String) AccessController.doPrivileged(
484 new PrivilegedAction()
488 return Security.getProperty(name);