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 java.io.*; 34 import java.lang.annotation.Annotation; 35 import java.nio.channels.Channel; 36 import java.nio.channels.spi.SelectorProvider; 37 import java.util.Locale; 38 import java.util.Map; 39 import java.util.Properties; 40 import java.util.PropertyPermission; 41 import libcore.icu.ICU; 42 import libcore.io.Libcore; 43 import libcore.timezone.TimeZoneDataFiles; 44 45 import sun.reflect.CallerSensitive; 46 import sun.reflect.Reflection; 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 */ System()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 */ setIn(InputStream in)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 */ setOut(PrintStream out)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 */ setErr(PrintStream err)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 */ console()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 */ inheritedChannel()247 public static Channel inheritedChannel() throws IOException { 248 return SelectorProvider.provider().inheritedChannel(); 249 } 250 setIn0(InputStream in)251 private static native void setIn0(InputStream in); setOut0(PrintStream out)252 private static native void setOut0(PrintStream out); setErr0(PrintStream err)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 setSecurityManager(final SecurityManager s)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 */ getSecurityManager()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 */ currentTimeMillis()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 */ nanoTime()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 arraycopy(Object src, int srcPos, Object dest, int destPos, int length)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") arraycopy(char[] src, int srcPos, char[] dst, int dstPos, int length)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 arraycopyCharUnchecked(char[] src, int srcPos, char[] dst, int dstPos, int length)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") arraycopy(byte[] src, int srcPos, byte[] dst, int dstPos, int length)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 arraycopyByteUnchecked(byte[] src, int srcPos, byte[] dst, int dstPos, int length)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") arraycopy(short[] src, int srcPos, short[] dst, int dstPos, int length)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 arraycopyShortUnchecked(short[] src, int srcPos, short[] dst, int dstPos, int length)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") arraycopy(int[] src, int srcPos, int[] dst, int dstPos, int length)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 arraycopyIntUnchecked(int[] src, int srcPos, int[] dst, int dstPos, int length)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") arraycopy(long[] src, int srcPos, long[] dst, int dstPos, int length)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 arraycopyLongUnchecked(long[] src, int srcPos, long[] dst, int dstPos, int length)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") arraycopy(float[] src, int srcPos, float[] dst, int dstPos, int length)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 arraycopyFloatUnchecked(float[] src, int srcPos, float[] dst, int dstPos, int length)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") arraycopy(double[] src, int srcPos, double[] dst, int dstPos, int length)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 arraycopyDoubleUnchecked(double[] src, int srcPos, double[] dst, int dstPos, int length)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") arraycopy(boolean[] src, int srcPos, boolean[] dst, int dstPos, int length)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 arraycopyBooleanUnchecked(boolean[] src, int srcPos, boolean[] dst, int dstPos, int length)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 */ identityHashCode(Object x)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 specialProperties()933 private static native String[] specialProperties(); 934 935 static final class PropertiesWithNonOverrideableDefaults extends Properties { PropertiesWithNonOverrideableDefaults(Properties defaults)936 PropertiesWithNonOverrideableDefaults(Properties defaults) { 937 super(defaults); 938 } 939 940 @Override put(Object key, Object value)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 remove(Object key)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 parsePropertyAssignments(Properties p, String[] assignments)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 initUnchangeableSystemProperties()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 // os.name was previously hardcoded to "Linux", but was reverted due to support 998 // for Fuchsia. b/121268567 shows initialization regressions. 999 p.put("os.name", info.sysname); 1000 p.put("os.version", info.release); 1001 1002 // Android-added: Undocumented properties that exist only on Android. 1003 p.put("android.icu.library.version", ICU.getIcuVersion()); 1004 p.put("android.icu.unicode.version", ICU.getUnicodeVersion()); 1005 p.put("android.icu.cldr.version", ICU.getCldrVersion()); 1006 1007 // Property override for ICU4J : this is the location of the ICU4C data. This 1008 // is prioritized over the properties in ICUConfig.properties. The issue with using 1009 // that is that it doesn't play well with jarjar and it needs complicated build rules 1010 // to change its default value. 1011 String icuDataPath = TimeZoneDataFiles.generateIcuDataPath(); 1012 p.put("android.icu.impl.ICUBinary.dataPath", icuDataPath); 1013 1014 parsePropertyAssignments(p, specialProperties()); 1015 1016 // Override built-in properties with settings from the command line. 1017 // Note: it is not possible to override hardcoded values. 1018 parsePropertyAssignments(p, runtime.properties()); 1019 1020 1021 // Set static hardcoded properties. 1022 // These come last, as they must be guaranteed to agree with what a backend compiler 1023 // may assume when compiling the boot image on Android. 1024 for (String[] pair : AndroidHardcodedSystemProperties.STATIC_PROPERTIES) { 1025 if (p.containsKey(pair[0])) { 1026 logE("Ignoring command line argument: -D" + pair[0]); 1027 } 1028 if (pair[1] == null) { 1029 p.remove(pair[0]); 1030 } else { 1031 p.put(pair[0], pair[1]); 1032 } 1033 } 1034 1035 return p; 1036 } 1037 initProperties()1038 private static Properties initProperties() { 1039 Properties p = new PropertiesWithNonOverrideableDefaults(unchangeableProps); 1040 setDefaultChangeableProperties(p); 1041 return p; 1042 } 1043 setDefaultChangeableProperties(Properties p)1044 private static Properties setDefaultChangeableProperties(Properties p) { 1045 // On Android, each app gets its own temporary directory. 1046 // (See android.app.ActivityThread.) This is just a fallback default, 1047 // useful only on the host. 1048 // We check first if the property has not been set already: note that it 1049 // can only be set from the command line through the '-Djava.io.tmpdir=' option. 1050 if (!unchangeableProps.containsKey("java.io.tmpdir")) { 1051 p.put("java.io.tmpdir", "/tmp"); 1052 } 1053 1054 // Android has always had an empty "user.home" (see docs for getProperty). 1055 // This is not useful for normal android apps which need to use android specific 1056 // APIs such as {@code Context.getFilesDir} and {@code Context.getCacheDir} but 1057 // we make it changeable for backward compatibility, so that they can change it 1058 // to a writeable location if required. 1059 // We check first if the property has not been set already: note that it 1060 // can only be set from the command line through the '-Duser.home=' option. 1061 if (!unchangeableProps.containsKey("user.home")) { 1062 p.put("user.home", ""); 1063 } 1064 1065 return p; 1066 } 1067 1068 /** 1069 * Inits an unchangeable system property with the given value. 1070 * 1071 * This is called from native code when the environment needs to change under native 1072 * bridge emulation. 1073 * 1074 * @hide also visible for tests. 1075 */ setUnchangeableSystemProperty(String key, String value)1076 public static void setUnchangeableSystemProperty(String key, String value) { 1077 checkKey(key); 1078 unchangeableProps.put(key, value); 1079 } 1080 addLegacyLocaleSystemProperties()1081 private static void addLegacyLocaleSystemProperties() { 1082 final String locale = getProperty("user.locale", ""); 1083 if (!locale.isEmpty()) { 1084 Locale l = Locale.forLanguageTag(locale); 1085 setUnchangeableSystemProperty("user.language", l.getLanguage()); 1086 setUnchangeableSystemProperty("user.region", l.getCountry()); 1087 setUnchangeableSystemProperty("user.variant", l.getVariant()); 1088 } else { 1089 // If "user.locale" isn't set we fall back to our old defaults of 1090 // language="en" and region="US" (if unset) and don't attempt to set it. 1091 // The Locale class will fall back to using user.language and 1092 // user.region if unset. 1093 final String language = getProperty("user.language", ""); 1094 final String region = getProperty("user.region", ""); 1095 1096 if (language.isEmpty()) { 1097 setUnchangeableSystemProperty("user.language", "en"); 1098 } 1099 1100 if (region.isEmpty()) { 1101 setUnchangeableSystemProperty("user.region", "US"); 1102 } 1103 } 1104 } 1105 1106 /** 1107 * Determines the current system properties. 1108 * 1109 * 1110 * <p>The following properties are always provided by the Dalvik VM:</p> 1111 * <p><table BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""> 1112 * <tr BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"> 1113 * <td><b>Name</b></td> <td><b>Meaning</b></td> <td><b>Example</b></td></tr> 1114 * <tr><td>file.separator</td> <td>{@link java.io.File#separator}</td> <td>{@code /}</td></tr> 1115 * 1116 * <tr><td>java.class.path</td> <td>System class path</td> <td>{@code .}</td></tr> 1117 * <tr><td>java.class.version</td> <td>(Not useful on Android)</td> <td>{@code 50.0}</td></tr> 1118 * <tr><td>java.compiler</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1119 * <tr><td>java.ext.dirs</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1120 * <tr><td>java.home</td> <td>Location of the VM on the file system</td> <td>{@code /system}</td></tr> 1121 * <tr><td>java.io.tmpdir</td> <td>See {@link java.io.File#createTempFile}</td> <td>{@code /sdcard}</td></tr> 1122 * <tr><td>java.library.path</td> <td>Search path for JNI libraries</td> <td>{@code /vendor/lib:/system/lib}</td></tr> 1123 * <tr><td>java.vendor</td> <td>Human-readable VM vendor</td> <td>{@code The Android Project}</td></tr> 1124 * <tr><td>java.vendor.url</td> <td>URL for VM vendor's web site</td> <td>{@code http://www.android.com/}</td></tr> 1125 * <tr><td>java.version</td> <td>(Not useful on Android)</td> <td>{@code 0}</td></tr> 1126 * 1127 * <tr><td>java.specification.version</td> <td>VM libraries version</td> <td>{@code 0.9}</td></tr> 1128 * <tr><td>java.specification.vendor</td> <td>VM libraries vendor</td> <td>{@code The Android Project}</td></tr> 1129 * <tr><td>java.specification.name</td> <td>VM libraries name</td> <td>{@code Dalvik Core Library}</td></tr> 1130 * <tr><td>java.vm.version</td> <td>VM implementation version</td> <td>{@code 1.2.0}</td></tr> 1131 * <tr><td>java.vm.vendor</td> <td>VM implementation vendor</td> <td>{@code The Android Project}</td></tr> 1132 * <tr><td>java.vm.name</td> <td>VM implementation name</td> <td>{@code Dalvik}</td></tr> 1133 * <tr><td>java.vm.specification.version</td> <td>VM specification version</td> <td>{@code 0.9}</td></tr> 1134 * <tr><td>java.vm.specification.vendor</td> <td>VM specification vendor</td> <td>{@code The Android Project}</td></tr> 1135 * <tr><td>java.vm.specification.name</td> <td>VM specification name</td> <td>{@code Dalvik Virtual Machine Specification}</td></tr> 1136 * 1137 * <tr><td>line.separator</td> <td>The system line separator</td> <td>{@code \n}</td></tr> 1138 * 1139 * <tr><td>os.arch</td> <td>OS architecture</td> <td>{@code armv7l}</td></tr> 1140 * <tr><td>os.name</td> <td>OS (kernel) name</td> <td>{@code Linux}</td></tr> 1141 * <tr><td>os.version</td> <td>OS (kernel) version</td> <td>{@code 2.6.32.9-g103d848}</td></tr> 1142 * 1143 * <tr><td>path.separator</td> <td>See {@link java.io.File#pathSeparator}</td> <td>{@code :}</td></tr> 1144 * 1145 * <tr><td>user.dir</td> <td>Base of non-absolute paths</td> <td>{@code /}</td></tr> 1146 * <tr><td>user.home</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1147 * <tr><td>user.name</td> <td>(Not useful on Android)</td> <td>Empty</td></tr> 1148 * 1149 * </table> 1150 * <p> 1151 * Multiple paths in a system property value are separated by the path 1152 * separator character of the platform. 1153 * <p> 1154 * Note that even if the security manager does not permit the 1155 * <code>getProperties</code> operation, it may choose to permit the 1156 * {@link #getProperty(String)} operation. 1157 * 1158 * @return the system properties 1159 * @exception SecurityException if a security manager exists and its 1160 * <code>checkPropertiesAccess</code> method doesn't allow access 1161 * to the system properties. 1162 * @see #setProperties 1163 * @see java.lang.SecurityException 1164 * @see java.lang.SecurityManager#checkPropertiesAccess() 1165 * @see java.util.Properties 1166 */ getProperties()1167 public static Properties getProperties() { 1168 SecurityManager sm = getSecurityManager(); 1169 if (sm != null) { 1170 sm.checkPropertiesAccess(); 1171 } 1172 1173 return props; 1174 } 1175 1176 /** 1177 * Returns the system-dependent line separator string. It always 1178 * returns the same value - the initial value of the {@linkplain 1179 * #getProperty(String) system property} {@code line.separator}. 1180 * 1181 * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft 1182 * Windows systems it returns {@code "\r\n"}. 1183 * 1184 * @return the system-dependent line separator string 1185 * @since 1.7 1186 */ lineSeparator()1187 public static String lineSeparator() { 1188 return lineSeparator; 1189 } 1190 1191 private static String lineSeparator; 1192 1193 1194 // Comment replaced with android one. 1195 /** 1196 * Attempts to set all system properties. Copies all properties from 1197 * {@code p} and discards system properties that are read only and cannot 1198 * be modified. See {@link #getProperty} for a list of such properties. 1199 */ setProperties(Properties props)1200 public static void setProperties(Properties props) { 1201 Properties baseProperties = new PropertiesWithNonOverrideableDefaults(unchangeableProps); 1202 if (props != null) { 1203 baseProperties.putAll(props); 1204 } else { 1205 setDefaultChangeableProperties(baseProperties); 1206 } 1207 1208 System.props = baseProperties; 1209 } 1210 1211 /** 1212 * Gets the system property indicated by the specified key. 1213 * <p> 1214 * First, if there is a security manager, its 1215 * <code>checkPropertyAccess</code> method is called with the key as 1216 * its argument. This may result in a SecurityException. 1217 * <p> 1218 * If there is no current set of system properties, a set of system 1219 * properties is first created and initialized in the same manner as 1220 * for the <code>getProperties</code> method. 1221 * 1222 * @param key the name of the system property. 1223 * @return the string value of the system property, 1224 * or <code>null</code> if there is no property with that key. 1225 * 1226 * @exception SecurityException if a security manager exists and its 1227 * <code>checkPropertyAccess</code> method doesn't allow 1228 * access to the specified system property. 1229 * @exception NullPointerException if <code>key</code> is 1230 * <code>null</code>. 1231 * @exception IllegalArgumentException if <code>key</code> is empty. 1232 * @see #setProperty 1233 * @see java.lang.SecurityException 1234 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) 1235 * @see java.lang.System#getProperties() 1236 */ getProperty(String key)1237 public static String getProperty(String key) { 1238 checkKey(key); 1239 SecurityManager sm = getSecurityManager(); 1240 if (sm != null) { 1241 sm.checkPropertyAccess(key); 1242 } 1243 1244 return props.getProperty(key); 1245 } 1246 1247 /** 1248 * Gets the system property indicated by the specified key. 1249 * <p> 1250 * First, if there is a security manager, its 1251 * <code>checkPropertyAccess</code> method is called with the 1252 * <code>key</code> as its argument. 1253 * <p> 1254 * If there is no current set of system properties, a set of system 1255 * properties is first created and initialized in the same manner as 1256 * for the <code>getProperties</code> method. 1257 * 1258 * @param key the name of the system property. 1259 * @param def a default value. 1260 * @return the string value of the system property, 1261 * or the default value if there is no property with that key. 1262 * 1263 * @exception SecurityException if a security manager exists and its 1264 * <code>checkPropertyAccess</code> method doesn't allow 1265 * access to the specified system property. 1266 * @exception NullPointerException if <code>key</code> is 1267 * <code>null</code>. 1268 * @exception IllegalArgumentException if <code>key</code> is empty. 1269 * @see #setProperty 1270 * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String) 1271 * @see java.lang.System#getProperties() 1272 */ getProperty(String key, String def)1273 public static String getProperty(String key, String def) { 1274 checkKey(key); 1275 SecurityManager sm = getSecurityManager(); 1276 if (sm != null) { 1277 sm.checkPropertyAccess(key); 1278 } 1279 1280 return props.getProperty(key, def); 1281 } 1282 1283 /** 1284 * Sets the system property indicated by the specified key. 1285 * <p> 1286 * First, if a security manager exists, its 1287 * <code>SecurityManager.checkPermission</code> method 1288 * is called with a <code>PropertyPermission(key, "write")</code> 1289 * permission. This may result in a SecurityException being thrown. 1290 * If no exception is thrown, the specified property is set to the given 1291 * value. 1292 * <p> 1293 * 1294 * @param key the name of the system property. 1295 * @param value the value of the system property. 1296 * @return the previous value of the system property, 1297 * or <code>null</code> if it did not have one. 1298 * 1299 * @exception SecurityException if a security manager exists and its 1300 * <code>checkPermission</code> method doesn't allow 1301 * setting of the specified property. 1302 * @exception NullPointerException if <code>key</code> or 1303 * <code>value</code> is <code>null</code>. 1304 * @exception IllegalArgumentException if <code>key</code> is empty. 1305 * @see #getProperty 1306 * @see java.lang.System#getProperty(java.lang.String) 1307 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 1308 * @see java.util.PropertyPermission 1309 * @see SecurityManager#checkPermission 1310 * @since 1.2 1311 */ setProperty(String key, String value)1312 public static String setProperty(String key, String value) { 1313 checkKey(key); 1314 SecurityManager sm = getSecurityManager(); 1315 if (sm != null) { 1316 sm.checkPermission(new PropertyPermission(key, 1317 SecurityConstants.PROPERTY_WRITE_ACTION)); 1318 } 1319 1320 return (String) props.setProperty(key, value); 1321 } 1322 1323 /** 1324 * Removes the system property indicated by the specified key. 1325 * <p> 1326 * First, if a security manager exists, its 1327 * <code>SecurityManager.checkPermission</code> method 1328 * is called with a <code>PropertyPermission(key, "write")</code> 1329 * permission. This may result in a SecurityException being thrown. 1330 * If no exception is thrown, the specified property is removed. 1331 * <p> 1332 * 1333 * @param key the name of the system property to be removed. 1334 * @return the previous string value of the system property, 1335 * or <code>null</code> if there was no property with that key. 1336 * 1337 * @exception SecurityException if a security manager exists and its 1338 * <code>checkPropertyAccess</code> method doesn't allow 1339 * access to the specified system property. 1340 * @exception NullPointerException if <code>key</code> is 1341 * <code>null</code>. 1342 * @exception IllegalArgumentException if <code>key</code> is empty. 1343 * @see #getProperty 1344 * @see #setProperty 1345 * @see java.util.Properties 1346 * @see java.lang.SecurityException 1347 * @see java.lang.SecurityManager#checkPropertiesAccess() 1348 * @since 1.5 1349 */ clearProperty(String key)1350 public static String clearProperty(String key) { 1351 checkKey(key); 1352 SecurityManager sm = getSecurityManager(); 1353 if (sm != null) { 1354 sm.checkPermission(new PropertyPermission(key, "write")); 1355 } 1356 1357 return (String) props.remove(key); 1358 } 1359 checkKey(String key)1360 private static void checkKey(String key) { 1361 if (key == null) { 1362 throw new NullPointerException("key can't be null"); 1363 } 1364 if (key.equals("")) { 1365 throw new IllegalArgumentException("key can't be empty"); 1366 } 1367 } 1368 1369 /** 1370 * Gets the value of the specified environment variable. An 1371 * environment variable is a system-dependent external named 1372 * value. 1373 * 1374 * <p>If a security manager exists, its 1375 * {@link SecurityManager#checkPermission checkPermission} 1376 * method is called with a 1377 * <code>{@link RuntimePermission}("getenv."+name)</code> 1378 * permission. This may result in a {@link SecurityException} 1379 * being thrown. If no exception is thrown the value of the 1380 * variable <code>name</code> is returned. 1381 * 1382 * <p><a name="EnvironmentVSSystemProperties"><i>System 1383 * properties</i> and <i>environment variables</i></a> are both 1384 * conceptually mappings between names and values. Both 1385 * mechanisms can be used to pass user-defined information to a 1386 * Java process. Environment variables have a more global effect, 1387 * because they are visible to all descendants of the process 1388 * which defines them, not just the immediate Java subprocess. 1389 * They can have subtly different semantics, such as case 1390 * insensitivity, on different operating systems. For these 1391 * reasons, environment variables are more likely to have 1392 * unintended side effects. It is best to use system properties 1393 * where possible. Environment variables should be used when a 1394 * global effect is desired, or when an external system interface 1395 * requires an environment variable (such as <code>PATH</code>). 1396 * 1397 * <p>On UNIX systems the alphabetic case of <code>name</code> is 1398 * typically significant, while on Microsoft Windows systems it is 1399 * typically not. For example, the expression 1400 * <code>System.getenv("FOO").equals(System.getenv("foo"))</code> 1401 * is likely to be true on Microsoft Windows. 1402 * 1403 * @param name the name of the environment variable 1404 * @return the string value of the variable, or <code>null</code> 1405 * if the variable is not defined in the system environment 1406 * @throws NullPointerException if <code>name</code> is <code>null</code> 1407 * @throws SecurityException 1408 * if a security manager exists and its 1409 * {@link SecurityManager#checkPermission checkPermission} 1410 * method doesn't allow access to the environment variable 1411 * <code>name</code> 1412 * @see #getenv() 1413 * @see ProcessBuilder#environment() 1414 */ getenv(String name)1415 public static String getenv(String name) { 1416 if (name == null) { 1417 throw new NullPointerException("name == null"); 1418 } 1419 1420 return Libcore.os.getenv(name); 1421 } 1422 1423 1424 /** 1425 * Returns an unmodifiable string map view of the current system environment. 1426 * The environment is a system-dependent mapping from names to 1427 * values which is passed from parent to child processes. 1428 * 1429 * <p>If the system does not support environment variables, an 1430 * empty map is returned. 1431 * 1432 * <p>The returned map will never contain null keys or values. 1433 * Attempting to query the presence of a null key or value will 1434 * throw a {@link NullPointerException}. Attempting to query 1435 * the presence of a key or value which is not of type 1436 * {@link String} will throw a {@link ClassCastException}. 1437 * 1438 * <p>The returned map and its collection views may not obey the 1439 * general contract of the {@link Object#equals} and 1440 * {@link Object#hashCode} methods. 1441 * 1442 * <p>The returned map is typically case-sensitive on all platforms. 1443 * 1444 * <p>If a security manager exists, its 1445 * {@link SecurityManager#checkPermission checkPermission} 1446 * method is called with a 1447 * <code>{@link RuntimePermission}("getenv.*")</code> 1448 * permission. This may result in a {@link SecurityException} being 1449 * thrown. 1450 * 1451 * <p>When passing information to a Java subprocess, 1452 * <a href=#EnvironmentVSSystemProperties>system properties</a> 1453 * are generally preferred over environment variables. 1454 * 1455 * @return the environment as a map of variable names to values 1456 * @throws SecurityException 1457 * if a security manager exists and its 1458 * {@link SecurityManager#checkPermission checkPermission} 1459 * method doesn't allow access to the process environment 1460 * @see #getenv(String) 1461 * @see ProcessBuilder#environment() 1462 * @since 1.5 1463 */ getenv()1464 public static java.util.Map<String,String> getenv() { 1465 SecurityManager sm = getSecurityManager(); 1466 if (sm != null) { 1467 sm.checkPermission(new RuntimePermission("getenv.*")); 1468 } 1469 1470 return ProcessEnvironment.getenv(); 1471 } 1472 1473 /** 1474 * Terminates the currently running Java Virtual Machine. The 1475 * argument serves as a status code; by convention, a nonzero status 1476 * code indicates abnormal termination. 1477 * <p> 1478 * This method calls the <code>exit</code> method in class 1479 * <code>Runtime</code>. This method never returns normally. 1480 * <p> 1481 * The call <code>System.exit(n)</code> is effectively equivalent to 1482 * the call: 1483 * <blockquote><pre> 1484 * Runtime.getRuntime().exit(n) 1485 * </pre></blockquote> 1486 * 1487 * @param status exit status. 1488 * @throws SecurityException 1489 * if a security manager exists and its <code>checkExit</code> 1490 * method doesn't allow exit with the specified status. 1491 * @see java.lang.Runtime#exit(int) 1492 */ exit(int status)1493 public static void exit(int status) { 1494 Runtime.getRuntime().exit(status); 1495 } 1496 1497 /** 1498 * Runs the garbage collector. 1499 * <p> 1500 * Calling the <code>gc</code> method suggests that the Java Virtual 1501 * Machine expend effort toward recycling unused objects in order to 1502 * make the memory they currently occupy available for quick reuse. 1503 * When control returns from the method call, the Java Virtual 1504 * Machine has made a best effort to reclaim space from all discarded 1505 * objects. 1506 * <p> 1507 * The call <code>System.gc()</code> is effectively equivalent to the 1508 * call: 1509 * <blockquote><pre> 1510 * Runtime.getRuntime().gc() 1511 * </pre></blockquote> 1512 * 1513 * @see java.lang.Runtime#gc() 1514 */ gc()1515 public static void gc() { 1516 boolean shouldRunGC; 1517 synchronized (LOCK) { 1518 shouldRunGC = justRanFinalization; 1519 if (shouldRunGC) { 1520 justRanFinalization = false; 1521 } else { 1522 runGC = true; 1523 } 1524 } 1525 if (shouldRunGC) { 1526 Runtime.getRuntime().gc(); 1527 } 1528 } 1529 1530 /** 1531 * Runs the finalization methods of any objects pending finalization. 1532 * <p> 1533 * Calling this method suggests that the Java Virtual Machine expend 1534 * effort toward running the <code>finalize</code> methods of objects 1535 * that have been found to be discarded but whose <code>finalize</code> 1536 * methods have not yet been run. When control returns from the 1537 * method call, the Java Virtual Machine has made a best effort to 1538 * complete all outstanding finalizations. 1539 * <p> 1540 * The call <code>System.runFinalization()</code> is effectively 1541 * equivalent to the call: 1542 * <blockquote><pre> 1543 * Runtime.getRuntime().runFinalization() 1544 * </pre></blockquote> 1545 * 1546 * @see java.lang.Runtime#runFinalization() 1547 */ runFinalization()1548 public static void runFinalization() { 1549 boolean shouldRunGC; 1550 synchronized (LOCK) { 1551 shouldRunGC = runGC; 1552 runGC = false; 1553 } 1554 if (shouldRunGC) { 1555 Runtime.getRuntime().gc(); 1556 } 1557 Runtime.getRuntime().runFinalization(); 1558 synchronized (LOCK) { 1559 justRanFinalization = true; 1560 } 1561 } 1562 1563 /** 1564 * Enable or disable finalization on exit; doing so specifies that the 1565 * finalizers of all objects that have finalizers that have not yet been 1566 * automatically invoked are to be run before the Java runtime exits. 1567 * By default, finalization on exit is disabled. 1568 * 1569 * <p>If there is a security manager, 1570 * its <code>checkExit</code> method is first called 1571 * with 0 as its argument to ensure the exit is allowed. 1572 * This could result in a SecurityException. 1573 * 1574 * @deprecated This method is inherently unsafe. It may result in 1575 * finalizers being called on live objects while other threads are 1576 * concurrently manipulating those objects, resulting in erratic 1577 * behavior or deadlock. 1578 * @param value indicating enabling or disabling of finalization 1579 * @throws SecurityException 1580 * if a security manager exists and its <code>checkExit</code> 1581 * method doesn't allow the exit. 1582 * 1583 * @see java.lang.Runtime#exit(int) 1584 * @see java.lang.Runtime#gc() 1585 * @see java.lang.SecurityManager#checkExit(int) 1586 * @since JDK1.1 1587 */ 1588 @Deprecated runFinalizersOnExit(boolean value)1589 public static void runFinalizersOnExit(boolean value) { 1590 Runtime.runFinalizersOnExit(value); 1591 } 1592 1593 /** 1594 * Loads the native library specified by the filename argument. The filename 1595 * argument must be an absolute path name. 1596 * 1597 * If the filename argument, when stripped of any platform-specific library 1598 * prefix, path, and file extension, indicates a library whose name is, 1599 * for example, L, and a native library called L is statically linked 1600 * with the VM, then the JNI_OnLoad_L function exported by the library 1601 * is invoked rather than attempting to load a dynamic library. 1602 * A filename matching the argument does not have to exist in the 1603 * file system. 1604 * See the JNI Specification for more details. 1605 * 1606 * Otherwise, the filename argument is mapped to a native library image in 1607 * an implementation-dependent manner. 1608 * 1609 * <p> 1610 * The call <code>System.load(name)</code> is effectively equivalent 1611 * to the call: 1612 * <blockquote><pre> 1613 * Runtime.getRuntime().load(name) 1614 * </pre></blockquote> 1615 * 1616 * @param filename the file to load. 1617 * @exception SecurityException if a security manager exists and its 1618 * <code>checkLink</code> method doesn't allow 1619 * loading of the specified dynamic library 1620 * @exception UnsatisfiedLinkError if either the filename is not an 1621 * absolute path name, the native library is not statically 1622 * linked with the VM, or the library cannot be mapped to 1623 * a native library image by the host system. 1624 * @exception NullPointerException if <code>filename</code> is 1625 * <code>null</code> 1626 * @see java.lang.Runtime#load(java.lang.String) 1627 * @see java.lang.SecurityManager#checkLink(java.lang.String) 1628 */ 1629 @CallerSensitive load(String filename)1630 public static void load(String filename) { 1631 Runtime.getRuntime().load0(Reflection.getCallerClass(), filename); 1632 } 1633 1634 /** 1635 * Loads the native library specified by the <code>libname</code> 1636 * argument. The <code>libname</code> argument must not contain any platform 1637 * specific prefix, file extension or path. If a native library 1638 * called <code>libname</code> is statically linked with the VM, then the 1639 * JNI_OnLoad_<code>libname</code> function exported by the library is invoked. 1640 * See the JNI Specification for more details. 1641 * 1642 * Otherwise, the libname argument is loaded from a system library 1643 * location and mapped to a native library image in an implementation- 1644 * dependent manner. 1645 * <p> 1646 * The call <code>System.loadLibrary(name)</code> is effectively 1647 * equivalent to the call 1648 * <blockquote><pre> 1649 * Runtime.getRuntime().loadLibrary(name) 1650 * </pre></blockquote> 1651 * 1652 * @param libname the name of the library. 1653 * @exception SecurityException if a security manager exists and its 1654 * <code>checkLink</code> method doesn't allow 1655 * loading of the specified dynamic library 1656 * @exception UnsatisfiedLinkError if either the libname argument 1657 * contains a file path, the native library is not statically 1658 * linked with the VM, or the library cannot be mapped to a 1659 * native library image by the host system. 1660 * @exception NullPointerException if <code>libname</code> is 1661 * <code>null</code> 1662 * @see java.lang.Runtime#loadLibrary(java.lang.String) 1663 * @see java.lang.SecurityManager#checkLink(java.lang.String) 1664 */ 1665 @CallerSensitive loadLibrary(String libname)1666 public static void loadLibrary(String libname) { 1667 Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname); 1668 } 1669 1670 /** 1671 * Maps a library name into a platform-specific string representing 1672 * a native library. 1673 * 1674 * @param libname the name of the library. 1675 * @return a platform-dependent native library name. 1676 * @exception NullPointerException if <code>libname</code> is 1677 * <code>null</code> 1678 * @see java.lang.System#loadLibrary(java.lang.String) 1679 * @see java.lang.ClassLoader#findLibrary(java.lang.String) 1680 * @since 1.2 1681 */ mapLibraryName(String libname)1682 public static native String mapLibraryName(String libname); 1683 1684 /** 1685 * Create PrintStream for stdout/err based on encoding. 1686 */ newPrintStream(FileOutputStream fos, String enc)1687 private static PrintStream newPrintStream(FileOutputStream fos, String enc) { 1688 if (enc != null) { 1689 try { 1690 return new PrintStream(new BufferedOutputStream(fos, 128), true, enc); 1691 } catch (UnsupportedEncodingException uee) {} 1692 } 1693 return new PrintStream(new BufferedOutputStream(fos, 128), true); 1694 } 1695 1696 1697 /** 1698 * Initialize the system class. Called after thread initialization. 1699 */ 1700 static { 1701 unchangeableProps = initUnchangeableSystemProperties(); 1702 props = initProperties(); addLegacyLocaleSystemProperties()1703 addLegacyLocaleSystemProperties(); sun.misc.Version.initSystemProperties()1704 sun.misc.Version.initSystemProperties(); 1705 1706 // TODO: Confirm that this isn't something super important. 1707 // sun.misc.VM.saveAndRemoveProperties(props); 1708 1709 lineSeparator = props.getProperty("line.separator"); 1710 1711 FileInputStream fdIn = new FileInputStream(FileDescriptor.in); 1712 FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out); 1713 FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err); 1714 // BEGIN Android-changed: lower buffer size. 1715 // in = new BufferedInputStream(fdIn); 1716 in = new BufferedInputStream(fdIn, 128); 1717 // END Android-changed: lower buffer size. 1718 out = newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")); 1719 err = newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")); 1720 1721 // Initialize any miscellenous operating system settings that need to be 1722 // set for the class libraries. Currently this is no-op everywhere except 1723 // for Windows where the process-wide error mode is set before the java.io 1724 // classes are used. sun.misc.VM.initializeOSEnvironment()1725 sun.misc.VM.initializeOSEnvironment(); 1726 1727 // Subsystems that are invoked during initialization can invoke 1728 // sun.misc.VM.isBooted() in order to avoid doing things that should 1729 // wait until the application class loader has been set up. 1730 // IMPORTANT: Ensure that this remains the last initialization action! sun.misc.VM.booted()1731 sun.misc.VM.booted(); 1732 } 1733 1734 /** 1735 * @hide internal use only 1736 */ logE(String message)1737 public static void logE(String message) { 1738 log('E', message, null); 1739 } 1740 1741 /** 1742 * @hide internal use only 1743 */ logE(String message, Throwable th)1744 public static void logE(String message, Throwable th) { 1745 log('E', message, th); 1746 } 1747 1748 /** 1749 * @hide internal use only 1750 */ logI(String message)1751 public static void logI(String message) { 1752 log('I', message, null); 1753 } 1754 1755 /** 1756 * @hide internal use only 1757 */ logI(String message, Throwable th)1758 public static void logI(String message, Throwable th) { 1759 log('I', message, th); 1760 } 1761 1762 /** 1763 * @hide internal use only 1764 */ logW(String message)1765 public static void logW(String message) { 1766 log('W', message, null); 1767 } 1768 1769 /** 1770 * @hide internal use only 1771 */ logW(String message, Throwable th)1772 public static void logW(String message, Throwable th) { 1773 log('W', message, th); 1774 } 1775 log(char type, String message, Throwable th)1776 private static native void log(char type, String message, Throwable th); 1777 } 1778