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
.lang
.reflect
.Array
;
42 import java
.lang
.reflect
.InvocationTargetException
;
43 import java
.lang
.reflect
.Method
;
44 import java
.math
.BigInteger
;
46 import java
.security
.AccessController
;
47 import java
.security
.PrivilegedAction
;
48 import java
.security
.Security
;
51 * A collection of useful class methods.
53 * @author Casey Marshall (rsdio@metastatic.org)
59 // -------------------------------------------------------------------------
61 static final String HEX
= "0123456789abcdef";
63 // Static methods only.
67 // -------------------------------------------------------------------------
70 * Convert a hexadecimal string into its byte representation.
72 * @param hex The hexadecimal string.
73 * @return The converted bytes.
75 static byte[] toByteArray(String hex
)
77 hex
= hex
.toLowerCase();
78 byte[] buf
= new byte[hex
.length() / 2];
80 for (int i
= 0; i
< buf
.length
; i
++)
82 buf
[i
] = (byte) ((Character
.digit(hex
.charAt(j
++), 16) << 4) |
83 Character
.digit(hex
.charAt(j
++), 16));
89 * Convert a byte array to a hexadecimal string, as though it were a
90 * big-endian arbitrarily-sized integer.
92 * @param buf The bytes to format.
93 * @param off The offset to start at.
94 * @param len The number of bytes to format.
95 * @return A hexadecimal representation of the specified bytes.
97 static String
toHexString(byte[] buf
, int off
, int len
)
99 StringBuffer str
= new StringBuffer();
100 for (int i
= 0; i
< len
; i
++)
102 str
.append(HEX
.charAt(buf
[i
+off
] >>> 4 & 0x0F));
103 str
.append(HEX
.charAt(buf
[i
+off
] & 0x0F));
105 return str
.toString();
109 * See {@link #toHexString(byte[],int,int)}.
111 static String
toHexString(byte[] buf
)
113 return Util
.toHexString(buf
, 0, buf
.length
);
117 * Convert a byte array to a hexadecimal string, separating octets
118 * with the given character.
120 * @param buf The bytes to format.
121 * @param off The offset to start at.
122 * @param len The number of bytes to format.
123 * @param sep The character to insert between octets.
124 * @return A hexadecimal representation of the specified bytes.
126 static String
toHexString(byte[] buf
, int off
, int len
, char sep
)
128 StringBuffer str
= new StringBuffer();
129 for (int i
= 0; i
< len
; i
++)
131 str
.append(HEX
.charAt(buf
[i
+off
] >>> 4 & 0x0F));
132 str
.append(HEX
.charAt(buf
[i
+off
] & 0x0F));
136 return str
.toString();
140 * See {@link #toHexString(byte[],int,int,char)}.
142 static String
toHexString(byte[] buf
, char sep
)
144 return Util
.toHexString(buf
, 0, buf
.length
, sep
);
148 * Create a representation of the given byte array similar to the
149 * output of <code>`hexdump -C'</code>, which is
151 * <p><pre>OFFSET SIXTEEN-BYTES-IN-HEX PRINTABLE-BYTES</pre>
153 * <p>The printable bytes show up as-is if they are printable and
154 * not a newline character, otherwise showing as '.'.
156 * @param buf The bytes to format.
157 * @param off The offset to start at.
158 * @param len The number of bytes to encode.
159 * @param prefix A string to prepend to every line.
160 * @return The formatted string.
162 static String
hexDump(byte[] buf
, int off
, int len
, String prefix
)
164 String nl
= getProperty("line.separator");
165 StringBuffer str
= new StringBuffer();
171 str
.append(Util
.formatInt(i
+off
, 16, 8));
173 String s
= Util
.toHexString(buf
, i
+off
, Math
.min(16, len
-i
), ' ');
175 for (int j
= 56 - (56 - s
.length()); j
< 56; j
++)
177 for (int j
= 0; j
< Math
.min(16, len
- i
); j
++)
179 if ((buf
[i
+off
+j
] & 0xFF) < 0x20 || (buf
[i
+off
+j
] & 0xFF) > 0x7E)
182 str
.append((char) (buf
[i
+off
+j
] & 0xFF));
187 return str
.toString();
191 * See {@link #hexDump(byte[],int,int,String)}.
193 static String
hexDump(byte[] buf
, int off
, int len
)
195 return hexDump(buf
, off
, len
, "");
199 * See {@link #hexDump(byte[],int,int,String)}.
201 static String
hexDump(byte[] buf
, String prefix
)
203 return hexDump(buf
, 0, buf
.length
, prefix
);
207 * See {@link #hexDump(byte[],int,int,String)}.
209 static String
hexDump(byte[] buf
)
211 return hexDump(buf
, 0, buf
.length
);
215 * Format an integer into the specified radix, zero-filled.
217 * @param i The integer to format.
218 * @param radix The radix to encode to.
219 * @param len The target length of the string. The string is
220 * zero-padded to this length, but may be longer.
221 * @return The formatted integer.
223 static String
formatInt(int i
, int radix
, int len
)
225 String s
= Integer
.toString(i
, radix
);
226 StringBuffer buf
= new StringBuffer();
227 for (int j
= 0; j
< len
- s
.length(); j
++)
230 return buf
.toString();
234 * Concatenate two byte arrays into one.
236 * @param b1 The first byte array.
237 * @param b2 The second byte array.
238 * @return The concatenation of b1 and b2.
240 static byte[] concat(byte[] b1
, byte[] b2
)
242 byte[] b3
= new byte[b1
.length
+b2
.length
];
243 System
.arraycopy(b1
, 0, b3
, 0, b1
.length
);
244 System
.arraycopy(b2
, 0, b3
, b1
.length
, b2
.length
);
249 * See {@link #trim(byte[],int,int)}.
251 static byte[] trim(byte[] buffer
, int len
)
253 return trim(buffer
, 0, len
);
257 * Returns a portion of a byte array, possibly zero-filled.
259 * @param buffer The byte array to trim.
260 * @param off The offset to begin reading at.
261 * @param len The number of bytes to return. This value can be larger
262 * than <i>buffer.length - off</i>, in which case the rest of the
263 * returned byte array will be filled with zeros.
264 * @throws IndexOutOfBoundsException If <i>off</i> or <i>len</i> is
265 * negative, or if <i>off</i> is larger than the byte array's
267 * @return The trimmed byte array.
269 static byte[] trim(byte[] buffer
, int off
, int len
)
271 if (off
< 0 || len
< 0 || off
> buffer
.length
)
272 throw new IndexOutOfBoundsException("max=" + buffer
.length
+
273 " off=" + off
+ " len=" + len
);
274 if (off
== 0 && len
== buffer
.length
)
276 byte[] b
= new byte[len
];
277 System
.arraycopy(buffer
, off
, b
, 0, Math
.min(len
, buffer
.length
- off
));
282 * Returns the byte array representation of the given big integer with
283 * the leading zero byte (if any) trimmed off.
285 * @param bi The integer to trim.
286 * @return The byte representation of the big integer, with any leading
289 static byte[] trim(BigInteger bi
)
291 byte[] buf
= bi
.toByteArray();
292 if (buf
[0] == 0x00 && !bi
.equals(BigInteger
.ZERO
))
294 return trim(buf
, 1, buf
.length
- 1);
303 * Returns the integer value of <code>{@link
304 * java.lang.System#currentTimeMillis()} / 1000</code>.
306 * @return The current time, in seconds.
308 static int unixTime()
310 return (int) (System
.currentTimeMillis() / 1000L);
314 * Transform an Object array into another by calling the given method
315 * on each object. The returned object array will have the runtime
316 * type of <i>returnType</i>. For example, the following will transform
317 * array of objects into their String representations, returning a String
318 * array. For example:
320 * <blockquote><p><code>
321 * String[] strings = (String[]) Util.transform(array, String.class,
323 * </code></p></blockquote>
325 * <p>If any element of the given array is <tt>null</tt>, then that
326 * entry in the returned array will also be <tt>null</tt>.
328 * @param array The array to transform. It does not need to be of
330 * @param returnType The desired return type of the returned array.
331 * This must by the <i>component</i> type, not the array type.
332 * @param method The name of the method to invoke from each object.
333 * @param args The arguments to pass to the method, or <tt>null</tt>
334 * if the method takes no arguments.
335 * @throws InvocationTargetException If an exception occurs while
336 * calling <i>method</i> of any object.
337 * @throws NoSuchMethodException If <i>method</i> is not the name of
338 * a valid method of any component of the array.
339 * @throws ClassCastException If the returned object from the method
340 * is not assignable to the return type.
341 * @throws IllegalArgumentException If <i>args</i> is not appropriate
343 * @throws IllegalAccessException If <i>method</i> is not accessible.
344 * @throws SecurityException If <i>method</i> is not accessible.
345 * @return An array containing the output of <i>method</i> called on
346 * each element of <i>array</i> with <i>args</i>. The return type
347 * of the array will be an array of <i>returnType</i>.
349 static Object
[] transform(Object
[] array
, Class returnType
,
350 String method
, Object
[] args
)
351 throws InvocationTargetException
, NoSuchMethodException
,
352 IllegalAccessException
355 args
= new Object
[0];
356 Object
[] result
= (Object
[]) Array
.newInstance(returnType
, array
.length
);
357 Class
[] argsClasses
= new Class
[args
.length
];
358 for (int i
= 0; i
< args
.length
; i
++)
360 argsClasses
[i
] = args
[i
].getClass();
362 for (int i
= 0; i
< array
.length
; i
++)
364 if (array
[i
] == null)
369 Class objClass
= array
[i
].getClass();
370 Method objMethod
= objClass
.getMethod(method
, argsClasses
);
371 Object o
= objMethod
.invoke(array
[i
], args
);
372 if (!returnType
.isAssignableFrom(o
.getClass()))
373 throw new ClassCastException();
380 * Get a system property as a privileged action.
382 * @param name The name of the property to get.
383 * @return The property named <i>name</i>, or null if the property is
385 * @throws SecurityException If the Jessie code still does not have
386 * permission to read the property.
388 static String
getProperty(final String name
)
390 return (String
) AccessController
.doPrivileged(
391 new PrivilegedAction()
395 return System
.getProperty(name
);
402 * Get a security property as a privileged action.
404 * @param name The name of the property to get.
405 * @return The property named <i>name</i>, or null if the property is
407 * @throws SecurityException If the Jessie code still does not have
408 * permission to read the property.
410 static String
getSecurityProperty(final String name
)
412 return (String
) AccessController
.doPrivileged(
413 new PrivilegedAction()
417 return Security
.getProperty(name
);