Skip to content

Commit

Permalink
8300916: Re-examine the initialization of JNU Charset in StaticProperty
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, alanb
  • Loading branch information
naotoj committed Jan 30, 2023
1 parent f4592b1 commit 3238139
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
13 changes: 6 additions & 7 deletions src/java.base/share/classes/java/nio/charset/Charset.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
Expand Down Expand Up @@ -27,8 +27,9 @@

import jdk.internal.misc.ThreadTracker;
import jdk.internal.misc.VM;
import jdk.internal.util.StaticProperty;
import jdk.internal.vm.annotation.Stable;
import sun.nio.cs.ThreadLocalCoders;
import sun.security.action.GetPropertyAction;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
Expand All @@ -48,7 +49,6 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.jar.JarFile;


/**
Expand Down Expand Up @@ -628,7 +628,7 @@ public SortedMap<String,Charset> run() {
});
}

private static volatile Charset defaultCharset;
private @Stable static Charset defaultCharset;

/**
* Returns the default charset of this Java virtual machine.
Expand All @@ -651,9 +651,8 @@ public SortedMap<String,Charset> run() {
public static Charset defaultCharset() {
if (defaultCharset == null) {
synchronized (Charset.class) {
String csn = GetPropertyAction
.privilegedGetProperty("file.encoding");
Charset cs = lookup(csn);
// do not look for providers other than the standard one
Charset cs = standardProvider.charsetForName(StaticProperty.fileEncoding());
if (cs != null)
defaultCharset = cs;
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, 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
Expand All @@ -26,7 +26,6 @@
package jdk.internal.util;

import java.util.Properties;
import java.nio.charset.Charset;

/**
* System Property access for internal use only.
Expand Down Expand Up @@ -54,7 +53,6 @@ public final class StaticProperty {
private static final String FILE_ENCODING;
private static final String JAVA_PROPERTIES_DATE;
private static final String SUN_JNU_ENCODING;
private static final Charset jnuCharset;
private static final String JAVA_LOCALE_USE_OLD_ISO_CODES;

private StaticProperty() {}
Expand All @@ -74,7 +72,6 @@ private StaticProperty() {}
FILE_ENCODING = getProperty(props, "file.encoding");
JAVA_PROPERTIES_DATE = getProperty(props, "java.properties.date", null);
SUN_JNU_ENCODING = getProperty(props, "sun.jnu.encoding");
jnuCharset = Charset.forName(SUN_JNU_ENCODING, Charset.defaultCharset());
JAVA_LOCALE_USE_OLD_ISO_CODES = getProperty(props, "java.locale.useOldISOCodes", "");
}

Expand Down Expand Up @@ -236,16 +233,6 @@ public static String jnuEncoding() {
return SUN_JNU_ENCODING;
}

/**
* {@return {@code Charset} for {@code sun.jnu.encoding} system property}
*
* <strong>If {@code sun.jnu.encoding} system property has invalid
* encoding name, {@link Charset#defaultCharset()} is returned.</strong>
*/
public static Charset jnuCharset() {
return jnuCharset;
}

/**
* {@return the {@code java.locale.useOldISOCodes} system property}
*
Expand Down
15 changes: 7 additions & 8 deletions src/java.base/unix/classes/java/lang/ProcessEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
Expand Down Expand Up @@ -54,10 +54,9 @@

package java.lang;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import jdk.internal.util.StaticProperty;

import static java.lang.ProcessImpl.JNU_CHARSET;


final class ProcessEnvironment
Expand Down Expand Up @@ -165,7 +164,7 @@ public static Variable valueOfQueryOnly(Object str) {
}

public static Variable valueOfQueryOnly(String str) {
return new Variable(str, str.getBytes(StaticProperty.jnuCharset()));
return new Variable(str, str.getBytes(JNU_CHARSET));
}

public static Variable valueOf(String str) {
Expand All @@ -174,7 +173,7 @@ public static Variable valueOf(String str) {
}

public static Variable valueOf(byte[] bytes) {
return new Variable(new String(bytes, StaticProperty.jnuCharset()), bytes);
return new Variable(new String(bytes, JNU_CHARSET), bytes);
}

public int compareTo(Variable variable) {
Expand All @@ -198,7 +197,7 @@ public static Value valueOfQueryOnly(Object str) {
}

public static Value valueOfQueryOnly(String str) {
return new Value(str, str.getBytes(StaticProperty.jnuCharset()));
return new Value(str, str.getBytes(JNU_CHARSET));
}

public static Value valueOf(String str) {
Expand All @@ -207,7 +206,7 @@ public static Value valueOf(String str) {
}

public static Value valueOf(byte[] bytes) {
return new Value(new String(bytes, StaticProperty.jnuCharset()), bytes);
return new Value(new String(bytes, JNU_CHARSET), bytes);
}

public int compareTo(Value value) {
Expand Down
14 changes: 9 additions & 5 deletions src/java.base/unix/classes/java/lang/ProcessImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
Expand Down Expand Up @@ -69,6 +69,10 @@ final class ProcessImpl extends Process {
// Linux platforms support a normal (non-forcible) kill signal.
static final boolean SUPPORTS_NORMAL_TERMINATION = true;

// Cache for JNU Charset. The encoding name is guaranteed
// to be supported in this environment.
static final Charset JNU_CHARSET = Charset.forName(StaticProperty.jnuEncoding());

private final int pid;
private final ProcessHandleImpl processHandle;
private int exitcode;
Expand Down Expand Up @@ -114,11 +118,11 @@ LaunchMechanism launchMechanism() {
LaunchMechanism lm;
if (s == null) {
lm = defaultLaunchMechanism;
s = lm.name().toLowerCase(Locale.ENGLISH);
s = lm.name().toLowerCase(Locale.ROOT);
} else {
try {
lm = LaunchMechanism.valueOf(
s.toUpperCase(Locale.ENGLISH));
s.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
lm = null;
}
Expand Down Expand Up @@ -152,7 +156,7 @@ static Platform get() {
private static byte[] toCString(String s) {
if (s == null)
return null;
byte[] bytes = s.getBytes(StaticProperty.jnuCharset());
byte[] bytes = s.getBytes(JNU_CHARSET);
byte[] result = new byte[bytes.length + 1];
System.arraycopy(bytes, 0,
result, 0,
Expand All @@ -176,7 +180,7 @@ static Process start(String[] cmdarray,
byte[][] args = new byte[cmdarray.length-1][];
int size = args.length; // For added NUL bytes
for (int i = 0; i < args.length; i++) {
args[i] = cmdarray[i+1].getBytes(StaticProperty.jnuCharset());
args[i] = cmdarray[i+1].getBytes(JNU_CHARSET);
size += args[i].length;
}
byte[] argBlock = new byte[size];
Expand Down

1 comment on commit 3238139

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.