]> git.ipfire.org Git - thirdparty/gcc.git/blob - libjava/java/lang/System.java
System.java: Reordered imports.
[thirdparty/gcc.git] / libjava / java / lang / System.java
1 /* System.java -- useful methods to interface with the system
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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, or (at your option)
9 any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
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
24 combination.
25
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. */
37
38
39 package java.lang;
40
41 import gnu.classpath.Configuration;
42
43 import java.io.BufferedInputStream;
44 import java.io.BufferedOutputStream;
45 import java.io.FileDescriptor;
46 import java.io.FileInputStream;
47 import java.io.FileOutputStream;
48 import java.io.InputStream;
49 import java.io.PrintStream;
50 import java.util.Properties;
51 import java.util.PropertyPermission;
52
53 /**
54 * System represents system-wide resources; things that represent the
55 * general environment. As such, all methods are static.
56 *
57 * @author John Keiser
58 * @author Eric Blake <ebb9@email.byu.edu>
59 * @since 1.0
60 * @status still missing 1.4 functionality
61 */
62 public final class System
63 {
64 // WARNING: System is a CORE class in the bootstrap cycle. See the comments
65 // in vm/reference/java/lang/Runtime for implications of this fact.
66
67 /**
68 * Add to the default properties. The field is stored in Runtime, because
69 * of the bootstrap sequence; but this adds several useful properties to
70 * the defaults. Once the default is stabilized, it should not be modified;
71 * instead it is passed as a parent properties for fast setup of the
72 * defaults when calling <code>setProperties(null)</code>.
73 */
74 static
75 {
76 // Note that this loadLibrary() takes precedence over the one in Object,
77 // since Object.<clinit> is waiting for System.<clinit> to complete
78 // first; but loading a library twice is harmless.
79 if (Configuration.INIT_LOAD_LIBRARY)
80 loadLibrary("javalang");
81
82 Properties defaultProperties = Runtime.defaultProperties;
83
84 // Set base URL if not already set.
85 if (defaultProperties.get("gnu.classpath.home.url") == null)
86 defaultProperties.put("gnu.classpath.home.url",
87 "file://"
88 + defaultProperties.get("gnu.classpath.home")
89 + "/lib");
90
91 // Set short name if not already set.
92 if (defaultProperties.get("gnu.classpath.vm.shortname") == null)
93 {
94 String value = defaultProperties.getProperty("java.vm.name");
95 int index = value.lastIndexOf(' ');
96 if (index != -1)
97 value = value.substring(index + 1);
98 defaultProperties.put("gnu.classpath.vm.shortname", value);
99 }
100
101 defaultProperties.put("gnu.cpu.endian",
102 isWordsBigEndian() ? "big" : "little");
103
104 // GCJ LOCAL: Classpath sets common encoding aliases here.
105 // Since we don't (yet) have gnu.java.io.EncodingManager, these
106 // are a waste of time and just slow down system startup.
107
108 // XXX FIXME - Temp hack for old systems that set the wrong property
109 if (defaultProperties.get("java.io.tmpdir") == null)
110 defaultProperties.put("java.io.tmpdir",
111 defaultProperties.get("java.tmpdir"));
112 }
113
114 /**
115 * Stores the current system properties. This can be modified by
116 * {@link #setProperties(Properties)}, but will never be null, because
117 * setProperties(null) sucks in the default properties.
118 */
119 // Note that we use clone here and not new. Some programs assume
120 // that the system properties do not have a parent.
121 private static Properties properties
122 = (Properties) Runtime.defaultProperties.clone();
123
124 /**
125 * The standard InputStream. This is assigned at startup and starts its
126 * life perfectly valid. Although it is marked final, you can change it
127 * using {@link #setIn(InputStream)} through some hefty VM magic.
128 *
129 * <p>This corresponds to the C stdin and C++ cin variables, which
130 * typically input from the keyboard, but may be used to pipe input from
131 * other processes or files. That should all be transparent to you,
132 * however.
133 */
134 public static final InputStream in
135 = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
136 /**
137 * The standard output PrintStream. This is assigned at startup and
138 * starts its life perfectly valid. Although it is marked final, you can
139 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
140 *
141 * <p>This corresponds to the C stdout and C++ cout variables, which
142 * typically output normal messages to the screen, but may be used to pipe
143 * output to other processes or files. That should all be transparent to
144 * you, however.
145 */
146 public static final PrintStream out
147 = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true);
148 /**
149 * The standard output PrintStream. This is assigned at startup and
150 * starts its life perfectly valid. Although it is marked final, you can
151 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
152 *
153 * <p>This corresponds to the C stderr and C++ cerr variables, which
154 * typically output error messages to the screen, but may be used to pipe
155 * output to other processes or files. That should all be transparent to
156 * you, however.
157 */
158 public static final PrintStream err
159 = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err)), true);
160
161 /**
162 * This class is uninstantiable.
163 */
164 private System()
165 {
166 }
167
168 /**
169 * Set {@link #in} to a new InputStream. This uses some VM magic to change
170 * a "final" variable, so naturally there is a security check,
171 * <code>RuntimePermission("setIO")</code>.
172 *
173 * @param in the new InputStream
174 * @throws SecurityException if permission is denied
175 * @since 1.1
176 */
177 public static void setIn(InputStream in)
178 {
179 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
180 if (sm != null)
181 sm.checkPermission(new RuntimePermission("setIO"));
182 setIn0(in);
183 }
184
185 /**
186 * Set {@link #out} to a new PrintStream. This uses some VM magic to change
187 * a "final" variable, so naturally there is a security check,
188 * <code>RuntimePermission("setIO")</code>.
189 *
190 * @param out the new PrintStream
191 * @throws SecurityException if permission is denied
192 * @since 1.1
193 */
194 public static void setOut(PrintStream out)
195 {
196 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
197 if (sm != null)
198 sm.checkPermission(new RuntimePermission("setIO"));
199
200 setOut0(out);
201 }
202
203 /**
204 * Set {@link #err} to a new PrintStream. This uses some VM magic to change
205 * a "final" variable, so naturally there is a security check,
206 * <code>RuntimePermission("setIO")</code>.
207 *
208 * @param err the new PrintStream
209 * @throws SecurityException if permission is denied
210 * @since 1.1
211 */
212 public static void setErr(PrintStream err)
213 {
214 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
215 if (sm != null)
216 sm.checkPermission(new RuntimePermission("setIO"));
217 setErr0(err);
218 }
219
220 /**
221 * Set the current SecurityManager. If a security manager already exists,
222 * then <code>RuntimePermission("setSecurityManager")</code> is checked
223 * first. Since this permission is denied by the default security manager,
224 * setting the security manager is often an irreversible action.
225 *
226 * <STRONG>Spec Note:</STRONG> Don't ask me, I didn't write it. It looks
227 * pretty vulnerable; whoever gets to the gate first gets to set the policy.
228 * There is probably some way to set the original security manager as a
229 * command line argument to the VM, but I don't know it.
230 *
231 * @param sm the new SecurityManager
232 * @throws SecurityException if permission is denied
233 */
234 public synchronized static void setSecurityManager(SecurityManager sm)
235 {
236 // Implementation note: the field lives in Runtime because of bootstrap
237 // initialization issues. This method is synchronized so that no other
238 // thread changes it to null before this thread makes the change.
239 if (Runtime.securityManager != null)
240 Runtime.securityManager.checkPermission
241 (new RuntimePermission("setSecurityManager"));
242 Runtime.securityManager = sm;
243 }
244
245 /**
246 * Get the current SecurityManager. If the SecurityManager has not been
247 * set yet, then this method returns null.
248 *
249 * @return the current SecurityManager, or null
250 */
251 public static SecurityManager getSecurityManager()
252 {
253 // Implementation note: the field lives in Runtime because of bootstrap
254 // initialization issues.
255 return Runtime.securityManager;
256 }
257
258 /**
259 * Get the current time, measured in the number of milliseconds from the
260 * beginning of Jan. 1, 1970. This is gathered from the system clock, with
261 * any attendant incorrectness (it may be timezone dependent).
262 *
263 * @return the current time
264 * @see java.util.Date
265 */
266 public static native long currentTimeMillis();
267
268 /**
269 * Copy one array onto another from <code>src[srcStart]</code> ...
270 * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
271 * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
272 * neither array may be null, they must be of compatible types, and the
273 * start and length must fit within both arrays. Then the copying starts,
274 * and proceeds through increasing slots. If src and dest are the same
275 * array, this will appear to copy the data to a temporary location first.
276 * An ArrayStoreException in the middle of copying will leave earlier
277 * elements copied, but later elements unchanged.
278 *
279 * @param src the array to copy elements from
280 * @param srcStart the starting position in src
281 * @param dest the array to copy elements to
282 * @param destStart the starting position in dest
283 * @param len the number of elements to copy
284 * @throws NullPointerException if src or dest is null
285 * @throws ArrayStoreException if src or dest is not an array, if they are
286 * not compatible array types, or if an incompatible runtime type
287 * is stored in dest
288 * @throws IndexOutOfBoundsException if len is negative, or if the start or
289 * end copy position in either array is out of bounds
290 */
291 public static native void arraycopy(Object src, int srcStart,
292 Object dest, int destStart, int len);
293
294 /**
295 * Get a hash code computed by the VM for the Object. This hash code will
296 * be the same as Object's hashCode() method. It is usually some
297 * convolution of the pointer to the Object internal to the VM. It
298 * follows standard hash code rules, in that it will remain the same for a
299 * given Object for the lifetime of that Object.
300 *
301 * @param o the Object to get the hash code for
302 * @return the VM-dependent hash code for this Object
303 * @since 1.1
304 */
305 public static native int identityHashCode(Object o);
306
307 /**
308 * Get all the system properties at once. A security check may be performed,
309 * <code>checkPropertiesAccess</code>. Note that a security manager may
310 * allow getting a single property, but not the entire group.
311 *
312 * <p>The required properties include:
313 * <dl>
314 * <dt>java.version</dt> <dd>Java version number</dd>
315 * <dt>java.vendor</dt> <dd>Java vendor specific string</dd>
316 * <dt>java.vendor.url</dt> <dd>Java vendor URL</dd>
317 * <dt>java.home</dt> <dd>Java installation directory</dd>
318 * <dt>java.vm.specification.version</dt> <dd>VM Spec version</dd>
319 * <dt>java.vm.specification.vendor</dt> <dd>VM Spec vendor</dd>
320 * <dt>java.vm.specification.name</dt> <dd>VM Spec name</dd>
321 * <dt>java.vm.version</dt> <dd>VM implementation version</dd>
322 * <dt>java.vm.vendor</dt> <dd>VM implementation vendor</dd>
323 * <dt>java.vm.name</dt> <dd>VM implementation name</dd>
324 * <dt>java.specification.version</dt> <dd>Java Runtime Environment version</dd>
325 * <dt>java.specification.vendor</dt> <dd>Java Runtime Environment vendor</dd>
326 * <dt>java.specification.name</dt> <dd>Java Runtime Environment name</dd>
327 * <dt>java.class.version</dt> <dd>Java class version number</dd>
328 * <dt>java.class.path</dt> <dd>Java classpath</dd>
329 * <dt>java.library.path</dt> <dd>Path for finding Java libraries</dd>
330 * <dt>java.io.tmpdir</dt> <dd>Default temp file path</dd>
331 * <dt>java.compiler</dt> <dd>Name of JIT to use</dd>
332 * <dt>java.ext.dirs</dt> <dd>Java extension path</dd>
333 * <dt>os.name</dt> <dd>Operating System Name</dd>
334 * <dt>os.arch</dt> <dd>Operating System Architecture</dd>
335 * <dt>os.version</dt> <dd>Operating System Version</dd>
336 * <dt>file.separator</dt> <dd>File separator ("/" on Unix)</dd>
337 * <dt>path.separator</dt> <dd>Path separator (":" on Unix)</dd>
338 * <dt>line.separator</dt> <dd>Line separator ("\n" on Unix)</dd>
339 * <dt>user.name</dt> <dd>User account name</dd>
340 * <dt>user.home</dt> <dd>User home directory</dd>
341 * <dt>user.dir</dt> <dd>User's current working directory</dd>
342 * </dl>
343 *
344 * In addition, gnu defines several other properties, where ? stands for
345 * each character in '0' through '9':
346 * <dl>
347 * <dt>gnu.classpath.home</dt> <dd>Path to the classpath libraries.</dd>
348 * <dt>gnu.classpath.version</dt> <dd>Version of the classpath libraries.</dd>
349 * <dt>gnu.classpath.vm.shortname</dt> <dd>Succinct version of the VM name;
350 * used for finding property files in file system</dd>
351 * <dt>gnu.classpath.home.url</dt> <dd> Base URL; used for finding
352 * property files in file system</dd>
353 * <dt>gnu.cpu.endian</dt> <dd>big or little</dd>
354 * <dt>gnu.java.io.encoding_scheme_alias.ISO-8859-?</dt> <dd>8859_?</dd>
355 * <dt>gnu.java.io.encoding_scheme_alias.iso-8859-?</dt> <dd>8859_?</dd>
356 * <dt>gnu.java.io.encoding_scheme_alias.iso8859_?</dt> <dd>8859_?</dd>
357 * <dt>gnu.java.io.encoding_scheme_alias.iso-latin-_?</dt> <dd>8859_?</dd>
358 * <dt>gnu.java.io.encoding_scheme_alias.latin?</dt> <dd>8859_?</dd>
359 * <dt>gnu.java.io.encoding_scheme_alias.UTF-8</dt> <dd>UTF8</dd>
360 * <dt>gnu.java.io.encoding_scheme_alias.utf-8</dt> <dd>UTF8</dd>
361 * </dl>
362 *
363 * @return the system properties, will never be null
364 * @throws SecurityException if permission is denied
365 */
366 public static Properties getProperties()
367 {
368 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
369 if (sm != null)
370 sm.checkPropertiesAccess();
371 return properties;
372 }
373
374 /**
375 * Set all the system properties at once. A security check may be performed,
376 * <code>checkPropertiesAccess</code>. Note that a security manager may
377 * allow setting a single property, but not the entire group. An argument
378 * of null resets the properties to the startup default.
379 *
380 * @param properties the new set of system properties
381 * @throws SecurityException if permission is denied
382 */
383 public static void setProperties(Properties properties)
384 {
385 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
386 if (sm != null)
387 sm.checkPropertiesAccess();
388 if (properties == null)
389 {
390 // Note that we use clone here and not new. Some programs
391 // assume that the system properties do not have a parent.
392 properties = (Properties) Runtime.defaultProperties.clone();
393 }
394 System.properties = properties;
395 }
396
397 /**
398 * Get a single system property by name. A security check may be performed,
399 * <code>checkPropertyAccess(key)</code>.
400 *
401 * @param key the name of the system property to get
402 * @return the property, or null if not found
403 * @throws SecurityException if permission is denied
404 * @throws NullPointerException if key is null
405 * @throws IllegalArgumentException if key is ""
406 */
407 public static String getProperty(String key)
408 {
409 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
410 if (sm != null)
411 sm.checkPropertyAccess(key);
412 else if (key.length() == 0)
413 throw new IllegalArgumentException("key can't be empty");
414 return properties.getProperty(key);
415 }
416
417 /**
418 * Get a single system property by name. A security check may be performed,
419 * <code>checkPropertyAccess(key)</code>.
420 *
421 * @param key the name of the system property to get
422 * @param def the default
423 * @return the property, or def if not found
424 * @throws SecurityException if permission is denied
425 * @throws NullPointerException if key is null
426 * @throws IllegalArgumentException if key is ""
427 */
428 public static String getProperty(String key, String def)
429 {
430 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
431 if (sm != null)
432 sm.checkPropertyAccess(key);
433 return properties.getProperty(key, def);
434 }
435
436 /**
437 * Set a single system property by name. A security check may be performed,
438 * <code>checkPropertyAccess(key, "write")</code>.
439 *
440 * @param key the name of the system property to set
441 * @param value the new value
442 * @return the previous value, or null
443 * @throws SecurityException if permission is denied
444 * @throws NullPointerException if key is null
445 * @throws IllegalArgumentException if key is ""
446 * @since 1.2
447 */
448 public static String setProperty(String key, String value)
449 {
450 SecurityManager sm = Runtime.securityManager; // Be thread-safe.
451 if (sm != null)
452 sm.checkPermission(new PropertyPermission(key, "write"));
453 return (String) properties.setProperty(key, value);
454 }
455
456 /**
457 * This used to get an environment variable, but following Sun's lead,
458 * it now throws an Error. Use <code>getProperty</code> instead.
459 *
460 * @param name the name of the environment variable
461 * @return this does not return
462 * @throws Error this is not supported
463 * @deprecated use {@link #getProperty(String)}; getenv is not supported
464 */
465 public static String getenv(String name)
466 {
467 throw new Error("getenv no longer supported, use properties instead: "
468 + name);
469 }
470
471 /**
472 * Terminate the Virtual Machine. This just calls
473 * <code>Runtime.getRuntime().exit(status)</code>, and never returns.
474 * Obviously, a security check is in order, <code>checkExit</code>.
475 *
476 * @param status the exit status; by convention non-zero is abnormal
477 * @throws SecurityException if permission is denied
478 * @see Runtime#exit(int)
479 */
480 public static void exit(int status)
481 {
482 Runtime.getRuntime().exit(status);
483 }
484
485 /**
486 * Calls the garbage collector. This is only a hint, and it is up to the
487 * implementation what this hint suggests, but it usually causes a
488 * best-effort attempt to reclaim unused memory from discarded objects.
489 * This calls <code>Runtime.getRuntime().gc()</code>.
490 *
491 * @see Runtime#gc()
492 */
493 public static void gc()
494 {
495 Runtime.getRuntime().gc();
496 }
497
498 /**
499 * Runs object finalization on pending objects. This is only a hint, and
500 * it is up to the implementation what this hint suggests, but it usually
501 * causes a best-effort attempt to run finalizers on all objects ready
502 * to be reclaimed. This calls
503 * <code>Runtime.getRuntime().runFinalization()</code>.
504 *
505 * @see Runtime#runFinalization()
506 */
507 public static void runFinalization()
508 {
509 Runtime.getRuntime().runFinalization();
510 }
511
512 /**
513 * Tell the Runtime whether to run finalization before exiting the
514 * JVM. This is inherently unsafe in multi-threaded applications,
515 * since it can force initialization on objects which are still in use
516 * by live threads, leading to deadlock; therefore this is disabled by
517 * default. There may be a security check, <code>checkExit(0)</code>. This
518 * calls <code>Runtime.getRuntime().runFinalizersOnExit()</code>.
519 *
520 * @param finalizeOnExit whether to run finalizers on exit
521 * @throws SecurityException if permission is denied
522 * @see Runtime#runFinalizersOnExit()
523 * @since 1.1
524 * @deprecated never rely on finalizers to do a clean, thread-safe,
525 * mop-up from your code
526 */
527 public static void runFinalizersOnExit(boolean finalizeOnExit)
528 {
529 Runtime.getRuntime().runFinalizersOnExit(finalizeOnExit);
530 }
531
532 /**
533 * Load a code file using its explicit system-dependent filename. A security
534 * check may be performed, <code>checkLink</code>. This just calls
535 * <code>Runtime.getRuntime().load(filename)</code>.
536 *
537 * @param filename the code file to load
538 * @throws SecurityException if permission is denied
539 * @throws UnsatisfiedLinkError if the file cannot be loaded
540 * @see Runtime#load(String)
541 */
542 public static void load(String filename)
543 {
544 Runtime.getRuntime().load(filename);
545 }
546
547 /**
548 * Load a library using its explicit system-dependent filename. A security
549 * check may be performed, <code>checkLink</code>. This just calls
550 * <code>Runtime.getRuntime().load(filename)</code>.
551 *
552 * @param libname the library file to load
553 * @throws SecurityException if permission is denied
554 * @throws UnsatisfiedLinkError if the file cannot be loaded
555 * @see Runtime#load(String)
556 */
557 public static void loadLibrary(String libname)
558 {
559 Runtime.getRuntime().loadLibrary(libname);
560 }
561
562 /**
563 * Convert a library name to its platform-specific variant.
564 *
565 * @param libname the library name, as used in <code>loadLibrary</code>
566 * @return the platform-specific mangling of the name
567 * @since 1.2
568 */
569 public static String mapLibraryName(String libname)
570 {
571 // XXX Fix this!!!!
572 return Runtime.nativeGetLibname("", libname);
573 }
574
575 /**
576 * Detect big-endian systems.
577 *
578 * @return true if the system is big-endian.
579 */
580 static native boolean isWordsBigEndian();
581
582 /**
583 * Set {@link #in} to a new InputStream.
584 *
585 * @param in the new InputStream
586 * @see #setIn(InputStream)
587 */
588 private static native void setIn0(InputStream in);
589
590 /**
591 * Set {@link #out} to a new PrintStream.
592 *
593 * @param out the new PrintStream
594 * @see #setOut(PrintStream)
595 */
596 private static native void setOut0(PrintStream out);
597
598 /**
599 * Set {@link #err} to a new PrintStream.
600 *
601 * @param err the new PrintStream
602 * @see #setErr(PrintStream)
603 */
604 private static native void setErr0(PrintStream err);
605 } // class System