Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / lang / System.java
blobb538b795b95a42b9b9594747808c4fe4357a0a91
1 /* System.java -- useful methods to interface with the system
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.lang;
42 import gnu.classpath.SystemProperties;
43 import gnu.classpath.VMStackWalker;
45 import java.io.InputStream;
46 import java.io.PrintStream;
47 import java.util.Properties;
48 import java.util.PropertyPermission;
50 /**
51 * System represents system-wide resources; things that represent the
52 * general environment. As such, all methods are static.
54 * @author John Keiser
55 * @author Eric Blake (ebb9@email.byu.edu)
56 * @since 1.0
57 * @status still missing 1.4 functionality
59 public final class System
61 // WARNING: System is a CORE class in the bootstrap cycle. See the comments
62 // in vm/reference/java/lang/Runtime for implications of this fact.
64 /**
65 * The standard InputStream. This is assigned at startup and starts its
66 * life perfectly valid. Although it is marked final, you can change it
67 * using {@link #setIn(InputStream)} through some hefty VM magic.
69 * <p>This corresponds to the C stdin and C++ cin variables, which
70 * typically input from the keyboard, but may be used to pipe input from
71 * other processes or files. That should all be transparent to you,
72 * however.
74 public static final InputStream in = VMSystem.makeStandardInputStream();
76 /**
77 * The standard output PrintStream. This is assigned at startup and
78 * starts its life perfectly valid. Although it is marked final, you can
79 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
81 * <p>This corresponds to the C stdout and C++ cout variables, which
82 * typically output normal messages to the screen, but may be used to pipe
83 * output to other processes or files. That should all be transparent to
84 * you, however.
86 public static final PrintStream out = VMSystem.makeStandardOutputStream();
88 /**
89 * The standard output PrintStream. This is assigned at startup and
90 * starts its life perfectly valid. Although it is marked final, you can
91 * change it using {@link #setErr(PrintStream)} through some hefty VM magic.
93 * <p>This corresponds to the C stderr and C++ cerr variables, which
94 * typically output error messages to the screen, but may be used to pipe
95 * output to other processes or files. That should all be transparent to
96 * you, however.
98 public static final PrintStream err = VMSystem.makeStandardErrorStream();
101 * This class is uninstantiable.
103 private System()
108 * Set {@link #in} to a new InputStream. This uses some VM magic to change
109 * a "final" variable, so naturally there is a security check,
110 * <code>RuntimePermission("setIO")</code>.
112 * @param in the new InputStream
113 * @throws SecurityException if permission is denied
114 * @since 1.1
116 public static void setIn(InputStream in)
118 SecurityManager sm = SecurityManager.current; // Be thread-safe.
119 if (sm != null)
120 sm.checkPermission(new RuntimePermission("setIO"));
121 VMSystem.setIn(in);
125 * Set {@link #out} to a new PrintStream. This uses some VM magic to change
126 * a "final" variable, so naturally there is a security check,
127 * <code>RuntimePermission("setIO")</code>.
129 * @param out the new PrintStream
130 * @throws SecurityException if permission is denied
131 * @since 1.1
133 public static void setOut(PrintStream out)
135 SecurityManager sm = SecurityManager.current; // Be thread-safe.
136 if (sm != null)
137 sm.checkPermission(new RuntimePermission("setIO"));
139 VMSystem.setOut(out);
143 * Set {@link #err} to a new PrintStream. This uses some VM magic to change
144 * a "final" variable, so naturally there is a security check,
145 * <code>RuntimePermission("setIO")</code>.
147 * @param err the new PrintStream
148 * @throws SecurityException if permission is denied
149 * @since 1.1
151 public static void setErr(PrintStream err)
153 SecurityManager sm = SecurityManager.current; // Be thread-safe.
154 if (sm != null)
155 sm.checkPermission(new RuntimePermission("setIO"));
156 VMSystem.setErr(err);
160 * Set the current SecurityManager. If a security manager already exists,
161 * then <code>RuntimePermission("setSecurityManager")</code> is checked
162 * first. Since this permission is denied by the default security manager,
163 * setting the security manager is often an irreversible action.
165 * <STRONG>Spec Note:</STRONG> Don't ask me, I didn't write it. It looks
166 * pretty vulnerable; whoever gets to the gate first gets to set the policy.
167 * There is probably some way to set the original security manager as a
168 * command line argument to the VM, but I don't know it.
170 * @param sm the new SecurityManager
171 * @throws SecurityException if permission is denied
173 public static synchronized void setSecurityManager(SecurityManager sm)
175 // Implementation note: the field lives in SecurityManager because of
176 // bootstrap initialization issues. This method is synchronized so that
177 // no other thread changes it to null before this thread makes the change.
178 if (SecurityManager.current != null)
179 SecurityManager.current.checkPermission
180 (new RuntimePermission("setSecurityManager"));
182 // java.security.Security's class initialiser loads and parses the
183 // policy files. If it hasn't been run already it will be run
184 // during the first permission check. That initialisation will
185 // fail if a very restrictive security manager is in force, so we
186 // preload it here.
187 if (SecurityManager.current == null)
191 Class.forName("java.security.Security");
193 catch (ClassNotFoundException e)
198 SecurityManager.current = sm;
202 * Get the current SecurityManager. If the SecurityManager has not been
203 * set yet, then this method returns null.
205 * @return the current SecurityManager, or null
207 public static SecurityManager getSecurityManager()
209 return SecurityManager.current;
213 * Get the current time, measured in the number of milliseconds from the
214 * beginning of Jan. 1, 1970. This is gathered from the system clock, with
215 * any attendant incorrectness (it may be timezone dependent).
217 * @return the current time
218 * @see java.util.Date
220 public static long currentTimeMillis()
222 return VMSystem.currentTimeMillis();
226 * Copy one array onto another from <code>src[srcStart]</code> ...
227 * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
228 * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
229 * neither array may be null, they must be of compatible types, and the
230 * start and length must fit within both arrays. Then the copying starts,
231 * and proceeds through increasing slots. If src and dest are the same
232 * array, this will appear to copy the data to a temporary location first.
233 * An ArrayStoreException in the middle of copying will leave earlier
234 * elements copied, but later elements unchanged.
236 * @param src the array to copy elements from
237 * @param srcStart the starting position in src
238 * @param dest the array to copy elements to
239 * @param destStart the starting position in dest
240 * @param len the number of elements to copy
241 * @throws NullPointerException if src or dest is null
242 * @throws ArrayStoreException if src or dest is not an array, if they are
243 * not compatible array types, or if an incompatible runtime type
244 * is stored in dest
245 * @throws IndexOutOfBoundsException if len is negative, or if the start or
246 * end copy position in either array is out of bounds
248 public static void arraycopy(Object src, int srcStart,
249 Object dest, int destStart, int len)
251 VMSystem.arraycopy(src, srcStart, dest, destStart, len);
255 * Get a hash code computed by the VM for the Object. This hash code will
256 * be the same as Object's hashCode() method. It is usually some
257 * convolution of the pointer to the Object internal to the VM. It
258 * follows standard hash code rules, in that it will remain the same for a
259 * given Object for the lifetime of that Object.
261 * @param o the Object to get the hash code for
262 * @return the VM-dependent hash code for this Object
263 * @since 1.1
265 public static int identityHashCode(Object o)
267 return VMSystem.identityHashCode(o);
271 * Get all the system properties at once. A security check may be performed,
272 * <code>checkPropertiesAccess</code>. Note that a security manager may
273 * allow getting a single property, but not the entire group.
275 * <p>The required properties include:
276 * <dl>
277 * <dt>java.version</dt> <dd>Java version number</dd>
278 * <dt>java.vendor</dt> <dd>Java vendor specific string</dd>
279 * <dt>java.vendor.url</dt> <dd>Java vendor URL</dd>
280 * <dt>java.home</dt> <dd>Java installation directory</dd>
281 * <dt>java.vm.specification.version</dt> <dd>VM Spec version</dd>
282 * <dt>java.vm.specification.vendor</dt> <dd>VM Spec vendor</dd>
283 * <dt>java.vm.specification.name</dt> <dd>VM Spec name</dd>
284 * <dt>java.vm.version</dt> <dd>VM implementation version</dd>
285 * <dt>java.vm.vendor</dt> <dd>VM implementation vendor</dd>
286 * <dt>java.vm.name</dt> <dd>VM implementation name</dd>
287 * <dt>java.specification.version</dt> <dd>Java Runtime Environment version</dd>
288 * <dt>java.specification.vendor</dt> <dd>Java Runtime Environment vendor</dd>
289 * <dt>java.specification.name</dt> <dd>Java Runtime Environment name</dd>
290 * <dt>java.class.version</dt> <dd>Java class version number</dd>
291 * <dt>java.class.path</dt> <dd>Java classpath</dd>
292 * <dt>java.library.path</dt> <dd>Path for finding Java libraries</dd>
293 * <dt>java.io.tmpdir</dt> <dd>Default temp file path</dd>
294 * <dt>java.compiler</dt> <dd>Name of JIT to use</dd>
295 * <dt>java.ext.dirs</dt> <dd>Java extension path</dd>
296 * <dt>os.name</dt> <dd>Operating System Name</dd>
297 * <dt>os.arch</dt> <dd>Operating System Architecture</dd>
298 * <dt>os.version</dt> <dd>Operating System Version</dd>
299 * <dt>file.separator</dt> <dd>File separator ("/" on Unix)</dd>
300 * <dt>path.separator</dt> <dd>Path separator (":" on Unix)</dd>
301 * <dt>line.separator</dt> <dd>Line separator ("\n" on Unix)</dd>
302 * <dt>user.name</dt> <dd>User account name</dd>
303 * <dt>user.home</dt> <dd>User home directory</dd>
304 * <dt>user.dir</dt> <dd>User's current working directory</dd>
305 * </dl>
307 * In addition, gnu defines several other properties, where ? stands for
308 * each character in '0' through '9':
309 * <dl>
310 * <dt>gnu.classpath.home</dt> <dd>Path to the classpath libraries.</dd>
311 * <dt>gnu.classpath.version</dt> <dd>Version of the classpath libraries.</dd>
312 * <dt>gnu.classpath.vm.shortname</dt> <dd>Succinct version of the VM name;
313 * used for finding property files in file system</dd>
314 * <dt>gnu.classpath.home.url</dt> <dd> Base URL; used for finding
315 * property files in file system</dd>
316 * <dt>gnu.cpu.endian</dt> <dd>big or little</dd>
317 * <dt>gnu.java.io.encoding_scheme_alias.iso-8859-?</dt> <dd>8859_?</dd>
318 * <dt>gnu.java.io.encoding_scheme_alias.iso8859_?</dt> <dd>8859_?</dd>
319 * <dt>gnu.java.io.encoding_scheme_alias.iso-latin-_?</dt> <dd>8859_?</dd>
320 * <dt>gnu.java.io.encoding_scheme_alias.latin?</dt> <dd>8859_?</dd>
321 * <dt>gnu.java.io.encoding_scheme_alias.utf-8</dt> <dd>UTF8</dd>
322 * </dl>
324 * @return the system properties, will never be null
325 * @throws SecurityException if permission is denied
327 public static Properties getProperties()
329 SecurityManager sm = SecurityManager.current; // Be thread-safe.
330 if (sm != null)
331 sm.checkPropertiesAccess();
332 return SystemProperties.getProperties();
336 * Set all the system properties at once. A security check may be performed,
337 * <code>checkPropertiesAccess</code>. Note that a security manager may
338 * allow setting a single property, but not the entire group. An argument
339 * of null resets the properties to the startup default.
341 * @param properties the new set of system properties
342 * @throws SecurityException if permission is denied
344 public static void setProperties(Properties properties)
346 SecurityManager sm = SecurityManager.current; // Be thread-safe.
347 if (sm != null)
348 sm.checkPropertiesAccess();
349 SystemProperties.setProperties(properties);
353 * Get a single system property by name. A security check may be performed,
354 * <code>checkPropertyAccess(key)</code>.
356 * @param key the name of the system property to get
357 * @return the property, or null if not found
358 * @throws SecurityException if permission is denied
359 * @throws NullPointerException if key is null
360 * @throws IllegalArgumentException if key is ""
362 public static String getProperty(String key)
364 SecurityManager sm = SecurityManager.current; // Be thread-safe.
365 if (sm != null)
366 sm.checkPropertyAccess(key);
367 if (key.length() == 0)
368 throw new IllegalArgumentException("key can't be empty");
369 return SystemProperties.getProperty(key);
373 * Get a single system property by name. A security check may be performed,
374 * <code>checkPropertyAccess(key)</code>.
376 * @param key the name of the system property to get
377 * @param def the default
378 * @return the property, or def if not found
379 * @throws SecurityException if permission is denied
380 * @throws NullPointerException if key is null
381 * @throws IllegalArgumentException if key is ""
383 public static String getProperty(String key, String def)
385 SecurityManager sm = SecurityManager.current; // Be thread-safe.
386 if (sm != null)
387 sm.checkPropertyAccess(key);
388 // This handles both the null pointer exception and the illegal
389 // argument exception.
390 if (key.length() == 0)
391 throw new IllegalArgumentException("key can't be empty");
392 return SystemProperties.getProperty(key, def);
396 * Set a single system property by name. A security check may be performed,
397 * <code>checkPropertyAccess(key, "write")</code>.
399 * @param key the name of the system property to set
400 * @param value the new value
401 * @return the previous value, or null
402 * @throws SecurityException if permission is denied
403 * @throws NullPointerException if key is null
404 * @throws IllegalArgumentException if key is ""
405 * @since 1.2
407 public static String setProperty(String key, String value)
409 SecurityManager sm = SecurityManager.current; // Be thread-safe.
410 if (sm != null)
411 sm.checkPermission(new PropertyPermission(key, "write"));
412 // This handles both the null pointer exception and the illegal
413 // argument exception.
414 if (key.length() == 0)
415 throw new IllegalArgumentException("key can't be empty");
416 return SystemProperties.setProperty(key, value);
420 * Remove a single system property by name. A security check may be
421 * performed, <code>checkPropertyAccess(key, "write")</code>.
423 * @param key the name of the system property to remove
424 * @return the previous value, or null
425 * @throws SecurityException if permission is denied
426 * @throws NullPointerException if key is null
427 * @throws IllegalArgumentException if key is ""
428 * @since 1.5
430 public static String clearProperty(String key)
432 SecurityManager sm = SecurityManager.current; // Be thread-safe.
433 if (sm != null)
434 sm.checkPermission(new PropertyPermission(key, "write"));
435 // This handles both the null pointer exception and the illegal
436 // argument exception.
437 if (key.length() == 0)
438 throw new IllegalArgumentException("key can't be empty");
439 return SystemProperties.remove(key);
443 * Gets the value of an environment variable.
445 * @param name the name of the environment variable
446 * @return the string value of the variable or null when the
447 * environment variable is not defined.
448 * @throws NullPointerException
449 * @throws SecurityException if permission is denied
450 * @since 1.5
451 * @specnote This method was deprecated in some JDK releases, but
452 * was restored in 1.5.
454 public static String getenv(String name)
456 if (name == null)
457 throw new NullPointerException();
458 SecurityManager sm = SecurityManager.current; // Be thread-safe.
459 if (sm != null)
460 sm.checkPermission(new RuntimePermission("getenv." + name));
461 return VMSystem.getenv(name);
465 * Terminate the Virtual Machine. This just calls
466 * <code>Runtime.getRuntime().exit(status)</code>, and never returns.
467 * Obviously, a security check is in order, <code>checkExit</code>.
469 * @param status the exit status; by convention non-zero is abnormal
470 * @throws SecurityException if permission is denied
471 * @see Runtime#exit(int)
473 public static void exit(int status)
475 Runtime.getRuntime().exit(status);
479 * Calls the garbage collector. This is only a hint, and it is up to the
480 * implementation what this hint suggests, but it usually causes a
481 * best-effort attempt to reclaim unused memory from discarded objects.
482 * This calls <code>Runtime.getRuntime().gc()</code>.
484 * @see Runtime#gc()
486 public static void gc()
488 Runtime.getRuntime().gc();
492 * Runs object finalization on pending objects. This is only a hint, and
493 * it is up to the implementation what this hint suggests, but it usually
494 * causes a best-effort attempt to run finalizers on all objects ready
495 * to be reclaimed. This calls
496 * <code>Runtime.getRuntime().runFinalization()</code>.
498 * @see Runtime#runFinalization()
500 public static void runFinalization()
502 Runtime.getRuntime().runFinalization();
506 * Tell the Runtime whether to run finalization before exiting the
507 * JVM. This is inherently unsafe in multi-threaded applications,
508 * since it can force initialization on objects which are still in use
509 * by live threads, leading to deadlock; therefore this is disabled by
510 * default. There may be a security check, <code>checkExit(0)</code>. This
511 * calls <code>Runtime.runFinalizersOnExit()</code>.
513 * @param finalizeOnExit whether to run finalizers on exit
514 * @throws SecurityException if permission is denied
515 * @see Runtime#runFinalizersOnExit(boolean)
516 * @since 1.1
517 * @deprecated never rely on finalizers to do a clean, thread-safe,
518 * mop-up from your code
520 public static void runFinalizersOnExit(boolean finalizeOnExit)
522 Runtime.runFinalizersOnExit(finalizeOnExit);
526 * Load a code file using its explicit system-dependent filename. A security
527 * check may be performed, <code>checkLink</code>. This just calls
528 * <code>Runtime.getRuntime().load(filename)</code>.
530 * <p>
531 * The library is loaded using the class loader associated with the
532 * class associated with the invoking method.
534 * @param filename the code file to load
535 * @throws SecurityException if permission is denied
536 * @throws UnsatisfiedLinkError if the file cannot be loaded
537 * @see Runtime#load(String)
539 public static void load(String filename)
541 Runtime.getRuntime().load(filename, VMStackWalker.getCallingClassLoader());
545 * Load a library using its explicit system-dependent filename. A security
546 * check may be performed, <code>checkLink</code>. This just calls
547 * <code>Runtime.getRuntime().load(filename)</code>.
549 * <p>
550 * The library is loaded using the class loader associated with the
551 * class associated with the invoking method.
553 * @param libname the library file to load
554 * @throws SecurityException if permission is denied
555 * @throws UnsatisfiedLinkError if the file cannot be loaded
556 * @see Runtime#load(String)
558 public static void loadLibrary(String libname)
560 Runtime.getRuntime().loadLibrary(libname,
561 VMStackWalker.getCallingClassLoader());
565 * Convert a library name to its platform-specific variant.
567 * @param libname the library name, as used in <code>loadLibrary</code>
568 * @return the platform-specific mangling of the name
569 * @since 1.2
571 public static String mapLibraryName(String libname)
573 return VMRuntime.mapLibraryName(libname);
576 } // class System