Skip to content

Commit

Permalink
Merge pull request #16598 from pshipton/defCharset2
Browse files Browse the repository at this point in the history
On jdk11+ don't call Charset.defaultEncoding() at startup
  • Loading branch information
keithc-ca authored Jan 31, 2023
2 parents 3d97f35 + c2a8a68 commit 50d0f1a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
32 changes: 20 additions & 12 deletions jcl/src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*[INCLUDE-IF Sidecar18-SE]*/
/*******************************************************************************
* Copyright (c) 1998, 2022 IBM Corp. and others
* Copyright (c) 1998, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -370,27 +370,35 @@ static void afterClinitInitialization() {
*/
String enableSharingInSubstringWhenOffsetIsZeroProperty = internalGetProperties().getProperty("java.lang.string.substring.nocopy"); //$NON-NLS-1$
String.enableSharingInSubstringWhenOffsetIsZero = enableSharingInSubstringWhenOffsetIsZeroProperty == null || enableSharingInSubstringWhenOffsetIsZeroProperty.equalsIgnoreCase("false"); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION < 17 */

// Set up standard in, out, and err.
/*[PR CMVC 193070] - OTT:Java 8 Test_JITHelpers test_getSuperclass NoSuchMet*/
/*[PR JAZZ 58297] - continue with the rules defined by JAZZ 57070 - Build a Java 8 J9 JCL using the SIDECAR18 preprocessor configuration */
/*[IF JAVA_SPEC_VERSION == 8]*/
// Check the default encoding
/*[Bug 102075] J2SE Setting -Dfile.encoding=junk fails to run*/
/*[IF JAVA_SPEC_VERSION >= 11]*/
StringCoding.encode(String.LATIN1, new byte[1]);
/*[ELSE]*/
StringCoding.encode(new char[1], 0, 1);
/*[ENDIF] JAVA_SPEC_VERSION >= 11 */
/*[ENDIF] JAVA_SPEC_VERSION < 17 */
/*[ENDIF] JAVA_SPEC_VERSION == 8 */

// Set up standard in, out, and err.
/*[IF Sidecar18-SE-OpenJ9]*/
Properties props = internalGetProperties();
/*[IF JAVA_SPEC_VERSION >= 11]*/
/*[IF JAVA_SPEC_VERSION >= 18]*/
consoleDefaultEncoding = props.getProperty("native.encoding"); //$NON-NLS-1$
consoleDefaultCharset = Charset.forName(consoleDefaultEncoding, sun.nio.cs.US_ASCII.INSTANCE);
consoleDefaultCharset = Charset.forName(consoleDefaultEncoding, sun.nio.cs.UTF_8.INSTANCE);
/*[ELSE] JAVA_SPEC_VERSION >= 18 */
consoleDefaultCharset = Charset.defaultCharset();
String fileEncodingProp = props.getProperty("file.encoding"); //$NON-NLS-1$
// Do not call Charset.defaultEncoding() since this would initialize the default encoding
// before the jdk.charset module is loaded.
try {
if (Charset.isSupported(fileEncodingProp)) {
consoleDefaultCharset = Charset.forName(fileEncodingProp);
}
} catch (IllegalArgumentException e) {
// ignore
}
if (consoleDefaultCharset == null) {
consoleDefaultCharset = sun.nio.cs.UTF_8.INSTANCE;
}
/*[IF PLATFORM-mz31|PLATFORM-mz64]*/
try {
consoleDefaultEncoding = props.getProperty("console.encoding"); //$NON-NLS-1$
Expand All @@ -402,7 +410,7 @@ static void afterClinitInitialization() {
// use the defaultCharset()
}
/*[ELSE] PLATFORM-mz31|PLATFORM-mz64 */
consoleDefaultEncoding = props.getProperty("file.encoding"); //$NON-NLS-1$
consoleDefaultEncoding = fileEncodingProp;
/*[ENDIF] PLATFORM-mz31|PLATFORM-mz64 */
/*[ENDIF] JAVA_SPEC_VERSION >= 18 */
/* consoleDefaultCharset must be initialized before calling getCharset() */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<!--
Copyright (c) 2022, 2022 IBM Corp. and others
Copyright (c) 2022, 2023 IBM Corp. and others
This program and the accompanying materials are made available under
the terms of the Eclipse Public License 2.0 which accompanies this
distribution and is available at https://www.eclipse.org/legal/epl-2.0/
or the Apache License, Version 2.0 which accompanies this distribution and
is available at https://www.apache.org/licenses/LICENSE-2.0.
This program and the accompanying materials are made available under
the terms of the Eclipse Public License 2.0 which accompanies this
distribution and is available at https://www.eclipse.org/legal/epl-2.0/
or the Apache License, Version 2.0 which accompanies this distribution and
is available at https://www.apache.org/licenses/LICENSE-2.0.
This Source Code may also be made available under the following
Secondary Licenses when the conditions for such availability set
forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
General Public License, version 2 with the GNU Classpath
Exception [1] and GNU General Public License, version 2 with the
OpenJDK Assembly Exception [2].
This Source Code may also be made available under the following
Secondary Licenses when the conditions for such availability set
forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
General Public License, version 2 with the GNU Classpath
Exception [1] and GNU General Public License, version 2 with the
OpenJDK Assembly Exception [2].
[1] https://www.gnu.org/software/classpath/license.html
[2] https://openjdk.org/legal/assembly-exception.html
[1] https://www.gnu.org/software/classpath/license.html
[2] https://openjdk.org/legal/assembly-exception.html
SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
-->
<!DOCTYPE suite SYSTEM "cmdlinetester.dtd">

Expand Down Expand Up @@ -54,4 +54,12 @@
<command>$EXE$ $OPENS$ $CP$ -D$DEFAULT_ENCODING_PROP$=$ENCODING2$ $TARGET$ err $ENCODING2$</command>
<return type="success" value="0" />
</test>

<test id="With -Dfile.encoding=CP943C default Charset is x-IBM943C">
<command>$EXE$ -Dfile.encoding=CP943C -cp $Q$$JARPATH$$Q$ DefaultCharset</command>
<output regex="no" type="success">x-IBM943C</output>
<output type="failure" caseSensitive="no" regex="no">Unhandled Exception</output>
<output type="failure" caseSensitive="yes" regex="no">Exception:</output>
<output type="failure" caseSensitive="yes" regex="no">Processing dump event</output>
</test>
</suite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2023, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

import java.nio.charset.Charset;

public class DefaultCharset {

public static void main(String[] args) {
System.out.println(Charset.defaultCharset());
}
}

0 comments on commit 50d0f1a

Please sign in to comment.