Skip to content

[GR-8209] Introduce an option to change the default charset. #3834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.oracle.svm.core.jdk.localization;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.ListResourceBundle;
Expand Down Expand Up @@ -65,8 +66,8 @@ public class BundleContentSubstitutedLocalizationSupport extends LocalizationSup

private final Map<Class<?>, StoredBundle> storedBundles = new ConcurrentHashMap<>();

public BundleContentSubstitutedLocalizationSupport(Locale defaultLocale, Set<Locale> locales, List<String> requestedPatterns, ForkJoinPool pool) {
super(defaultLocale, locales);
public BundleContentSubstitutedLocalizationSupport(Locale defaultLocale, Set<Locale> locales, Charset defaultCharset, List<String> requestedPatterns, ForkJoinPool pool) {
super(defaultLocale, locales, defaultCharset);
this.pool = pool;
this.compressBundlesPatterns = parseCompressBundlePatterns(requestedPatterns);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.text.spi.BreakIteratorProvider;
import java.text.spi.CollatorProvider;
Expand Down Expand Up @@ -134,6 +136,8 @@ public abstract class LocalizationFeature implements Feature {
*/
protected Locale defaultLocale = Locale.getDefault();

private Charset defaultCharset;

protected Set<Locale> allLocales;

protected LocalizationSupport support;
Expand All @@ -150,6 +154,9 @@ public static class Options {
@Option(help = "Default locale of the image, by the default it is the same as the default locale of the image builder.", type = OptionType.User)//
public static final HostedOptionKey<String> DefaultLocale = new HostedOptionKey<>(Locale.getDefault().toLanguageTag());

@Option(help = "Default charset of the image, by the default it is the same as the default charset of the image builder.", type = OptionType.User)//
public static final HostedOptionKey<String> DefaultCharset = new HostedOptionKey<>(Charset.defaultCharset().name());

@Option(help = "Comma separated list of locales to be included into the image. The default locale is included in the list automatically if not present.", type = OptionType.User)//
public static final HostedOptionKey<LocatableMultiOptionValue.Strings> IncludeLocales = new HostedOptionKey<>(new LocatableMultiOptionValue.Strings());

Expand Down Expand Up @@ -228,6 +235,13 @@ public void afterRegistration(AfterRegistrationAccess access) {
allLocales = processLocalesOption();
defaultLocale = parseLocaleFromTag(Options.DefaultLocale.getValue());
UserError.guarantee(defaultLocale != null, "Invalid default locale %s", Options.DefaultLocale.getValue());
try {
defaultCharset = Charset.forName(Options.DefaultCharset.getValue());
VMError.guarantee(defaultCharset.name().equals(Options.DefaultCharset.getValue()),
"Failed to locate charset " + Options.DefaultCharset.getValue() + ", instead " + defaultCharset.name() + " was provided");
} catch (IllegalCharsetNameException | UnsupportedCharsetException ex) {
throw UserError.abort(ex, "Invalid default charset %s", Options.DefaultCharset.getValue());
}
allLocales.add(defaultLocale);
support = selectLocalizationSupport();
ImageSingletons.add(LocalizationSupport.class, support);
Expand All @@ -245,12 +259,12 @@ public void afterRegistration(AfterRegistrationAccess access) {
@Platforms(Platform.HOSTED_ONLY.class)
private LocalizationSupport selectLocalizationSupport() {
if (optimizedMode) {
return new OptimizedLocalizationSupport(defaultLocale, allLocales);
return new OptimizedLocalizationSupport(defaultLocale, allLocales, defaultCharset);
} else if (substituteLoadLookup) {
List<String> requestedPatterns = Options.LocalizationCompressBundles.getValue().values();
return new BundleContentSubstitutedLocalizationSupport(defaultLocale, allLocales, requestedPatterns, compressionPool);
return new BundleContentSubstitutedLocalizationSupport(defaultLocale, allLocales, defaultCharset, requestedPatterns, compressionPool);
}
return new LocalizationSupport(defaultLocale, allLocales);
return new LocalizationSupport(defaultLocale, allLocales, defaultCharset);
}

@Override
Expand Down Expand Up @@ -317,19 +331,19 @@ private static Set<Locale> processLocalesOption() {
* more than this can add additional charsets.
*/
@Platforms(Platform.HOSTED_ONLY.class)
private static void addCharsets() {
private void addCharsets() {
if (Options.AddAllCharsets.getValue()) {
for (Charset c : Charset.availableCharsets().values()) {
addCharset(c);
}
} else {
addCharset(Charset.defaultCharset());
addCharset(Charset.forName("US-ASCII"));
addCharset(Charset.forName("ISO-8859-1"));
addCharset(Charset.forName("UTF-8"));
addCharset(Charset.forName("UTF-16BE"));
addCharset(Charset.forName("UTF-16LE"));
addCharset(Charset.forName("UTF-16"));
addCharset(defaultCharset);
addCharset(StandardCharsets.US_ASCII);
addCharset(StandardCharsets.ISO_8859_1);
addCharset(StandardCharsets.UTF_8);
addCharset(StandardCharsets.UTF_16BE);
addCharset(StandardCharsets.UTF_16LE);
addCharset(StandardCharsets.UTF_16);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ public class LocalizationSupport {

public final ResourceBundle.Control control = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT);

public LocalizationSupport(Locale defaultLocale, Set<Locale> locales) {
public final Charset defaultCharset;

public LocalizationSupport(Locale defaultLocale, Set<Locale> locales, Charset defaultCharset) {
this.defaultLocale = defaultLocale;
this.allLocales = locales.toArray(new Locale[0]);
this.defaultCharset = defaultCharset;
this.supportedLanguageTags = locales.stream().map(Locale::toString).collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.oracle.svm.core.option.SubstrateOptionsParser;
import org.graalvm.collections.Pair;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand All @@ -50,8 +51,8 @@ public class OptimizedLocalizationSupport extends LocalizationSupport {

private final String includeResourceBundlesOption = SubstrateOptionsParser.commandArgument(LocalizationFeature.Options.IncludeResourceBundles, "");

public OptimizedLocalizationSupport(Locale defaultLocale, Set<Locale> locales) {
super(defaultLocale, locales);
public OptimizedLocalizationSupport(Locale defaultLocale, Set<Locale> locales, Charset defaultCharset) {
super(defaultLocale, locales, defaultCharset);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@
@SuppressWarnings({"unused"})
final class Target_java_nio_charset_Charset {

@Alias private static Charset defaultCharset;

@Substitute
private static Charset defaultCharset() {
/*
* The default charset is initialized during native image generation and therefore always
* available without any checks.
*/
return defaultCharset;
return ImageSingletons.lookup(LocalizationSupport.class).defaultCharset;
}

@Substitute
Expand Down