-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
System.java
2452 lines (2320 loc) · 105 KB
/
System.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Console;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.invoke.StringConcatFactory;
import java.lang.module.ModuleDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL;
import java.nio.charset.CharacterCodingException;
import java.nio.channels.Channel;
import java.nio.channels.spi.SelectorProvider;
import java.nio.charset.Charset;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.PropertyPermission;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Supplier;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.StaticProperty;
import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.VM;
import jdk.internal.logger.LoggerFinderLoader;
import jdk.internal.logger.LazyLoggers;
import jdk.internal.logger.LocalizedLoggerWrapper;
import jdk.internal.util.SystemProps;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
import sun.nio.fs.DefaultFileSystemProvider;
import sun.reflect.annotation.AnnotationType;
import sun.nio.ch.Interruptible;
import sun.security.util.SecurityConstants;
/**
* The {@code System} class contains several useful class fields
* and methods. It cannot be instantiated.
*
* Among the facilities provided by the {@code System} class
* are standard input, standard output, and error output streams;
* access to externally defined properties and environment
* variables; a means of loading files and libraries; and a utility
* method for quickly copying a portion of an array.
*
* @since 1.0
*/
public final class System {
/* Register the natives via the static initializer.
*
* The VM will invoke the initPhase1 method to complete the initialization
* of this class separate from <clinit>.
*/
private static native void registerNatives();
static {
registerNatives();
}
/** Don't let anyone instantiate this class */
private System() {
}
/**
* The "standard" input stream. This stream is already
* open and ready to supply input data. Typically this stream
* corresponds to keyboard input or another input source specified by
* the host environment or user. In case this stream is wrapped
* in a {@link java.io.InputStreamReader}, {@link Console#charset()}
* should be used for the charset, or consider using
* {@link Console#reader()}.
*
* @see Console#charset()
* @see Console#reader()
*/
public static final InputStream in = null;
/**
* The "standard" output stream. This stream is already
* open and ready to accept output data. Typically this stream
* corresponds to display output or another output destination
* specified by the host environment or user. The encoding used
* in the conversion from characters to bytes is equivalent to
* {@link Console#charset()} if the {@code Console} exists,
* {@link Charset#defaultCharset()} otherwise.
* <p>
* For simple stand-alone Java applications, a typical way to write
* a line of output data is:
* <blockquote><pre>
* System.out.println(data)
* </pre></blockquote>
* <p>
* See the {@code println} methods in class {@code PrintStream}.
*
* @see java.io.PrintStream#println()
* @see java.io.PrintStream#println(boolean)
* @see java.io.PrintStream#println(char)
* @see java.io.PrintStream#println(char[])
* @see java.io.PrintStream#println(double)
* @see java.io.PrintStream#println(float)
* @see java.io.PrintStream#println(int)
* @see java.io.PrintStream#println(long)
* @see java.io.PrintStream#println(java.lang.Object)
* @see java.io.PrintStream#println(java.lang.String)
* @see Console#charset()
* @see Charset#defaultCharset()
*/
public static final PrintStream out = null;
/**
* The "standard" error output stream. This stream is already
* open and ready to accept output data.
* <p>
* Typically this stream corresponds to display output or another
* output destination specified by the host environment or user. By
* convention, this output stream is used to display error messages
* or other information that should come to the immediate attention
* of a user even if the principal output stream, the value of the
* variable {@code out}, has been redirected to a file or other
* destination that is typically not continuously monitored.
* The encoding used in the conversion from characters to bytes is
* equivalent to {@link Console#charset()} if the {@code Console}
* exists, {@link Charset#defaultCharset()} otherwise.
*
* @see Console#charset()
* @see Charset#defaultCharset()
*/
public static final PrintStream err = null;
// indicates if a security manager is possible
private static final int NEVER = 1;
private static final int MAYBE = 2;
private static @Stable int allowSecurityManager;
// current security manager
@SuppressWarnings("removal")
private static volatile SecurityManager security; // read by VM
// return true if a security manager is allowed
private static boolean allowSecurityManager() {
return (allowSecurityManager != NEVER);
}
/**
* Reassigns the "standard" input stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" input stream.
*
* @param in the new standard input stream.
*
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard input stream.
*
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*
* @since 1.1
*/
public static void setIn(InputStream in) {
checkIO();
setIn0(in);
}
/**
* Reassigns the "standard" output stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" output stream.
*
* @param out the new standard output stream
*
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard output stream.
*
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*
* @since 1.1
*/
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
/**
* Reassigns the "standard" error output stream.
*
* First, if there is a security manager, its {@code checkPermission}
* method is called with a {@code RuntimePermission("setIO")} permission
* to see if it's ok to reassign the "standard" error output stream.
*
* @param err the new standard error output stream.
*
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method doesn't allow
* reassigning of the standard error output stream.
*
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*
* @since 1.1
*/
public static void setErr(PrintStream err) {
checkIO();
setErr0(err);
}
private static volatile Console cons;
/**
* Returns the unique {@link java.io.Console Console} object associated
* with the current Java virtual machine, if any.
*
* @return The system console, if any, otherwise {@code null}.
*
* @since 1.6
*/
public static Console console() {
Console c;
if ((c = cons) == null) {
synchronized (System.class) {
if ((c = cons) == null) {
cons = c = SharedSecrets.getJavaIOAccess().console();
}
}
}
return c;
}
/**
* Returns the channel inherited from the entity that created this
* Java virtual machine.
*
* This method returns the channel obtained by invoking the
* {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
* inheritedChannel} method of the system-wide default
* {@link java.nio.channels.spi.SelectorProvider} object.
*
* <p> In addition to the network-oriented channels described in
* {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
* inheritedChannel}, this method may return other kinds of
* channels in the future.
*
* @return The inherited channel, if any, otherwise {@code null}.
*
* @throws IOException
* If an I/O error occurs
*
* @throws SecurityException
* If a security manager is present and it does not
* permit access to the channel.
*
* @since 1.5
*/
public static Channel inheritedChannel() throws IOException {
return SelectorProvider.provider().inheritedChannel();
}
private static void checkIO() {
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("setIO"));
}
}
private static native void setIn0(InputStream in);
private static native void setOut0(PrintStream out);
private static native void setErr0(PrintStream err);
private static class CallersHolder {
// Remember callers of setSecurityManager() here so that warning
// is only printed once for each different caller
final static Map<Class<?>, Boolean> callers
= Collections.synchronizedMap(new WeakHashMap<>());
}
// Remember initial System.err. setSecurityManager() warning goes here
private static volatile @Stable PrintStream initialErrStream;
private static URL codeSource(Class<?> clazz) {
PrivilegedAction<ProtectionDomain> pa = clazz::getProtectionDomain;
@SuppressWarnings("removal")
CodeSource cs = AccessController.doPrivileged(pa).getCodeSource();
return (cs != null) ? cs.getLocation() : null;
}
/**
* Sets the system-wide security manager.
*
* If there is a security manager already installed, this method first
* calls the security manager's {@code checkPermission} method
* with a {@code RuntimePermission("setSecurityManager")}
* permission to ensure it's ok to replace the existing
* security manager.
* This may result in throwing a {@code SecurityException}.
*
* <p> Otherwise, the argument is established as the current
* security manager. If the argument is {@code null} and no
* security manager has been established, then no action is taken and
* the method simply returns.
*
* @implNote In the JDK implementation, if the Java virtual machine is
* started with the system property {@code java.security.manager} set to
* the special token "{@code disallow}" then the {@code setSecurityManager}
* method cannot be used to set a security manager.
*
* @param sm the security manager or {@code null}
* @throws SecurityException
* if the security manager has already been set and its {@code
* checkPermission} method doesn't allow it to be replaced
* @throws UnsupportedOperationException
* if {@code sm} is non-null and a security manager is not allowed
* to be set dynamically
* @see #getSecurityManager
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
* @deprecated This method is only useful in conjunction with
* {@linkplain SecurityManager the Security Manager}, which is
* deprecated and subject to removal in a future release.
* Consequently, this method is also deprecated and subject to
* removal. There is no replacement for the Security Manager or this
* method.
*/
@Deprecated(since="17", forRemoval=true)
@CallerSensitive
public static void setSecurityManager(@SuppressWarnings("removal") SecurityManager sm) {
if (allowSecurityManager()) {
var callerClass = Reflection.getCallerClass();
if (CallersHolder.callers.putIfAbsent(callerClass, true) == null) {
URL url = codeSource(callerClass);
final String source;
if (url == null) {
source = callerClass.getName();
} else {
source = callerClass.getName() + " (" + url + ")";
}
initialErrStream.printf("""
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by %s
WARNING: Please consider reporting this to the maintainers of %s
WARNING: System::setSecurityManager will be removed in a future release
""", source, callerClass.getName());
}
implSetSecurityManager(sm);
} else {
// security manager not allowed
if (sm != null) {
throw new UnsupportedOperationException(
"The Security Manager is deprecated and will be removed in a future release");
}
}
}
private static void implSetSecurityManager(@SuppressWarnings("removal") SecurityManager sm) {
if (security == null) {
// ensure image reader is initialized
Object.class.getResource("java/lang/ANY");
// ensure the default file system is initialized
DefaultFileSystemProvider.theFileSystem();
}
if (sm != null) {
try {
// pre-populates the SecurityManager.packageAccess cache
// to avoid recursive permission checking issues with custom
// SecurityManager implementations
sm.checkPackageAccess("java.lang");
} catch (Exception e) {
// no-op
}
}
setSecurityManager0(sm);
}
@SuppressWarnings("removal")
private static synchronized
void setSecurityManager0(final SecurityManager s) {
SecurityManager sm = getSecurityManager();
if (sm != null) {
// ask the currently installed security manager if we
// can replace it.
sm.checkPermission(new RuntimePermission("setSecurityManager"));
}
if ((s != null) && (s.getClass().getClassLoader() != null)) {
// New security manager class is not on bootstrap classpath.
// Force policy to get initialized before we install the new
// security manager, in order to prevent infinite loops when
// trying to initialize the policy (which usually involves
// accessing some security and/or system properties, which in turn
// calls the installed security manager's checkPermission method
// which will loop infinitely if there is a non-system class
// (in this case: the new security manager class) on the stack).
AccessController.doPrivileged(new PrivilegedAction<>() {
public Object run() {
s.getClass().getProtectionDomain().implies
(SecurityConstants.ALL_PERMISSION);
return null;
}
});
}
security = s;
}
/**
* Gets the system-wide security manager.
*
* @return if a security manager has already been established for the
* current application, then that security manager is returned;
* otherwise, {@code null} is returned.
* @see #setSecurityManager
* @deprecated This method is only useful in conjunction with
* {@linkplain SecurityManager the Security Manager}, which is
* deprecated and subject to removal in a future release.
* Consequently, this method is also deprecated and subject to
* removal. There is no replacement for the Security Manager or this
* method.
*/
@SuppressWarnings("removal")
@Deprecated(since="17", forRemoval=true)
public static SecurityManager getSecurityManager() {
if (allowSecurityManager()) {
return security;
} else {
return null;
}
}
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class {@code Date} for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
@IntrinsicCandidate
public static native long currentTimeMillis();
/**
* Returns the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds.
*
* This method can only be used to measure elapsed time and is
* not related to any other notion of system or wall-clock time.
* The value returned represents nanoseconds since some fixed but
* arbitrary <i>origin</i> time (perhaps in the future, so values
* may be negative). The same origin is used by all invocations of
* this method in an instance of a Java virtual machine; other
* virtual machine instances are likely to use a different origin.
*
* <p>This method provides nanosecond precision, but not necessarily
* nanosecond resolution (that is, how frequently the value changes)
* - no guarantees are made except that the resolution is at least as
* good as that of {@link #currentTimeMillis()}.
*
* <p>Differences in successive calls that span greater than
* approximately 292 years (2<sup>63</sup> nanoseconds) will not
* correctly compute elapsed time due to numerical overflow.
*
* <p>The values returned by this method become meaningful only when
* the difference between two such values, obtained within the same
* instance of a Java virtual machine, is computed.
*
* <p>For example, to measure how long some code takes to execute:
* <pre> {@code
* long startTime = System.nanoTime();
* // ... the code being measured ...
* long elapsedNanos = System.nanoTime() - startTime;}</pre>
*
* <p>To compare elapsed time against a timeout, use <pre> {@code
* if (System.nanoTime() - startTime >= timeoutNanos) ...}</pre>
* instead of <pre> {@code
* if (System.nanoTime() >= startTime + timeoutNanos) ...}</pre>
* because of the possibility of numerical overflow.
*
* @return the current value of the running Java Virtual Machine's
* high-resolution time source, in nanoseconds
* @since 1.5
*/
@IntrinsicCandidate
public static native long nanoTime();
/**
* Copies an array from the specified source array, beginning at the
* specified position, to the specified position of the destination array.
* A subsequence of array components are copied from the source
* array referenced by {@code src} to the destination array
* referenced by {@code dest}. The number of components copied is
* equal to the {@code length} argument. The components at
* positions {@code srcPos} through
* {@code srcPos+length-1} in the source array are copied into
* positions {@code destPos} through
* {@code destPos+length-1}, respectively, of the destination
* array.
* <p>
* If the {@code src} and {@code dest} arguments refer to the
* same array object, then the copying is performed as if the
* components at positions {@code srcPos} through
* {@code srcPos+length-1} were first copied to a temporary
* array with {@code length} components and then the contents of
* the temporary array were copied into positions
* {@code destPos} through {@code destPos+length-1} of the
* destination array.
* <p>
* If {@code dest} is {@code null}, then a
* {@code NullPointerException} is thrown.
* <p>
* If {@code src} is {@code null}, then a
* {@code NullPointerException} is thrown and the destination
* array is not modified.
* <p>
* Otherwise, if any of the following is true, an
* {@code ArrayStoreException} is thrown and the destination is
* not modified:
* <ul>
* <li>The {@code src} argument refers to an object that is not an
* array.
* <li>The {@code dest} argument refers to an object that is not an
* array.
* <li>The {@code src} argument and {@code dest} argument refer
* to arrays whose component types are different primitive types.
* <li>The {@code src} argument refers to an array with a primitive
* component type and the {@code dest} argument refers to an array
* with a reference component type.
* <li>The {@code src} argument refers to an array with a reference
* component type and the {@code dest} argument refers to an array
* with a primitive component type.
* </ul>
* <p>
* Otherwise, if any of the following is true, an
* {@code IndexOutOfBoundsException} is
* thrown and the destination is not modified:
* <ul>
* <li>The {@code srcPos} argument is negative.
* <li>The {@code destPos} argument is negative.
* <li>The {@code length} argument is negative.
* <li>{@code srcPos+length} is greater than
* {@code src.length}, the length of the source array.
* <li>{@code destPos+length} is greater than
* {@code dest.length}, the length of the destination array.
* </ul>
* <p>
* Otherwise, if any actual component of the source array from
* position {@code srcPos} through
* {@code srcPos+length-1} cannot be converted to the component
* type of the destination array by assignment conversion, an
* {@code ArrayStoreException} is thrown. In this case, let
* <b><i>k</i></b> be the smallest nonnegative integer less than
* length such that {@code src[srcPos+}<i>k</i>{@code ]}
* cannot be converted to the component type of the destination
* array; when the exception is thrown, source array components from
* positions {@code srcPos} through
* {@code srcPos+}<i>k</i>{@code -1}
* will already have been copied to destination array positions
* {@code destPos} through
* {@code destPos+}<i>k</I>{@code -1} and no other
* positions of the destination array will have been modified.
* (Because of the restrictions already itemized, this
* paragraph effectively applies only to the situation where both
* arrays have component types that are reference types.)
*
* @param src the source array.
* @param srcPos starting position in the source array.
* @param dest the destination array.
* @param destPos starting position in the destination data.
* @param length the number of array elements to be copied.
* @throws IndexOutOfBoundsException if copying would cause
* access of data outside array bounds.
* @throws ArrayStoreException if an element in the {@code src}
* array could not be stored into the {@code dest} array
* because of a type mismatch.
* @throws NullPointerException if either {@code src} or
* {@code dest} is {@code null}.
*/
@IntrinsicCandidate
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
/**
* Returns the same hash code for the given object as
* would be returned by the default method hashCode(),
* whether or not the given object's class overrides
* hashCode().
* The hash code for the null reference is zero.
*
* @param x object for which the hashCode is to be calculated
* @return the hashCode
* @since 1.1
* @see Object#hashCode
* @see java.util.Objects#hashCode(Object)
*/
@IntrinsicCandidate
public static native int identityHashCode(Object x);
/**
* System properties.
*
* See {@linkplain #getProperties getProperties} for details.
*/
private static Properties props;
/**
* Determines the current system properties.
*
* First, if there is a security manager, its
* {@code checkPropertiesAccess} method is called with no
* arguments. This may result in a security exception.
* <p>
* The current set of system properties for use by the
* {@link #getProperty(String)} method is returned as a
* {@code Properties} object. If there is no current set of
* system properties, a set of system properties is first created and
* initialized. This set of system properties includes a value
* for each of the following keys unless the description of the associated
* value indicates that the value is optional.
* <table class="striped" style="text-align:left">
* <caption style="display:none">Shows property keys and associated values</caption>
* <thead>
* <tr><th scope="col">Key</th>
* <th scope="col">Description of Associated Value</th></tr>
* </thead>
* <tbody>
* <tr><th scope="row">{@systemProperty java.version}</th>
* <td>Java Runtime Environment version, which may be interpreted
* as a {@link Runtime.Version}</td></tr>
* <tr><th scope="row">{@systemProperty java.version.date}</th>
* <td>Java Runtime Environment version date, in ISO-8601 YYYY-MM-DD
* format, which may be interpreted as a {@link
* java.time.LocalDate}</td></tr>
* <tr><th scope="row">{@systemProperty java.vendor}</th>
* <td>Java Runtime Environment vendor</td></tr>
* <tr><th scope="row">{@systemProperty java.vendor.url}</th>
* <td>Java vendor URL</td></tr>
* <tr><th scope="row">{@systemProperty java.vendor.version}</th>
* <td>Java vendor version <em>(optional)</em> </td></tr>
* <tr><th scope="row">{@systemProperty java.home}</th>
* <td>Java installation directory</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.specification.version}</th>
* <td>Java Virtual Machine specification version, whose value is the
* {@linkplain Runtime.Version#feature feature} element of the
* {@linkplain Runtime#version() runtime version}</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.specification.vendor}</th>
* <td>Java Virtual Machine specification vendor</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.specification.name}</th>
* <td>Java Virtual Machine specification name</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.version}</th>
* <td>Java Virtual Machine implementation version which may be
* interpreted as a {@link Runtime.Version}</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.vendor}</th>
* <td>Java Virtual Machine implementation vendor</td></tr>
* <tr><th scope="row">{@systemProperty java.vm.name}</th>
* <td>Java Virtual Machine implementation name</td></tr>
* <tr><th scope="row">{@systemProperty java.specification.version}</th>
* <td>Java Runtime Environment specification version, whose value is
* the {@linkplain Runtime.Version#feature feature} element of the
* {@linkplain Runtime#version() runtime version}</td></tr>
* <tr><th scope="row">{@systemProperty java.specification.vendor}</th>
* <td>Java Runtime Environment specification vendor</td></tr>
* <tr><th scope="row">{@systemProperty java.specification.name}</th>
* <td>Java Runtime Environment specification name</td></tr>
* <tr><th scope="row">{@systemProperty java.class.version}</th>
* <td>Java class format version number</td></tr>
* <tr><th scope="row">{@systemProperty java.class.path}</th>
* <td>Java class path (refer to
* {@link ClassLoader#getSystemClassLoader()} for details)</td></tr>
* <tr><th scope="row">{@systemProperty java.library.path}</th>
* <td>List of paths to search when loading libraries</td></tr>
* <tr><th scope="row">{@systemProperty java.io.tmpdir}</th>
* <td>Default temp file path</td></tr>
* <tr><th scope="row">{@systemProperty java.compiler}</th>
* <td>Name of JIT compiler to use</td></tr>
* <tr><th scope="row">{@systemProperty os.name}</th>
* <td>Operating system name</td></tr>
* <tr><th scope="row">{@systemProperty os.arch}</th>
* <td>Operating system architecture</td></tr>
* <tr><th scope="row">{@systemProperty os.version}</th>
* <td>Operating system version</td></tr>
* <tr><th scope="row">{@systemProperty file.separator}</th>
* <td>File separator ("/" on UNIX)</td></tr>
* <tr><th scope="row">{@systemProperty path.separator}</th>
* <td>Path separator (":" on UNIX)</td></tr>
* <tr><th scope="row">{@systemProperty line.separator}</th>
* <td>Line separator ("\n" on UNIX)</td></tr>
* <tr><th scope="row">{@systemProperty user.name}</th>
* <td>User's account name</td></tr>
* <tr><th scope="row">{@systemProperty user.home}</th>
* <td>User's home directory</td></tr>
* <tr><th scope="row">{@systemProperty user.dir}</th>
* <td>User's current working directory</td></tr>
* <tr><th scope="row">{@systemProperty native.encoding}</th>
* <td>Character encoding name derived from the host environment and/or
* the user's settings. Setting this system property has no effect.</td></tr>
* </tbody>
* </table>
* <p>
* Multiple paths in a system property value are separated by the path
* separator character of the platform.
* <p>
* Note that even if the security manager does not permit the
* {@code getProperties} operation, it may choose to permit the
* {@link #getProperty(String)} operation.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified.</strong>
* Property values may be cached during initialization or on first use.
* Setting a standard property after initialization using {@link #getProperties()},
* {@link #setProperties(Properties)}, {@link #setProperty(String, String)}, or
* {@link #clearProperty(String)} may not have the desired effect.
*
* @implNote
* In addition to the standard system properties, the system
* properties may include the following keys:
* <table class="striped">
* <caption style="display:none">Shows property keys and associated values</caption>
* <thead>
* <tr><th scope="col">Key</th>
* <th scope="col">Description of Associated Value</th></tr>
* </thead>
* <tbody>
* <tr><th scope="row">{@systemProperty jdk.module.path}</th>
* <td>The application module path</td></tr>
* <tr><th scope="row">{@systemProperty jdk.module.upgrade.path}</th>
* <td>The upgrade module path</td></tr>
* <tr><th scope="row">{@systemProperty jdk.module.main}</th>
* <td>The module name of the initial/main module</td></tr>
* <tr><th scope="row">{@systemProperty jdk.module.main.class}</th>
* <td>The main class name of the initial module</td></tr>
* </tbody>
* </table>
*
* @return the system properties
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow access
* to the system properties.
* @see #setProperties
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertiesAccess()
* @see java.util.Properties
*/
public static Properties getProperties() {
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
return props;
}
/**
* Returns the system-dependent line separator string. It always
* returns the same value - the initial value of the {@linkplain
* #getProperty(String) system property} {@code line.separator}.
*
* <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
* Windows systems it returns {@code "\r\n"}.
*
* @return the system-dependent line separator string
* @since 1.7
*/
public static String lineSeparator() {
return lineSeparator;
}
private static String lineSeparator;
/**
* Sets the system properties to the {@code Properties} argument.
*
* First, if there is a security manager, its
* {@code checkPropertiesAccess} method is called with no
* arguments. This may result in a security exception.
* <p>
* The argument becomes the current set of system properties for use
* by the {@link #getProperty(String)} method. If the argument is
* {@code null}, then the current set of system properties is
* forgotten.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param props the new system properties.
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow access
* to the system properties.
* @see #getProperties
* @see java.util.Properties
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertiesAccess()
*/
public static void setProperties(Properties props) {
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
if (props == null) {
Map<String, String> tempProps = SystemProps.initProperties();
VersionProps.init(tempProps);
props = createProperties(tempProps);
}
System.props = props;
}
/**
* Gets the system property indicated by the specified key.
*
* First, if there is a security manager, its
* {@code checkPropertyAccess} method is called with the key as
* its argument. This may result in a SecurityException.
* <p>
* If there is no current set of system properties, a set of system
* properties is first created and initialized in the same manner as
* for the {@code getProperties} method.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param key the name of the system property.
* @return the string value of the system property,
* or {@code null} if there is no property with that key.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @see #setProperty
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
* @see java.lang.System#getProperties()
*/
public static String getProperty(String key) {
checkKey(key);
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key);
}
/**
* Gets the system property indicated by the specified key.
*
* First, if there is a security manager, its
* {@code checkPropertyAccess} method is called with the
* {@code key} as its argument.
* <p>
* If there is no current set of system properties, a set of system
* properties is first created and initialized in the same manner as
* for the {@code getProperties} method.
*
* @param key the name of the system property.
* @param def a default value.
* @return the string value of the system property,
* or the default value if there is no property with that key.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPropertyAccess} method doesn't allow
* access to the specified system property.
* @throws NullPointerException if {@code key} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @see #setProperty
* @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
* @see java.lang.System#getProperties()
*/
public static String getProperty(String key, String def) {
checkKey(key);
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key, def);
}
/**
* Sets the system property indicated by the specified key.
*
* First, if a security manager exists, its
* {@code SecurityManager.checkPermission} method
* is called with a {@code PropertyPermission(key, "write")}
* permission. This may result in a SecurityException being thrown.
* If no exception is thrown, the specified property is set to the given
* value.
*
* @apiNote
* <strong>Changing a standard system property may have unpredictable results
* unless otherwise specified</strong>.
* See {@linkplain #getProperties getProperties} for details.
*
* @param key the name of the system property.
* @param value the value of the system property.
* @return the previous value of the system property,
* or {@code null} if it did not have one.
*
* @throws SecurityException if a security manager exists and its
* {@code checkPermission} method doesn't allow
* setting of the specified property.
* @throws NullPointerException if {@code key} or
* {@code value} is {@code null}.
* @throws IllegalArgumentException if {@code key} is empty.
* @see #getProperty
* @see java.lang.System#getProperty(java.lang.String)
* @see java.lang.System#getProperty(java.lang.String, java.lang.String)
* @see java.util.PropertyPermission
* @see SecurityManager#checkPermission
* @since 1.2
*/
public static String setProperty(String key, String value) {
checkKey(key);
@SuppressWarnings("removal")
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPermission(new PropertyPermission(key,
SecurityConstants.PROPERTY_WRITE_ACTION));
}
return (String) props.setProperty(key, value);
}