65
65
66
66
/*[IF JAVA_SPEC_VERSION >= 20]*/
67
67
import java .lang .reflect .Field ;
68
- /*[ENDIF] JAVA_SPEC_VERSION >= 20 */
69
- /*[IF JAVA_SPEC_VERSION >= 17]*/
70
68
import jdk .internal .util .SystemProps ;
71
- /*[ENDIF] JAVA_SPEC_VERSION >= 17 */
69
+ /*[ENDIF] JAVA_SPEC_VERSION >= 20 */
72
70
73
71
/*[IF JAVA_SPEC_VERSION >= 24]*/
74
72
import java .net .URL ;
@@ -174,6 +172,10 @@ public final class System {
174
172
*/
175
173
private static Charset consoleDefaultCharset ;
176
174
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */
175
+ /*[IF JAVA_SPEC_VERSION >= 19]*/
176
+ private static String stdoutProp ;
177
+ private static String stderrProp ;
178
+ /*[ENDIF] JAVA_SPEC_VERSION >= 19 */
177
179
178
180
/*[IF JAVA_SPEC_VERSION >= 9]*/
179
181
static java .lang .ModuleLayer bootLayer ;
@@ -234,7 +236,7 @@ public final class System {
234
236
*/
235
237
static Charset getCharset (boolean isStdout , boolean fallback ) {
236
238
/*[IF JAVA_SPEC_VERSION >= 19]*/
237
- String primary = internalGetProperties (). getProperty ( isStdout ? "stdout.encoding" : "stderr.encoding" ); //$NON-NLS-1$ //$NON-NLS-2$
239
+ String primary = isStdout ? stdoutProp : stderrProp ;
238
240
/*[ELSE] JAVA_SPEC_VERSION >= 19 */
239
241
String primary = internalGetProperties ().getProperty (isStdout ? "sun.stdout.encoding" : "sun.stderr.encoding" ); //$NON-NLS-1$ //$NON-NLS-2$
240
242
/*[ENDIF] JAVA_SPEC_VERSION >= 19 */
@@ -277,6 +279,20 @@ static PrintStream createConsole(FileDescriptor desc, Charset charset) {
277
279
BufferedOutputStream bufStream = new BufferedOutputStream (new FileOutputStream (desc ));
278
280
Charset consoleCharset = charset == null ? consoleDefaultCharset : charset ;
279
281
282
+ /*[IF JAVA_SPEC_VERSION >= 19]*/
283
+ Properties props = internalGetProperties ();
284
+ // If the user didn't set the encoding property, set it now.
285
+ if (FileDescriptor .out == desc ) {
286
+ if (null == stdoutProp ) {
287
+ props .put ("stdout.encoding" , consoleCharset .name ()); //$NON-NLS-1$
288
+ }
289
+ } else if (FileDescriptor .err == desc ) {
290
+ if (null == stderrProp ) {
291
+ props .put ("stderr.encoding" , consoleCharset .name ()); //$NON-NLS-1$
292
+ }
293
+ }
294
+ /*[ENDIF] JAVA_SPEC_VERSION >= 19 */
295
+
280
296
/*[IF PLATFORM-mz31 | PLATFORM-mz64]*/
281
297
return ConsolePrintStream .localize (bufStream , true , consoleCharset );
282
298
/*[ELSE]*/
@@ -307,6 +323,12 @@ static void finalizeConsoleEncoding() {
307
323
setOut (createConsole (FileDescriptor .out , stdoutCharset ));
308
324
}
309
325
}
326
+
327
+ /*[IF JAVA_SPEC_VERSION >= 19]*/
328
+ // Cache the final system property values so they can be restored if ensureProperties(false) is called.
329
+ stdoutProp = systemProperties .getProperty ("stdout.encoding" ); //$NON-NLS-1$
330
+ stderrProp = systemProperties .getProperty ("stderr.encoding" ); //$NON-NLS-1$
331
+ /*[ENDIF] JAVA_SPEC_VERSION >= 19 */
310
332
}
311
333
/*[ELSE]*/
312
334
/*[IF Sidecar18-SE-OpenJ9]*/
@@ -367,9 +389,9 @@ static void afterClinitInitialization() {
367
389
// Fill in the properties from the VM information.
368
390
ensureProperties (true );
369
391
370
- /*[IF JAVA_SPEC_VERSION = = 11]*/
392
+ /*[IF JAVA_SPEC_VERSION > = 11]*/
371
393
initJCLPlatformEncoding ();
372
- /*[ENDIF] JAVA_SPEC_VERSION = = 11 */
394
+ /*[ENDIF] JAVA_SPEC_VERSION > = 11 */
373
395
374
396
/*[REM] Initialize the JITHelpers needed in J9VMInternals since the class can't do it itself */
375
397
try {
@@ -707,7 +729,7 @@ private static void ensureProperties(boolean isInitialization) {
707
729
/*[ENDIF] OpenJ9-RawBuild */
708
730
709
731
/*[IF JAVA_SPEC_VERSION > 11]*/
710
- Map <String , String > initializedProperties = new HashMap < >();
732
+ Map <String , String > initializedProperties = new Hashtable < String , String >();
711
733
/*[ELSE] JAVA_SPEC_VERSION > 11
712
734
Properties initializedProperties = new Properties();
713
735
/*[ENDIF] JAVA_SPEC_VERSION > 11 */
@@ -719,11 +741,9 @@ private static void ensureProperties(boolean isInitialization) {
719
741
if (osEncoding != null ) {
720
742
initializedProperties .put ("os.encoding" , osEncoding ); //$NON-NLS-1$
721
743
}
722
- initializedProperties .put ("ibm.system.encoding" , platformEncoding ); //$NON-NLS-1$
723
- /*[IF JAVA_SPEC_VERSION < 17]*/
724
744
/*[PR The launcher apparently needs sun.jnu.encoding property or it does not work]*/
745
+ initializedProperties .put ("ibm.system.encoding" , platformEncoding ); //$NON-NLS-1$
725
746
initializedProperties .put ("sun.jnu.encoding" , platformEncoding ); //$NON-NLS-1$
726
- /*[ENDIF] JAVA_SPEC_VERSION < 17 */
727
747
/*[IF JAVA_SPEC_VERSION == 8]*/
728
748
initializedProperties .put ("file.encoding.pkg" , "sun.io" ); //$NON-NLS-1$ //$NON-NLS-2$
729
749
/*[ENDIF] JJAVA_SPEC_VERSION == 8 */
@@ -749,9 +769,6 @@ private static void ensureProperties(boolean isInitialization) {
749
769
initializedProperties .put ("jfr.unsupported.vm" , "true" ); //$NON-NLS-1$ //$NON-NLS-2$
750
770
/*[ENDIF] JFR_SUPPORT */
751
771
752
- /*[IF JAVA_SPEC_VERSION >= 17]*/
753
- initializedProperties .putAll (SystemProps .initProperties ());
754
- /*[ELSE] JAVA_SPEC_VERSION >= 17 */
755
772
String [] list = getPropertyList ();
756
773
for (int i = 0 ; i < list .length ; i += 2 ) {
757
774
String key = list [i ];
@@ -762,12 +779,43 @@ private static void ensureProperties(boolean isInitialization) {
762
779
initializedProperties .put (key , list [i +1 ]);
763
780
}
764
781
initializedProperties .put ("file.encoding" , fileEncoding ); //$NON-NLS-1$
782
+
783
+ /*[IF JAVA_SPEC_VERSION >= 17]*/
784
+ /* Set native.encoding after setting all the defined properties, it can't be modified by using -D on the command line */
785
+ initializedProperties .put ("native.encoding" , platformEncoding ); //$NON-NLS-1$
765
786
/*[ENDIF] JAVA_SPEC_VERSION >= 17 */
766
787
767
788
/*[IF (JAVA_SPEC_VERSION >= 21) & (PLATFORM-mz31 | PLATFORM-mz64)]*/
768
789
initializedProperties .put ("com.ibm.autocvt" , zOSAutoConvert ); //$NON-NLS-1$
769
790
/*[ENDIF] (JAVA_SPEC_VERSION >= 21) & (PLATFORM-mz31 | PLATFORM-mz64) */
770
791
792
+ /*[IF JAVA_SPEC_VERSION >= 19]*/
793
+ if (null != stdoutProp ) {
794
+ // Reinitialize required properties if ensureProperties(false) is called.
795
+ initializedProperties .put ("stdout.encoding" , stdoutProp ); //$NON-NLS-1$
796
+ } else {
797
+ stdoutProp = initializedProperties .get ("stdout.encoding" ); //$NON-NLS-1$
798
+ if (null == stdoutProp ) {
799
+ stdoutProp = initializedProperties .get ("sun.stdout.encoding" ); //$NON-NLS-1$
800
+ if (null != stdoutProp ) {
801
+ initializedProperties .put ("stdout.encoding" , stdoutProp ); //$NON-NLS-1$
802
+ }
803
+ }
804
+ }
805
+ if (null != stderrProp ) {
806
+ // Reinitialize required properties if ensureProperties(false) is called.
807
+ initializedProperties .put ("stderr.encoding" , stderrProp ); //$NON-NLS-1$
808
+ } else {
809
+ stderrProp = initializedProperties .get ("stderr.encoding" );
810
+ if (null == stderrProp ) { //$NON-NLS-1$
811
+ stderrProp = initializedProperties .get ("sun.stderr.encoding" ); //$NON-NLS-1$
812
+ if (null != stderrProp ) {
813
+ initializedProperties .put ("stderr.encoding" , stderrProp ); //$NON-NLS-1$
814
+ }
815
+ }
816
+ }
817
+ /*[ENDIF] JAVA_SPEC_VERSION >= 19 */
818
+
771
819
/* java.lang.VersionProps.init() eventually calls into System.setProperty() where propertiesInitialized needs to be true */
772
820
propertiesInitialized = true ;
773
821
@@ -1040,7 +1088,6 @@ public static String setProperty(String prop, String value) {
1040
1088
return (String )systemProperties .setProperty (prop , value );
1041
1089
}
1042
1090
1043
- /*[IF JAVA_SPEC_VERSION < 17]*/
1044
1091
/**
1045
1092
* Answers an array of Strings containing key..value pairs
1046
1093
* (in consecutive array elements) which represent the
@@ -1050,14 +1097,13 @@ public static String setProperty(String prop, String value) {
1050
1097
* @return the default values for the system properties.
1051
1098
*/
1052
1099
private static native String [] getPropertyList ();
1053
- /*[ENDIF] JAVA_SPEC_VERSION < 17 */
1054
1100
1055
- /*[IF JAVA_SPEC_VERSION = = 11]*/
1101
+ /*[IF JAVA_SPEC_VERSION > = 11]*/
1056
1102
/**
1057
1103
* Invoke JCL native to initialize platform encoding explicitly.
1058
1104
*/
1059
1105
private static native void initJCLPlatformEncoding ();
1060
- /*[ENDIF] JAVA_SPEC_VERSION = = 11 */
1106
+ /*[ENDIF] JAVA_SPEC_VERSION > = 11 */
1061
1107
1062
1108
/**
1063
1109
* Before propertiesInitialized is set to true,
0 commit comments