1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 package java.lang; 27 28 import dalvik.annotation.optimization.FastNative; 29 import android.system.ErrnoException; 30 import android.system.StructPasswd; 31 import android.system.StructUtsname; 32 import dalvik.system.VMRuntime; 33 import dalvik.system.VMStack; 34 import java.io.*; 35 import java.lang.annotation.Annotation; 36 import java.nio.channels.Channel; 37 import java.nio.channels.spi.SelectorProvider; 38 import java.util.Locale; 39 import java.util.Map; 40 import java.util.Properties; 41 import java.util.PropertyPermission; 42 import libcore.icu.ICU; 43 import libcore.io.Libcore; 44 import libcore.util.TimeZoneDataFiles; 45 46 import sun.reflect.CallerSensitive; 47 import sun.security.util.SecurityConstants; 48 /** 49 * The <code>System</code> class contains several useful class fields 50 * and methods. It cannot be instantiated. 51 * 52 * <p>Among the facilities provided by the <code>System</code> class 53 * are standard input, standard output, and error output streams; 54 * access to externally defined properties and environment 55 * variables; a means of loading files and libraries; and a utility 56 * method for quickly copying a portion of an array. 57 * 58 * @author unascribed 59 * @since JDK1.0 60 */ 61 public final class System { 62 /** Don't let anyone instantiate this class */ 63 private System() { 64 } 65 66 /** 67 * The "standard" input stream. This stream is already 68 * open and ready to supply input data. Typically this stream 69 * corresponds to keyboard input or another input source specified by 70 * the host environment or user. 71 */ 72 public final static InputStream in; 73 74 /** 75 * The "standard" output stream. This stream is already 76 * open and ready to accept output data. Typically this stream 77 * corresponds to display output or another output destination 78 * specified by the host environment or user. 79 * <p> 80 * For simple stand-alone Java applications, a typical way to write 81 * a line of output data is: 82 * <blockquote><pre> 83 * System.out.println(data) 84 * </pre></blockquote> 85 * <p> 86 * See the <code>println</code> methods in class <code>PrintStream</code>. 87 * 88 * @see java.io.PrintStream#println() 89 * @see java.io.PrintStream#println(boolean) 90 * @see java.io.PrintStream#println(char) 91 * @see java.io.PrintStream#println(char[]) 92 * @see java.io.PrintStream#println(double) 93 * @see java.io.PrintStream#println(float) 94 * @see java.io.PrintStream#println(int) 95 * @see java.io.PrintStream#println(long) 96 * @see java.io.PrintStream#println(java.lang.Object) 97 * @see java.io.PrintStream#println(java.lang.String) 98 */ 99 public final static PrintStream out; 100 101 /** 102 * The "standard" error output stream. This stream is already 103 * open and ready to accept output data. 104 * <p> 105 * Typically this stream corresponds to display output or another 106 * output destination specified by the host environment or user. By 107 * convention, this output stream is used to display error messages 108 * or other information that should come to the immediate attention 109 * of a user even if the principal output stream, the value of the 110 * variable <code>out</code>, has been redirected to a file or other 111 * destination that is typically not continuously monitored. 112 */ 113 public final static PrintStream err; 114 115 /** 116 * Dedicated lock for GC / Finalization logic. 117 */ 118 private static final Object LOCK = new Object(); 119 120 /** 121 * Whether or not we need to do a GC before running the finalizers. 122 */ 123 private static boolean runGC; 124 125 /** 126 * If we just ran finalization, we might want to do a GC to free the finalized objects. 127 * This lets us do gc/runFinlization/gc sequences but prevents back to back System.gc(). 128 */ 129 private static boolean justRanFinalization; 130 131 /** 132 * Reassigns the "standard" input stream. 133 * 134 * <p>First, if there is a security manager, its <code>checkPermission</code> 135 * method is called with a <code>RuntimePermission("setIO")</code> permission 136 * to see if it's ok to reassign the "standard" input stream. 137 * <p> 138 * 139 * @param in the new standard input stream. 140 * 141 * @throws SecurityException 142 * if a security manager exists and its 143 * <code>checkPermission</code> method doesn't allow 144 * reassigning of the standard input stream. 145 * 146 * @see SecurityManager#checkPermission 147 * @see java.lang.RuntimePermission 148 * 149 * @since JDK1.1 150 */ 151 public static void setIn(InputStream in) { 152 setIn0(in); 153 } 154 155 /** 156 * Reassigns the "standard" output stream. 157 * 158 * <p>First, if there is a security manager, its <code>checkPermission</code> 159 * method is called with a <code>RuntimePermission("setIO")</code> permission 160 * to see if it's ok to reassign the "standard" output stream. 161 * 162 * @param out the new standard output stream 163 * 164 * @throws SecurityException 165 * if a security manager exists and its 166 * <code>checkPermission</code> method doesn't allow 167 * reassigning of the standard output stream. 168 * 169 * @see SecurityManager#checkPermission 170 * @see java.lang.RuntimePermission 171 * 172 * @since JDK1.1 173 */ 174 public static void setOut(PrintStream out) { 175 setOut0(out); 176 } 177 178 /** 179 * Reassigns the "standard" error output stream. 180 * 181 * <p>First, if there is a security manager, its <code>checkPermission</code> 182 * method is called with a <code>RuntimePermission("setIO")</code> permission 183 * to see if it's ok to reassign the "standard" error output stream. 184 * 185 * @param err the new standard error output stream. 186 * 187 * @throws SecurityException 188 * if a security manager exists and its 189 * <code>checkPermission</code> method doesn't allow 190 * reassigning of the standard error output stream. 191 * 192 * @see SecurityManager#checkPermission 193 * @see java.lang.RuntimePermission 194 * 195 * @since JDK1.1 196 */ 197 public static void setErr(PrintStream err) { 198 setErr0(err); 199 } 200 201 private static volatile Console cons = null; 202 /** 203 * Returns the unique {@link java.io.Console Console} object associated 204 * with the current Java virtual machine, if any. 205 * 206 * @return The system console, if any, otherwise <tt>null</tt>. 207 * 208 * @since 1.6 209 */ 210 public static Console console() { 211 // Android-changed: Added proper double checked locking for cons access 212 if (cons == null) { 213 synchronized (System.class) { 214 if (cons == null) { 215 cons = Console.console(); 216 } 217 } 218 } 219 return cons; 220 } 221 222 /** 223 * Returns the channel inherited from the entity that created this 224 * Java virtual machine. 225 * 226 * <p> This method returns the channel obtained by invoking the 227 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel 228 * inheritedChannel} method of the system-wide default 229 * {@link java.nio.channels.spi.SelectorProvider} object. </p> 230 * 231 * <p> In addition to the network-oriented channels described in 232 * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel 233 * inheritedChannel}, this method may return other kinds of 234 * channels in the future. 235 * 236 * @return The inherited channel, if any, otherwise <tt>null</tt>. 237 * 238 * @throws IOException 239 * If an I/O error occurs 240 * 241 * @throws SecurityException 242 * If a security manager is present and it does not 243 * permit access to the channel. 244 * 245 * @since 1.5 246 */ 247 public static Channel inheritedChannel() throws IOException { 248 return SelectorProvider.provider().inheritedChannel(); 249 } 250 251 private static native void setIn0(InputStream in); 252 private static native void setOut0(PrintStream out); 253 private static native void setErr0(PrintStream err); 254 255 /** 256 * Throws {@code SecurityException} (except in case {@code sm == null}). 257 * 258 * <p>Security managers do <i>not</i> provide a secure environment for 259 * executing untrusted code and are unsupported on Android. Untrusted code 260 * cannot be safely isolated within a single VM on Android, so this method 261 * <i>always</i> throws a {@code SecurityException} when passed a non-null SecurityManager 262 * 263 * @param s a security manager 264 * @throws SecurityException always, unless {@code sm == null} 265 */ 266 public static 267 void setSecurityManager(final SecurityManager s) { 268 if (s != null) { 269 throw new SecurityException(); 270 } 271 } 272 273 /** 274 * Always returns {@code null} in Android 275 * 276 * @return {@code null} in Android 277 */ 278 public static SecurityManager getSecurityManager() { 279 // No-op on android. 280 return null; 281 } 282 283 /** 284 * Returns the current time in milliseconds. Note that 285 * while the unit of time of the return value is a millisecond, 286 * the granularity of the value depends on the underlying 287 * operating system and may be larger. For example, many 288 * operating systems measure time in units of tens of 289 * milliseconds. 290 * 291 * <p> See the description of the class <code>Date</code> for 292 * a discussion of slight discrepancies that may arise between 293 * "computer time" and coordinated universal time (UTC). 294 * 295 * @return the difference, measured in milliseconds, between 296 * the current time and midnight, January 1, 1970 UTC. 297 * @see java.util.Date 298 */ 299 public static native long currentTimeMillis(); 300 301 /** 302 * Returns the current value of the running Java Virtual Machine's 303 * high-resolution time source, in nanoseconds. 304 * 305 * <p>This method can only be used to measure elapsed time and is 306 * not related to any other notion of system or wall-clock time. 307 * The value returned represents nanoseconds since some fixed but 308 * arbitrary <i>origin</i> time (perhaps in the future, so values 309 * may be negative). The same origin is used by all invocations of 310 * this method in an instance of a Java virtual machine; other 311 * virtual machine instances are likely to use a different origin. 312 * 313 * <p>This method provides nanosecond precision, but not necessarily 314 * nanosecond resolution (that is, how frequently the value changes) 315 * - no guarantees are made except that the resolution is at least as 316 * good as that of {@link #currentTimeMillis()}. 317 * 318 * <p>Differences in successive calls that span greater than 319 * approximately 292 years (2<sup>63</sup> nanoseconds) will not 320 * correctly compute elapsed time due to numerical overflow. 321 * 322 * <p>The values returned by this method become meaningful only when 323 * the difference between two such values, obtained within the same 324 * instance of a Java virtual machine, is computed. 325 * 326 * <p> For example, to measure how long some code takes to execute: 327 * <pre> {@code 328 * long startTime = System.nanoTime(); 329 * // ... the code being measured ... 330 * long estimatedTime = System.nanoTime() - startTime;}</pre> 331 * 332 * <p>To compare two nanoTime values 333 * <pre> {@code 334 * long t0 = System.nanoTime(); 335 * ... 336 * long t1 = System.nanoTime();}</pre> 337 * 338 * one should use {@code t1 - t0 < 0}, not {@code t1 < t0}, 339 * because of the possibility of numerical overflow. 340 * 341 * @return the current value of the running Java Virtual Machine's 342 * high-resolution time source, in nanoseconds 343 * @since 1.5 344 */ 345 public static native long nanoTime(); 346 347 /** 348 * Copies an array from the specified source array, beginning at the 349 * specified position, to the specified position of the destination array. 350 * A subsequence of array components are copied from the source 351 * array referenced by <code>src</code> to the destination array 352 * referenced by <code>dest</code>. The number of components copied is 353 * equal to the <code>length</code> argument. The components at 354 * positions <code>srcPos</code> through 355 * <code>srcPos+length-1</code> in the source array are copied into 356 * positions <code>destPos</code> through 357 * <code>destPos+length-1</code>, respectively, of the destination 358 * array. 359 * <p> 360 * If the <code>src</code> and <code>dest</code> arguments refer to the 361 * same array object, then the copying is performed as if the 362 * components at positions <code>srcPos</code> through 363 * <code>srcPos+length-1</code> were first copied to a temporary 364 * array with <code>length</code> components and then the contents of 365 * the temporary array were copied into positions 366 * <code>destPos</code> through <code>destPos+length-1</code> of the 367 * destination array. 368 * <p> 369 * If <code>dest</code> is <code>null</code>, then a 370 * <code>NullPointerException</code> is thrown. 371 * <p> 372 * If <code>src</code> is <code>null</code>, then a 373 * <code>NullPointerException</code> is thrown and the destination 374 * array is not modified. 375 * <p> 376 * Otherwise, if any of the following is true, an 377 * <code>ArrayStoreException</code> is thrown and the destination is 378 * not modified: 379 * <ul> 380 * <li>The <code>src</code> argument refers to an object that is not an 381 * array. 382 * <li>The <code>dest</code> argument refers to an object that is not an 383 * array. 384 * <li>The <code>src</code> argument and <code>dest</code> argument refer 385 * to arrays whose component types are different primitive types. 386 * <li>The <code>src</code> argument refers to an array with a primitive 387 * component type and the <code>dest</code> argument refers to an array 388 * with a reference component type. 389 * <li>The <code>src</code> argument refers to an array with a reference 390 * component type and the <code>dest</code> argument refers to an array 391 * with a primitive component type. 392 * </ul> 393 * <p> 394 * Otherwise, if any of the following is true, an 395 * <code>IndexOutOfBoundsException</code> is 396 * thrown and the destination is not modified: 397 * <ul> 398 * <li>The <code>srcPos</code> argument is negative. 399 * <li>The <code>destPos</code> argument is negative. 400 * <li>The <code>length</code> argument is negative. 401 * <li><code>srcPos+length</code> is greater than 402 * <code>src.length</code>, the length of the source array. 403 * <li><code>destPos+length</code> is greater than 404 * <code>dest.length</code>, the length of the destination array. 405 * </ul> 406 * <p> 407 * Otherwise, if any actual component of the source array from 408 * position <code>srcPos</code> through 409 * <code>srcPos+length-1</code> cannot be converted to the component 410 * type of the destination array by assignment conversion, an 411 * <code>ArrayStoreException</code> is thrown. In this case, let 412 * <b><i>k</i></b> be the smallest nonnegative integer less than 413 * length such that <code>src[srcPos+</code><i>k</i><code>]</code> 414 * cannot be converted to the component type of the destination 415 * array; when the exception is thrown, source array components from 416 * positions <code>srcPos</code> through 417 * <code>srcPos+</code><i>k</i><code>-1</code> 418 * will already have been copied to destination array positions 419 * <code>destPos</code> through 420 * <code>destPos+</code><i>k</I><code>-1</code> and no other 421 * positions of the destination array will have been modified. 422 * (Because of the restrictions already itemized, this 423 * paragraph effectively applies only to the situation where both 424 * arrays have component types that are reference types.) 425 * 426 * @param src the source array. 427 * @param srcPos starting position in the source array. 428 * @param dest the destination array. 429 * @param destPos starting position in the destination data. 430 * @param length the number of array elements to be copied. 431 * @exception IndexOutOfBoundsException if copying would cause 432 * access of data outside array bounds. 433 * @exception ArrayStoreException if an element in the <code>src</code> 434 * array could not be stored into the <code>dest</code> array 435 * because of a type mismatch. 436 * @exception NullPointerException if either <code>src</code> or 437 * <code>dest</code> is <code>null</code>. 438 */ 439 @FastNative 440 public static native void arraycopy(Object src, int srcPos, 441 Object dest, int destPos, 442 int length); 443 444 445 // BEGIN Android-changed 446 /** 447 * The char array length threshold below which to use a Java 448 * (non-native) version of arraycopy() instead of the native 449 * version. See b/7103825. 450 */ 451 private static final int ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD = 32; 452 453 /** 454 * The char[] specialized version of arraycopy(). 455 * Note: This method is required for runtime ART compiler optimizations. 456 * Do not remove or change the signature. 457 */ 458 @SuppressWarnings("unused") 459 private static void arraycopy(char[] src, int srcPos, char[] dst, int dstPos, int length) { 460 if (src == null) { 461 throw new NullPointerException("src == null"); 462 } 463 if (dst == null) { 464 throw new NullPointerException("dst == null"); 465 } 466 if (srcPos < 0 || dstPos < 0 || length < 0 || 467 srcPos > src.length - length || dstPos > dst.length - length) { 468 throw new ArrayIndexOutOfBoundsException( 469 "src.length=" + src.length + " srcPos=" + srcPos + 470 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 471 } 472 if (length <= ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD) { 473 // Copy char by char for shorter arrays. 474 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 475 // Copy backward (to avoid overwriting elements before 476 // they are copied in case of an overlap on the same 477 // array.) 478 for (int i = length - 1; i >= 0; --i) { 479 dst[dstPos + i] = src[srcPos + i]; 480 } 481 } else { 482 // Copy forward. 483 for (int i = 0; i < length; ++i) { 484 dst[dstPos + i] = src[srcPos + i]; 485 } 486 } 487 } else { 488 // Call the native version for longer arrays. 489 arraycopyCharUnchecked(src, srcPos, dst, dstPos, length); 490 } 491 } 492 493 /** 494 * The char[] specialized, unchecked, native version of 495 * arraycopy(). This assumes error checking has been done. 496 */ 497 @FastNative 498 private static native void arraycopyCharUnchecked(char[] src, int srcPos, 499 char[] dst, int dstPos, int length); 500 501 /** 502 * The byte array length threshold below which to use a Java 503 * (non-native) version of arraycopy() instead of the native 504 * version. See b/7103825. 505 */ 506 private static final int ARRAYCOPY_SHORT_BYTE_ARRAY_THRESHOLD = 32; 507 508 /** 509 * The byte[] specialized version of arraycopy(). 510 * Note: This method is required for runtime ART compiler optimizations. 511 * Do not remove or change the signature. 512 * Note: Unlike the others, this variant is public due to a dependency we 513 * are working on removing. b/74103559 514 * 515 * @hide 516 */ 517 @SuppressWarnings("unused") 518 public static void arraycopy(byte[] src, int srcPos, byte[] dst, int dstPos, int length) { 519 if (src == null) { 520 throw new NullPointerException("src == null"); 521 } 522 if (dst == null) { 523 throw new NullPointerException("dst == null"); 524 } 525 if (srcPos < 0 || dstPos < 0 || length < 0 || 526 srcPos > src.length - length || dstPos > dst.length - length) { 527 throw new ArrayIndexOutOfBoundsException( 528 "src.length=" + src.length + " srcPos=" + srcPos + 529 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 530 } 531 if (length <= ARRAYCOPY_SHORT_BYTE_ARRAY_THRESHOLD) { 532 // Copy byte by byte for shorter arrays. 533 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 534 // Copy backward (to avoid overwriting elements before 535 // they are copied in case of an overlap on the same 536 // array.) 537 for (int i = length - 1; i >= 0; --i) { 538 dst[dstPos + i] = src[srcPos + i]; 539 } 540 } else { 541 // Copy forward. 542 for (int i = 0; i < length; ++i) { 543 dst[dstPos + i] = src[srcPos + i]; 544 } 545 } 546 } else { 547 // Call the native version for longer arrays. 548 arraycopyByteUnchecked(src, srcPos, dst, dstPos, length); 549 } 550 } 551 552 /** 553 * The byte[] specialized, unchecked, native version of 554 * arraycopy(). This assumes error checking has been done. 555 */ 556 @FastNative 557 private static native void arraycopyByteUnchecked(byte[] src, int srcPos, 558 byte[] dst, int dstPos, int length); 559 560 /** 561 * The short array length threshold below which to use a Java 562 * (non-native) version of arraycopy() instead of the native 563 * version. See b/7103825. 564 */ 565 private static final int ARRAYCOPY_SHORT_SHORT_ARRAY_THRESHOLD = 32; 566 567 /** 568 * The short[] specialized version of arraycopy(). 569 * Note: This method is required for runtime ART compiler optimizations. 570 * Do not remove or change the signature. 571 */ 572 @SuppressWarnings("unused") 573 private static void arraycopy(short[] src, int srcPos, short[] dst, int dstPos, int length) { 574 if (src == null) { 575 throw new NullPointerException("src == null"); 576 } 577 if (dst == null) { 578 throw new NullPointerException("dst == null"); 579 } 580 if (srcPos < 0 || dstPos < 0 || length < 0 || 581 srcPos > src.length - length || dstPos > dst.length - length) { 582 throw new ArrayIndexOutOfBoundsException( 583 "src.length=" + src.length + " srcPos=" + srcPos + 584 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 585 } 586 if (length <= ARRAYCOPY_SHORT_SHORT_ARRAY_THRESHOLD) { 587 // Copy short by short for shorter arrays. 588 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 589 // Copy backward (to avoid overwriting elements before 590 // they are copied in case of an overlap on the same 591 // array.) 592 for (int i = length - 1; i >= 0; --i) { 593 dst[dstPos + i] = src[srcPos + i]; 594 } 595 } else { 596 // Copy forward. 597 for (int i = 0; i < length; ++i) { 598 dst[dstPos + i] = src[srcPos + i]; 599 } 600 } 601 } else { 602 // Call the native version for longer arrays. 603 arraycopyShortUnchecked(src, srcPos, dst, dstPos, length); 604 } 605 } 606 607 /** 608 * The short[] specialized, unchecked, native version of 609 * arraycopy(). This assumes error checking has been done. 610 */ 611 @FastNative 612 private static native void arraycopyShortUnchecked(short[] src, int srcPos, 613 short[] dst, int dstPos, int length); 614 615 /** 616 * The short array length threshold below which to use a Java 617 * (non-native) version of arraycopy() instead of the native 618 * version. See b/7103825. 619 */ 620 private static final int ARRAYCOPY_SHORT_INT_ARRAY_THRESHOLD = 32; 621 622 /** 623 * The int[] specialized version of arraycopy(). 624 * Note: This method is required for runtime ART compiler optimizations. 625 * Do not remove or change the signature. 626 */ 627 @SuppressWarnings("unused") 628 private static void arraycopy(int[] src, int srcPos, int[] dst, int dstPos, int length) { 629 if (src == null) { 630 throw new NullPointerException("src == null"); 631 } 632 if (dst == null) { 633 throw new NullPointerException("dst == null"); 634 } 635 if (srcPos < 0 || dstPos < 0 || length < 0 || 636 srcPos > src.length - length || dstPos > dst.length - length) { 637 throw new ArrayIndexOutOfBoundsException( 638 "src.length=" + src.length + " srcPos=" + srcPos + 639 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 640 } 641 if (length <= ARRAYCOPY_SHORT_INT_ARRAY_THRESHOLD) { 642 // Copy int by int for shorter arrays. 643 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 644 // Copy backward (to avoid overwriting elements before 645 // they are copied in case of an overlap on the same 646 // array.) 647 for (int i = length - 1; i >= 0; --i) { 648 dst[dstPos + i] = src[srcPos + i]; 649 } 650 } else { 651 // Copy forward. 652 for (int i = 0; i < length; ++i) { 653 dst[dstPos + i] = src[srcPos + i]; 654 } 655 } 656 } else { 657 // Call the native version for longer arrays. 658 arraycopyIntUnchecked(src, srcPos, dst, dstPos, length); 659 } 660 } 661 662 /** 663 * The int[] specialized, unchecked, native version of 664 * arraycopy(). This assumes error checking has been done. 665 */ 666 @FastNative 667 private static native void arraycopyIntUnchecked(int[] src, int srcPos, 668 int[] dst, int dstPos, int length); 669 670 /** 671 * The short array length threshold below which to use a Java 672 * (non-native) version of arraycopy() instead of the native 673 * version. See b/7103825. 674 */ 675 private static final int ARRAYCOPY_SHORT_LONG_ARRAY_THRESHOLD = 32; 676 677 /** 678 * The long[] specialized version of arraycopy(). 679 * Note: This method is required for runtime ART compiler optimizations. 680 * Do not remove or change the signature. 681 */ 682 @SuppressWarnings("unused") 683 private static void arraycopy(long[] src, int srcPos, long[] dst, int dstPos, int length) { 684 if (src == null) { 685 throw new NullPointerException("src == null"); 686 } 687 if (dst == null) { 688 throw new NullPointerException("dst == null"); 689 } 690 if (srcPos < 0 || dstPos < 0 || length < 0 || 691 srcPos > src.length - length || dstPos > dst.length - length) { 692 throw new ArrayIndexOutOfBoundsException( 693 "src.length=" + src.length + " srcPos=" + srcPos + 694 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 695 } 696 if (length <= ARRAYCOPY_SHORT_LONG_ARRAY_THRESHOLD) { 697 // Copy long by long for shorter arrays. 698 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 699 // Copy backward (to avoid overwriting elements before 700 // they are copied in case of an overlap on the same 701 // array.) 702 for (int i = length - 1; i >= 0; --i) { 703 dst[dstPos + i] = src[srcPos + i]; 704 } 705 } else { 706 // Copy forward. 707 for (int i = 0; i < length; ++i) { 708 dst[dstPos + i] = src[srcPos + i]; 709 } 710 } 711 } else { 712 // Call the native version for longer arrays. 713 arraycopyLongUnchecked(src, srcPos, dst, dstPos, length); 714 } 715 } 716 717 /** 718 * The long[] specialized, unchecked, native version of 719 * arraycopy(). This assumes error checking has been done. 720 */ 721 @FastNative 722 private static native void arraycopyLongUnchecked(long[] src, int srcPos, 723 long[] dst, int dstPos, int length); 724 725 /** 726 * The short array length threshold below which to use a Java 727 * (non-native) version of arraycopy() instead of the native 728 * version. See b/7103825. 729 */ 730 private static final int ARRAYCOPY_SHORT_FLOAT_ARRAY_THRESHOLD = 32; 731 732 /** 733 * The float[] specialized version of arraycopy(). 734 * Note: This method is required for runtime ART compiler optimizations. 735 * Do not remove or change the signature. 736 */ 737 @SuppressWarnings("unused") 738 private static void arraycopy(float[] src, int srcPos, float[] dst, int dstPos, int length) { 739 if (src == null) { 740 throw new NullPointerException("src == null"); 741 } 742 if (dst == null) { 743 throw new NullPointerException("dst == null"); 744 } 745 if (srcPos < 0 || dstPos < 0 || length < 0 || 746 srcPos > src.length - length || dstPos > dst.length - length) { 747 throw new ArrayIndexOutOfBoundsException( 748 "src.length=" + src.length + " srcPos=" + srcPos + 749 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 750 } 751 if (length <= ARRAYCOPY_SHORT_FLOAT_ARRAY_THRESHOLD) { 752 // Copy float by float for shorter arrays. 753 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 754 // Copy backward (to avoid overwriting elements before 755 // they are copied in case of an overlap on the same 756 // array.) 757 for (int i = length - 1; i >= 0; --i) { 758 dst[dstPos + i] = src[srcPos + i]; 759 } 760 } else { 761 // Copy forward. 762 for (int i = 0; i < length; ++i) { 763 dst[dstPos + i] = src[srcPos + i]; 764 } 765 } 766 } else { 767 // Call the native version for floater arrays. 768 arraycopyFloatUnchecked(src, srcPos, dst, dstPos, length); 769 } 770 } 771 772 /** 773 * The float[] specialized, unchecked, native version of 774 * arraycopy(). This assumes error checking has been done. 775 */ 776 @FastNative 777 private static native void arraycopyFloatUnchecked(float[] src, int srcPos, 778 float[] dst, int dstPos, int length); 779 780 /** 781 * The short array length threshold below which to use a Java 782 * (non-native) version of arraycopy() instead of the native 783 * version. See b/7103825. 784 */ 785 private static final int ARRAYCOPY_SHORT_DOUBLE_ARRAY_THRESHOLD = 32; 786 787 /** 788 * The double[] specialized version of arraycopy(). 789 * Note: This method is required for runtime ART compiler optimizations. 790 * Do not remove or change the signature. 791 */ 792 @SuppressWarnings("unused") 793 private static void arraycopy(double[] src, int srcPos, double[] dst, int dstPos, int length) { 794 if (src == null) { 795 throw new NullPointerException("src == null"); 796 } 797 if (dst == null) { 798 throw new NullPointerException("dst == null"); 799 } 800 if (srcPos < 0 || dstPos < 0 || length < 0 || 801 srcPos > src.length - length || dstPos > dst.length - length) { 802 throw new ArrayIndexOutOfBoundsException( 803 "src.length=" + src.length + " srcPos=" + srcPos + 804 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 805 } 806 if (length <= ARRAYCOPY_SHORT_DOUBLE_ARRAY_THRESHOLD) { 807 // Copy double by double for shorter arrays. 808 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 809 // Copy backward (to avoid overwriting elements before 810 // they are copied in case of an overlap on the same 811 // array.) 812 for (int i = length - 1; i >= 0; --i) { 813 dst[dstPos + i] = src[srcPos + i]; 814 } 815 } else { 816 // Copy forward. 817 for (int i = 0; i < length; ++i) { 818 dst[dstPos + i] = src[srcPos + i]; 819 } 820 } 821 } else { 822 // Call the native version for floater arrays. 823 arraycopyDoubleUnchecked(src, srcPos, dst, dstPos, length); 824 } 825 } 826 827 /** 828 * The double[] specialized, unchecked, native version of 829 * arraycopy(). This assumes error checking has been done. 830 */ 831 @FastNative 832 private static native void arraycopyDoubleUnchecked(double[] src, int srcPos, 833 double[] dst, int dstPos, int length); 834 835 /** 836 * The short array length threshold below which to use a Java 837 * (non-native) version of arraycopy() instead of the native 838 * version. See b/7103825. 839 */ 840 private static final int ARRAYCOPY_SHORT_BOOLEAN_ARRAY_THRESHOLD = 32; 841 842 /** 843 * The boolean[] specialized version of arraycopy(). 844 * Note: This method is required for runtime ART compiler optimizations. 845 * Do not remove or change the signature. 846 */ 847 @SuppressWarnings("unused") 848 private static void arraycopy(boolean[] src, int srcPos, boolean[] dst, int dstPos, int length) { 849 if (src == null) { 850 throw new NullPointerException("src == null"); 851 } 852 if (dst == null) { 853 throw new NullPointerException("dst == null"); 854 } 855 if (srcPos < 0 || dstPos < 0 || length < 0 || 856 srcPos > src.length - length || dstPos > dst.length - length) { 857 throw new ArrayIndexOutOfBoundsException( 858 "src.length=" + src.length + " srcPos=" + srcPos + 859 " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length); 860 } 861 if (length <= ARRAYCOPY_SHORT_BOOLEAN_ARRAY_THRESHOLD) { 862 // Copy boolean by boolean for shorter arrays. 863 if (src == dst && srcPos < dstPos && dstPos < srcPos + length) { 864 // Copy backward (to avoid overwriting elements before 865 // they are copied in case of an overlap on the same 866 // array.) 867 for (int i = length - 1; i >= 0; --i) { 868 dst[dstPos + i] = src[srcPos + i]; 869 } 870 } else { 871 // Copy forward. 872 for (int i = 0; i < length; ++i) { 873 dst[dstPos + i] = src[srcPos + i]; 874 } 875 } 876 } else { 877 // Call the native version for floater arrays. 878 arraycopyBooleanUnchecked(src, srcPos, dst, dstPos, length); 879 } 880 } 881 882 /** 883 * The boolean[] specialized, unchecked, native version of 884 * arraycopy(). This assumes error checking has been done. 885 */ 886 @FastNative 887 private static native void arraycopyBooleanUnchecked(boolean[] src, int srcPos, 888 boolean[] dst, int dstPos, int length); 889 // END Android-changed 890 891 /** 892 * Returns the same hash code for the given object as 893 * would be returned by the default method hashCode(), 894 * whether or not the given object's class overrides 895 * hashCode(). 896 * The hash code for the null reference is zero. 897 * 898 * @param x object for which the hashCode is to be calculated 899 * @return the hashCode 900 * @since JDK1.1 901 */ 902 public static int identityHashCode(Object x) { 903 if (x == null) { 904 return 0; 905 } 906 return Object.identityHashCode(x); 907 } 908 909 /** 910 * System properties. The following properties are guaranteed to be defined: 911 * <dl> 912 * <dt>java.version <dd>Java version number 913 * <dt>java.vendor <dd>Java vendor specific string 914 * <dt>java.vendor.url <dd>Java vendor URL 915 * <dt>java.home <dd>Java installation directory 916 * <dt>java.class.version <dd>Java class version number 917 * <dt>java.class.path <dd>Java classpath 918 * <dt>os.name <dd>Operating System Name 919 * <dt>os.arch <dd>Operating System Architecture 920 * <dt>os.version <dd>Operating System Version 921 * <dt>file.separator <dd>File separator ("/" on Unix) 922 * <dt>path.separator <dd>Path separator (":" on Unix) 923 * <dt>line.separator <dd>Line separator ("\n" on Unix) 924 * <dt>user.name <dd>User account name 925 * <dt>user.home <dd>User home directory 926 * <dt>user.dir <dd>User's current working directory 927 * </dl> 928 */ 929 930 private static Properties props; 931 private static Properties unchangeableProps; 932 933 private static native String[] specialProperties(); 934 935 static final class PropertiesWithNonOverrideableDefaults extends Properties { 936 PropertiesWithNonOverrideableDefaults(Properties defaults) { 937 super(defaults); 938 } 939 940 @Override 941 public Object put(Object key, Object value) { 942 if (defaults.containsKey(key)) { 943 logE("Ignoring attempt to set property \"" + key + 944 "\" to value \"" + value + "\"."); 945 return defaults.get(key); 946 } 947 948 return super.put(key, value); 949 } 950 951 @Override 952 public Object remove(Object key) { 953 if (defaults.containsKey(key)) { 954 logE("Ignoring attempt to remove property \"" + key + "\"."); 955 return null; 956 } 957 958 return super.remove(key); 959 } 960 } 961 962 private static void parsePropertyAssignments(Properties p, String[] assignments) { 963 for (String assignment : assignments) { 964 int split = assignment.indexOf('='); 965 String key = assignment.substring(0, split); 966 String value = assignment.substring(split + 1); 967 p.put(key, value); 968 } 969 } 970 971 private static Properties initUnchangeableSystemProperties() { 972 VMRuntime runtime = VMRuntime.getRuntime(); 973 Properties p = new Properties(); 974 975 // Set non-static properties. 976 p.put("java.boot.class.path", runtime.bootClassPath()); 977 p.put("java.class.path", runtime.classPath()); 978 979 // TODO: does this make any sense? Should we just leave java.home unset? 980 String javaHome = getenv("JAVA_HOME"); 981 if (javaHome == null) { 982 javaHome = "/system"; 983 } 984 p.put("java.home", javaHome); 985 986 p.put("java.vm.version", runtime.vmVersion()); 987 988 try { 989 StructPasswd passwd = Libcore.os.getpwuid(Libcore.os.getuid()); 990 p.put("user.name", passwd.pw_name); 991 } catch (ErrnoException exception) { 992 throw new AssertionError(exception); 993 } 994 995 StructUtsname info = Libcore.os.uname(); 996 p.put("os.arch", info.machine); 997 if (p.get("os.name") != null && !p.get("os.name").equals(info.sysname)) { 998 logE("Wrong compile-time assumption for os.name: " + p.get("os.name") + " vs " + 999 info.sysname); 1000 p.put("os.name", info.sysname); 1001 } 1002 p.put("os.version", info.release); 1003 1004 // Android-added: Undocumented properties that exist only on Android. 1005 p.put("android.icu.library.version", ICU.getIcuVersion()); 1006 p.put("android.icu.unicode.version", ICU.getUnicodeVersion()); 1007 p.put("android.icu.cldr.version", ICU.getCldrVersion()); 1008 1009 // Property override for ICU4J : this is the location of the ICU4C data. This 1010 // is prioritized over the properties in ICUConfig.properties. The issue with using 1011 // that is that it doesn't play well with jarjar and it needs complicated build rules 1012 // to change its default value. 1013 String icuDataPath = TimeZoneDataFiles.generateIcuDataPath(); 1014 p.put("android.icu.impl.ICUBinary.dataPath", icuDataPath); 1015 1016 parsePropertyAssignments(p, specialProperties()); 1017 1018 // Override built-in properties with settings from the command line. 1019 // Note: it is not possible to override hardcoded values. 1020 parsePropertyAssignments(p, runtime.properties()); 1021 1022 1023 // Set static hardcoded properties. 1024 // These come last, as they must be guaranteed to agree with what a backend compiler 1025 // may assume when compiling the boot image on Android. 1026 for (String[] pair : AndroidHardcodedSystemProperties.STATIC_PROPERTIES) { 1027 if (p.containsKey(pair[0])) { 1028 logE("Ignoring command line argument: -D" + pair[0]); 1029 } 1030 if (pair[1] == null) { 1031 p.remove(pair[0]); 1032 } else { 1033 p.put(pair[0], pair[1]); 1034 } 1035 } 1036 1037 return p; 1038 } 1039 1040 private static Properties initProperties() { 1041 Properties p = new PropertiesWithNonOverrideableDefaults(unchangeableProps); 1042 setDefaultChangeableProperties(p); 1043 return p; 1044 } 1045 1046 private static Properties setDefaultChangeableProperties(Properties p) { 1047 // On Android, each app gets its own temporary directory. 1048 // (See android.app.ActivityThread.) This is just a fallback default, 1049 // useful only on the host. 1050 // We check first if the property has not been set already: note that it 1051 // can only be set from the command line through the '-Djava.io.tmpdir=' option. 1052 if (!unchangeableProps.containsKey("java.io.tmpdir")) { 1053 p.put("java.io.tmpdir", "/tmp"); 1054 } 1055 1056 // Android has always had an empty "user.home" (see docs for getProperty). 1057 // This is not useful for normal android apps which need to use android specific 1058 // APIs such as {@code Context.getFilesDir} and {@code Context.getCacheDir} but 1059 // we make it changeable for backward compatibility, so that they can change it 1060 // to a writeable location if required. 1061 // We check first if the property has not been set already: note that it 1062 // can only be set from the command line through the '-Duser.home=' option. 1063 if (!unchangeableProps.containsKey("user.home")) { 1064 p.put("user.home", ""); 1065 } 1066 1067 return p; 1068 } 1069 1070 /** 1071 * Inits an unchangeable system property with the given value. 1072 * 1073 * This is called from native code when the environment needs to change under native 1074 * bridge emulation. 1075 * 1076 * @hide also visible for tests. 1077 */ 1078 public static void setUnchangeableSystemProperty(String key, String value) { 1079 checkKey(key); 1080 unchangeableProps.put(key, value); 1081 } 1082 1083 private static void addLegacyLocaleSystemProperties() { 1084 final String locale = getProperty("user.locale", ""); 1085 if (!locale.isEmpty()) { 1086 Locale l = Locale.forLanguageTag(locale); 1087 setUnchangeableSystemProperty("user.language", l.getLanguage()); 1088 setUnchangeableSystemProperty("user.region", l.getCountry()); 1089 setUnchangeableSystemProperty("user.variant", l.getVariant()); 1090 } else { 1091 // If "user.locale" isn't set we fall back to our old defaults of 1092 // language="en" and region="US" (if unset) and don't attempt to set it. 1093 // The Locale class will fall back to using user.language and 1094 // user.region if unset. 1095 final String language = getProperty("user.language", ""); 1096 final String region = getProperty("user.region", ""); 1097 1098 if (language.isEmpty()) { 1099 setUnchangeableSystemProperty("user.language", "en"); 1100 } 1101 1102 if (region.isEmpty()) { 1103 setUnchangeableSystemProperty("user.region", "US"); 1104 } 1105 } 1106 } 1107 1108 /** 1109 * Determines the current system properties. 1110 * 1111 * 1112 * <p>The following properties are always provided by the Dalvik VM:</p> 1113 * <p><table BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""> 1114 * <tr BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"> 1115 * <td><b>Name</b></td> <td><b>Meaning</b></td> <td><b>Example</b></td></tr> 1116 * <tr><td>file.separator</td> <td>{@link java.io.File#separator}</td> <td>{@code /}</td></tr> 1117 * 1118 * <tr><td>java.class.path</td> <td>System class path</td> <td>{@code .}</td></tr> 1119 * <tr><td>java.class.version</td> <td>(Not useful on Android)</td> <td>{@code 50.0}</td></tr> 1120 * <tr><td>java.compiler</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1121 * <tr><td>java.ext.dirs</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1122 * <tr><td>java.home</td> <td>Location of the VM on the file system</td> <td>{@code /system}</td></tr> 1123 * <tr><td>java.io.tmpdir</td> <td>See {@link java.io.File#createTempFile}</td> <td>{@code /sdcard}</td></tr> 1124 * <tr><td>java.library.path</td> <td>Search path for JNI libraries</td> <td>{@code /vendor/lib:/system/lib}</td></tr> 1125 * <tr><td>java.vendor</td> <td>Human-readable VM vendor</td> <td>{@code The Android Project}</td></tr> 1126 * <tr><td>java.vendor.url</td> <td>URL for VM vendor's web site</td> <td>{@code http://www.android.com/}</td></tr> 1127 * <tr><td>java.version</td> <td>(Not useful on Android)</td> <td>{@code 0}</td></tr> 1128 * 1129 * <tr><td>java.specification.version</td> <td>VM libraries version</td> <td>{@code 0.9}</td></tr> 1130 * <tr><td>java.specification.vendor</td> <td>VM libraries vendor</td> <td>{@code The Android Project}</td></tr> 1131 * <tr><td>java.specification.name</td> <td>VM libraries name</td> <td>{@code Dalvik Core Library}</td></tr> 1132 * <tr><td>java.vm.version</td> <td>VM implementation version</td> <td>{@code 1.2.0}</td></tr> 1133 * <tr><td>java.vm.vendor</td> <td>VM implementation vendor</td> <td>{@code The Android Project}</td></tr> 1134 * <tr><td>java.vm.name</td> <td>VM implementation name</td> <td>{@code Dalvik}</td></tr> 1135 * <tr><td>java.vm.specification.version</td> <td>VM specification version</td> <td>{@code 0.9}</td></tr> 1136 * <tr><td>java.vm.specification.vendor</td> <td>VM specification vendor</td> <td>{@code The Android Project}</td></tr> 1137 * <tr><td>java.vm.specification.name</td> <td>VM specification name</td> <td>{@code Dalvik Virtual Machine Specification}</td></tr> 1138 * 1139 * <tr><td>line.separator</td> <td>The system line separator</td> <td>{@code \n}</td></tr> 1140 * 1141 * <tr><td>os.arch</td> <td>OS architecture</td> <td>{@code armv7l}</td></tr> 1142 * <tr><td>os.name</td> <td>OS (kernel) name</td> <td>{@code Linux}</td></tr> 1143 * <tr><td>os.version</td> <td>OS (kernel) version</td> <td>{@code 2.6.32.9-g103d848}</td></tr> 1144 * 1145 * <tr><td>path.separator</td> <td>See {@link java.io.File#pathSeparator}</td> <td>{@code :}</td></tr> 1146 * 1147 * <tr><td>user.dir</td> <td>Base of non-absolute paths</td> <td>{@code /}</td></tr> 1148 * <tr><td>user.home</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1149 * <tr><td>user.name</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1150 * 1151 * </table> 1152 * <p> 1153 * Multiple paths in a system property value are separated by the path 1154 * separator character of the platform. 1155 * <p> 1156 * Note that even if the security manager does not permit the 1157 * <code>getProperties</code> operation, it may choose to permit the 1158 * {@link #getProperty(String)} operation. 1159 * 1160 * @return the system properties 1161 * @exception SecurityException if a security manager exists and its 1162 * <code>checkPropertiesAccess</code> method doesn't allow access 1163 * to the system properties. 1164 * @see #setProperties 1165 * @see java.lang.SecurityException 1166 * @see java.lang.SecurityManager#checkPropertiesAccess() 1167 * @see java.util.Properties 1168 */ 1169 public static Properties getProperties() { 1170 SecurityManager sm = getSecurityManager(); 1171 if (sm != null) { 1172 sm.checkPropertiesAccess(); 1173 } 1174 1175 return props; 1176 } 1177 1178 /** 1179 * Returns the system-dependent line separator string. It always 1180 * returns the same value - the initial value of the {@linkplain 1181 * #getProperty(String) system property} {@code line.separator}. 1182 * 1183 * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft 1184 * Windows systems it returns {@code "\r\n"}. 1185 * 1186 * @return the system-dependent line separator string 1187 * @since 1.7 1188 */ 1189 public static String lineSeparator() { 1190 return lineSeparator; 1191 } 1192 1193 private static String lineSeparator; 1194 1195 1196 // Comment replaced with android one. 1197 /** 1198 * Attempts to set all system properties. Copies all properties from 1199 * {@code p} and discards system properties that are read only and cannot 1200 * be modified. See {@link #getProperty} for a list of such properties. 1201 */ 1202 public static void setProperties(Properties props) { 1203 Properties baseProperties = new PropertiesWithNonOverrideableDefaults(unchangeableProps); 1204 if (props != null) { 1205 baseProperties.putAll(props); 1206 } else { 1207 setDefaultChangeableProperties(baseProperties); 1208 } 1209 1210 System.props = baseProperties; 1211 } 1212 1213 /** 1214 * Gets the system property indicated by the specified key. 1215 * <p> 1216 * First, if there is a security manager, its 1217 * <code>checkPropertyAccess</code> method is called with the key as 1218 * its argument. This may result in a SecurityException. 1219 * <p> 1220 * If there is no current set of system properties, a set of system 1221 * properties is first created and initialized in the same manner as 1222 * for the <code>getProperties</code> method. 1223 * 1224 * @param key the name of the system property. 1225 * @return the string value of the system property, 1226 * or <code>null</code> if there is no property with that key. 1227 * 1228 * @exception SecurityException if a security manager exists and its 1229 * <code>checkPropertyAccess</code> method doesn't allow 1230 * access to the specified system property. 1231 * @exception NullPointerException if <code>key</code> is 1232 * <code>null</code>. 1233 * @exception IllegalArgumentException if <code>key</code> is empty. 1234 * @see #setProperty 1235 * @see java.lang.SecurityException 1236 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) 1237 * @see java.lang.System#getProperties() 1238 */ 1239 public static String getProperty(String key) { 1240 checkKey(key); 1241 SecurityManager sm = getSecurityManager(); 1242 if (sm != null) { 1243 sm.checkPropertyAccess(key); 1244 } 1245 1246 return props.getProperty(key); 1247 } 1248 1249 /** 1250 * Gets the system property indicated by the specified key. 1251 * <p> 1252 * First, if there is a security manager, its 1253 * <code>checkPropertyAccess</code> method is called with the 1254 * <code>key</code> as its argument. 1255 * <p> 1256 * If there is no current set of system properties, a set of system 1257 * properties is first created and initialized in the same manner as 1258 * for the <code>getProperties</code> method. 1259 * 1260 * @param key the name of the system property. 1261 * @param def a default value. 1262 * @return the string value of the system property, 1263 * or the default value if there is no property with that key. 1264 * 1265 * @exception SecurityException if a security manager exists and its 1266 * <code>checkPropertyAccess</code> method doesn't allow 1267 * access to the specified system property. 1268 * @exception NullPointerException if <code>key</code> is 1269 * <code>null</code>. 1270 * @exception IllegalArgumentException if <code>key</code> is empty. 1271 * @see #setProperty 1272 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) 1273 * @see java.lang.System#getProperties() 1274 */ 1275 public static String getProperty(String key, String def) { 1276 checkKey(key); 1277 SecurityManager sm = getSecurityManager(); 1278 if (sm != null) { 1279 sm.checkPropertyAccess(key); 1280 } 1281 1282 return props.getProperty(key, def); 1283 } 1284 1285 /** 1286 * Sets the system property indicated by the specified key. 1287 * <p> 1288 * First, if a security manager exists, its 1289 * <code>SecurityManager.checkPermission</code> method 1290 * is called with a <code>PropertyPermission(key, "write")</code> 1291 * permission. This may result in a SecurityException being thrown. 1292 * If no exception is thrown, the specified property is set to the given 1293 * value. 1294 * <p> 1295 * 1296 * @param key the name of the system property. 1297 * @param value the value of the system property. 1298 * @return the previous value of the system property, 1299 * or <code>null</code> if it did not have one. 1300 * 1301 * @exception SecurityException if a security manager exists and its 1302 * <code>checkPermission</code> method doesn't allow 1303 * setting of the specified property. 1304 * @exception NullPointerException if <code>key</code> or 1305 * <code>value</code> is <code>null</code>. 1306 * @exception IllegalArgumentException if <code>key</code> is empty. 1307 * @see #getProperty 1308 * @see java.lang.System#getProperty(java.lang.String) 1309 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 1310 * @see java.util.PropertyPermission 1311 * @see SecurityManager#checkPermission 1312 * @since 1.2 1313 */ 1314 public static String setProperty(String key, String value) { 1315 checkKey(key); 1316 SecurityManager sm = getSecurityManager(); 1317 if (sm != null) { 1318 sm.checkPermission(new PropertyPermission(key, 1319 SecurityConstants.PROPERTY_WRITE_ACTION)); 1320 } 1321 1322 return (String) props.setProperty(key, value); 1323 } 1324 1325 /** 1326 * Removes the system property indicated by the specified key. 1327 * <p> 1328 * First, if a security manager exists, its 1329 * <code>SecurityManager.checkPermission</code> method 1330 * is called with a <code>PropertyPermission(key, "write")</code> 1331 * permission. This may result in a SecurityException being thrown. 1332 * If no exception is thrown, the specified property is removed. 1333 * <p> 1334 * 1335 * @param key the name of the system property to be removed. 1336 * @return the previous string value of the system property, 1337 * or <code>null</code> if there was no property with that key. 1338 * 1339 * @exception SecurityException if a security manager exists and its 1340 * <code>checkPropertyAccess</code> method doesn't allow 1341 * access to the specified system property. 1342 * @exception NullPointerException if <code>key</code> is 1343 * <code>null</code>. 1344 * @exception IllegalArgumentException if <code>key</code> is empty. 1345 * @see #getProperty 1346 * @see #setProperty 1347 * @see java.util.Properties 1348 * @see java.lang.SecurityException 1349 * @see java.lang.SecurityManager#checkPropertiesAccess() 1350 * @since 1.5 1351 */ 1352 public static String clearProperty(String key) { 1353 checkKey(key); 1354 SecurityManager sm = getSecurityManager(); 1355 if (sm != null) { 1356 sm.checkPermission(new PropertyPermission(key, "write")); 1357 } 1358 1359 return (String) props.remove(key); 1360 } 1361 1362 private static void checkKey(String key) { 1363 if (key == null) { 1364 throw new NullPointerException("key can't be null"); 1365 } 1366 if (key.equals("")) { 1367 throw new IllegalArgumentException("key can't be empty"); 1368 } 1369 } 1370 1371 /** 1372 * Gets the value of the specified environment variable. An 1373 * environment variable is a system-dependent external named 1374 * value. 1375 * 1376 * <p>If a security manager exists, its 1377 * {@link SecurityManager#checkPermission checkPermission} 1378 * method is called with a 1379 * <code>{@link RuntimePermission}("getenv."+name)</code> 1380 * permission. This may result in a {@link SecurityException} 1381 * being thrown. If no exception is thrown the value of the 1382 * variable <code>name</code> is returned. 1383 * 1384 * <p><a name="EnvironmentVSSystemProperties"><i>System 1385 * properties</i> and <i>environment variables</i></a> are both 1386 * conceptually mappings between names and values. Both 1387 * mechanisms can be used to pass user-defined information to a 1388 * Java process. Environment variables have a more global effect, 1389 * because they are visible to all descendants of the process 1390 * which defines them, not just the immediate Java subprocess. 1391 * They can have subtly different semantics, such as case 1392 * insensitivity, on different operating systems. For these 1393 * reasons, environment variables are more likely to have 1394 * unintended side effects. It is best to use system properties 1395 * where possible. Environment variables should be used when a 1396 * global effect is desired, or when an external system interface 1397 * requires an environment variable (such as <code>PATH</code>). 1398 * 1399 * <p>On UNIX systems the alphabetic case of <code>name</code> is 1400 * typically significant, while on Microsoft Windows systems it is 1401 * typically not. For example, the expression 1402 * <code>System.getenv("FOO").equals(System.getenv("foo"))</code> 1403 * is likely to be true on Microsoft Windows. 1404 * 1405 * @param name the name of the environment variable 1406 * @return the string value of the variable, or <code>null</code> 1407 * if the variable is not defined in the system environment 1408 * @throws NullPointerException if <code>name</code> is <code>null</code> 1409 * @throws SecurityException 1410 * if a security manager exists and its 1411 * {@link SecurityManager#checkPermission checkPermission} 1412 * method doesn't allow access to the environment variable 1413 * <code>name</code> 1414 * @see #getenv() 1415 * @see ProcessBuilder#environment() 1416 */ 1417 public static String getenv(String name) { 1418 if (name == null) { 1419 throw new NullPointerException("name == null"); 1420 } 1421 1422 return Libcore.os.getenv(name); 1423 } 1424 1425 1426 /** 1427 * Returns an unmodifiable string map view of the current system environment. 1428 * The environment is a system-dependent mapping from names to 1429 * values which is passed from parent to child processes. 1430 * 1431 * <p>If the system does not support environment variables, an 1432 * empty map is returned. 1433 * 1434 * <p>The returned map will never contain null keys or values. 1435 * Attempting to query the presence of a null key or value will 1436 * throw a {@link NullPointerException}. Attempting to query 1437 * the presence of a key or value which is not of type 1438 * {@link String} will throw a {@link ClassCastException}. 1439 * 1440 * <p>The returned map and its collection views may not obey the 1441 * general contract of the {@link Object#equals} and 1442 * {@link Object#hashCode} methods. 1443 * 1444 * <p>The returned map is typically case-sensitive on all platforms. 1445 * 1446 * <p>If a security manager exists, its 1447 * {@link SecurityManager#checkPermission checkPermission} 1448 * method is called with a 1449 * <code>{@link RuntimePermission}("getenv.*")</code> 1450 * permission. This may result in a {@link SecurityException} being 1451 * thrown. 1452 * 1453 * <p>When passing information to a Java subprocess, 1454 * <a href=#EnvironmentVSSystemProperties>system properties</a> 1455 * are generally preferred over environment variables. 1456 * 1457 * @return the environment as a map of variable names to values 1458 * @throws SecurityException 1459 * if a security manager exists and its 1460 * {@link SecurityManager#checkPermission checkPermission} 1461 * method doesn't allow access to the process environment 1462 * @see #getenv(String) 1463 * @see ProcessBuilder#environment() 1464 * @since 1.5 1465 */ 1466 public static java.util.Map<String,String> getenv() { 1467 SecurityManager sm = getSecurityManager(); 1468 if (sm != null) { 1469 sm.checkPermission(new RuntimePermission("getenv.*")); 1470 } 1471 1472 return ProcessEnvironment.getenv(); 1473 } 1474 1475 /** 1476 * Terminates the currently running Java Virtual Machine. The 1477 * argument serves as a status code; by convention, a nonzero status 1478 * code indicates abnormal termination. 1479 * <p> 1480 * This method calls the <code>exit</code> method in class 1481 * <code>Runtime</code>. This method never returns normally. 1482 * <p> 1483 * The call <code>System.exit(n)</code> is effectively equivalent to 1484 * the call: 1485 * <blockquote><pre> 1486 * Runtime.getRuntime().exit(n) 1487 * </pre></blockquote> 1488 * 1489 * @param status exit status. 1490 * @throws SecurityException 1491 * if a security manager exists and its <code>checkExit</code> 1492 * method doesn't allow exit with the specified status. 1493 * @see java.lang.Runtime#exit(int) 1494 */ 1495 public static void exit(int status) { 1496 Runtime.getRuntime().exit(status); 1497 } 1498 1499 /** 1500 * Runs the garbage collector. 1501 * <p> 1502 * Calling the <code>gc</code> method suggests that the Java Virtual 1503 * Machine expend effort toward recycling unused objects in order to 1504 * make the memory they currently occupy available for quick reuse. 1505 * When control returns from the method call, the Java Virtual 1506 * Machine has made a best effort to reclaim space from all discarded 1507 * objects. 1508 * <p> 1509 * The call <code>System.gc()</code> is effectively equivalent to the 1510 * call: 1511 * <blockquote><pre> 1512 * Runtime.getRuntime().gc() 1513 * </pre></blockquote> 1514 * 1515 * @see java.lang.Runtime#gc() 1516 */ 1517 public static void gc() { 1518 boolean shouldRunGC; 1519 synchronized (LOCK) { 1520 shouldRunGC = justRanFinalization; 1521 if (shouldRunGC) { 1522 justRanFinalization = false; 1523 } else { 1524 runGC = true; 1525 } 1526 } 1527 if (shouldRunGC) { 1528 Runtime.getRuntime().gc(); 1529 } 1530 } 1531 1532 /** 1533 * Runs the finalization methods of any objects pending finalization. 1534 * <p> 1535 * Calling this method suggests that the Java Virtual Machine expend 1536 * effort toward running the <code>finalize</code> methods of objects 1537 * that have been found to be discarded but whose <code>finalize</code> 1538 * methods have not yet been run. When control returns from the 1539 * method call, the Java Virtual Machine has made a best effort to 1540 * complete all outstanding finalizations. 1541 * <p> 1542 * The call <code>System.runFinalization()</code> is effectively 1543 * equivalent to the call: 1544 * <blockquote><pre> 1545 * Runtime.getRuntime().runFinalization() 1546 * </pre></blockquote> 1547 * 1548 * @see java.lang.Runtime#runFinalization() 1549 */ 1550 public static void runFinalization() { 1551 boolean shouldRunGC; 1552 synchronized (LOCK) { 1553 shouldRunGC = runGC; 1554 runGC = false; 1555 } 1556 if (shouldRunGC) { 1557 Runtime.getRuntime().gc(); 1558 } 1559 Runtime.getRuntime().runFinalization(); 1560 synchronized (LOCK) { 1561 justRanFinalization = true; 1562 } 1563 } 1564 1565 /** 1566 * Enable or disable finalization on exit; doing so specifies that the 1567 * finalizers of all objects that have finalizers that have not yet been 1568 * automatically invoked are to be run before the Java runtime exits. 1569 * By default, finalization on exit is disabled. 1570 * 1571 * <p>If there is a security manager, 1572 * its <code>checkExit</code> method is first called 1573 * with 0 as its argument to ensure the exit is allowed. 1574 * This could result in a SecurityException. 1575 * 1576 * @deprecated This method is inherently unsafe. It may result in 1577 * finalizers being called on live objects while other threads are 1578 * concurrently manipulating those objects, resulting in erratic 1579 * behavior or deadlock. 1580 * @param value indicating enabling or disabling of finalization 1581 * @throws SecurityException 1582 * if a security manager exists and its <code>checkExit</code> 1583 * method doesn't allow the exit. 1584 * 1585 * @see java.lang.Runtime#exit(int) 1586 * @see java.lang.Runtime#gc() 1587 * @see java.lang.SecurityManager#checkExit(int) 1588 * @since JDK1.1 1589 */ 1590 @Deprecated 1591 public static void runFinalizersOnExit(boolean value) { 1592 Runtime.runFinalizersOnExit(value); 1593 } 1594 1595 /** 1596 * Loads the native library specified by the filename argument. The filename 1597 * argument must be an absolute path name. 1598 * 1599 * If the filename argument, when stripped of any platform-specific library 1600 * prefix, path, and file extension, indicates a library whose name is, 1601 * for example, L, and a native library called L is statically linked 1602 * with the VM, then the JNI_OnLoad_L function exported by the library 1603 * is invoked rather than attempting to load a dynamic library. 1604 * A filename matching the argument does not have to exist in the 1605 * file system. 1606 * See the JNI Specification for more details. 1607 * 1608 * Otherwise, the filename argument is mapped to a native library image in 1609 * an implementation-dependent manner. 1610 * 1611 * <p> 1612 * The call <code>System.load(name)</code> is effectively equivalent 1613 * to the call: 1614 * <blockquote><pre> 1615 * Runtime.getRuntime().load(name) 1616 * </pre></blockquote> 1617 * 1618 * @param filename the file to load. 1619 * @exception SecurityException if a security manager exists and its 1620 * <code>checkLink</code> method doesn't allow 1621 * loading of the specified dynamic library 1622 * @exception UnsatisfiedLinkError if either the filename is not an 1623 * absolute path name, the native library is not statically 1624 * linked with the VM, or the library cannot be mapped to 1625 * a native library image by the host system. 1626 * @exception NullPointerException if <code>filename</code> is 1627 * <code>null</code> 1628 * @see java.lang.Runtime#load(java.lang.String) 1629 * @see java.lang.SecurityManager#checkLink(java.lang.String) 1630 */ 1631 @CallerSensitive 1632 public static void load(String filename) { 1633 Runtime.getRuntime().load0(VMStack.getStackClass1(), filename); 1634 } 1635 1636 /** 1637 * Loads the native library specified by the <code>libname</code> 1638 * argument. The <code>libname</code> argument must not contain any platform 1639 * specific prefix, file extension or path. If a native library 1640 * called <code>libname</code> is statically linked with the VM, then the 1641 * JNI_OnLoad_<code>libname</code> function exported by the library is invoked. 1642 * See the JNI Specification for more details. 1643 * 1644 * Otherwise, the libname argument is loaded from a system library 1645 * location and mapped to a native library image in an implementation- 1646 * dependent manner. 1647 * <p> 1648 * The call <code>System.loadLibrary(name)</code> is effectively 1649 * equivalent to the call 1650 * <blockquote><pre> 1651 * Runtime.getRuntime().loadLibrary(name) 1652 * </pre></blockquote> 1653 * 1654 * @param libname the name of the library. 1655 * @exception SecurityException if a security manager exists and its 1656 * <code>checkLink</code> method doesn't allow 1657 * loading of the specified dynamic library 1658 * @exception UnsatisfiedLinkError if either the libname argument 1659 * contains a file path, the native library is not statically 1660 * linked with the VM, or the library cannot be mapped to a 1661 * native library image by the host system. 1662 * @exception NullPointerException if <code>libname</code> is 1663 * <code>null</code> 1664 * @see java.lang.Runtime#loadLibrary(java.lang.String) 1665 * @see java.lang.SecurityManager#checkLink(java.lang.String) 1666 */ 1667 @CallerSensitive 1668 public static void loadLibrary(String libname) { 1669 Runtime.getRuntime().loadLibrary0(VMStack.getCallingClassLoader(), libname); 1670 } 1671 1672 /** 1673 * Maps a library name into a platform-specific string representing 1674 * a native library. 1675 * 1676 * @param libname the name of the library. 1677 * @return a platform-dependent native library name. 1678 * @exception NullPointerException if <code>libname</code> is 1679 * <code>null</code> 1680 * @see java.lang.System#loadLibrary(java.lang.String) 1681 * @see java.lang.ClassLoader#findLibrary(java.lang.String) 1682 * @since 1.2 1683 */ 1684 public static native String mapLibraryName(String libname); 1685 1686 /** 1687 * Create PrintStream for stdout/err based on encoding. 1688 */ 1689 private static PrintStream newPrintStream(FileOutputStream fos, String enc) { 1690 if (enc != null) { 1691 try { 1692 return new PrintStream(new BufferedOutputStream(fos, 128), true, enc); 1693 } catch (UnsupportedEncodingException uee) {} 1694 } 1695 return new PrintStream(new BufferedOutputStream(fos, 128), true); 1696 } 1697 1698 1699 /** 1700 * Initialize the system class. Called after thread initialization. 1701 */ 1702 static { 1703 unchangeableProps = initUnchangeableSystemProperties(); 1704 props = initProperties(); 1705 addLegacyLocaleSystemProperties(); 1706 sun.misc.Version.initSystemProperties(); 1707 1708 // TODO: Confirm that this isn't something super important. 1709 // sun.misc.VM.saveAndRemoveProperties(props); 1710 1711 lineSeparator = props.getProperty("line.separator"); 1712 1713 FileInputStream fdIn = new FileInputStream(FileDescriptor.in); 1714 FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out); 1715 FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err); 1716 // BEGIN Android-changed: lower buffer size. 1717 // in = new BufferedInputStream(fdIn); 1718 in = new BufferedInputStream(fdIn, 128); 1719 // END Android-changed: lower buffer size. 1720 out = newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")); 1721 err = newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")); 1722 1723 // Initialize any miscellenous operating system settings that need to be 1724 // set for the class libraries. Currently this is no-op everywhere except 1725 // for Windows where the process-wide error mode is set before the java.io 1726 // classes are used. 1727 sun.misc.VM.initializeOSEnvironment(); 1728 1729 // Subsystems that are invoked during initialization can invoke 1730 // sun.misc.VM.isBooted() in order to avoid doing things that should 1731 // wait until the application class loader has been set up. 1732 // IMPORTANT: Ensure that this remains the last initialization action! 1733 sun.misc.VM.booted(); 1734 } 1735 1736 /** 1737 * @hide internal use only 1738 */ 1739 public static void logE(String message) { 1740 log('E', message, null); 1741 } 1742 1743 /** 1744 * @hide internal use only 1745 */ 1746 public static void logE(String message, Throwable th) { 1747 log('E', message, th); 1748 } 1749 1750 /** 1751 * @hide internal use only 1752 */ 1753 public static void logI(String message) { 1754 log('I', message, null); 1755 } 1756 1757 /** 1758 * @hide internal use only 1759 */ 1760 public static void logI(String message, Throwable th) { 1761 log('I', message, th); 1762 } 1763 1764 /** 1765 * @hide internal use only 1766 */ 1767 public static void logW(String message) { 1768 log('W', message, null); 1769 } 1770 1771 /** 1772 * @hide internal use only 1773 */ 1774 public static void logW(String message, Throwable th) { 1775 log('W', message, th); 1776 } 1777 1778 private static native void log(char type, String message, Throwable th); 1779 } 1780