46
46
import java .util .HashMap ;
47
47
import java .util .Locale ;
48
48
import java .util .Map ;
49
+ import java .util .concurrent .ConcurrentHashMap ;
50
+ import java .util .concurrent .ConcurrentMap ;
49
51
50
52
import com .ibm .icu .charset .CharsetICU ;
51
53
import com .oracle .graal .python .charset .PythonRawUnicodeEscapeCharset ;
56
58
* Utility class for mapping Python encodings to Java charsets
57
59
*/
58
60
public class CharsetMapping {
59
- private static final Map <String , Charset > JAVA_CHARSETS = new HashMap <>();
61
+ private static final ConcurrentMap <String , Charset > JAVA_CHARSETS = new ConcurrentHashMap <>();
62
+ // Name maps are populated by static initializer and are immutable afterwards
60
63
private static final Map <String , String > CHARSET_NAME_MAP = new HashMap <>();
61
64
private static final Map <String , String > CHARSET_NAME_MAP_REVERSE = new HashMap <>();
62
65
@@ -80,26 +83,23 @@ public static String normalize(String encoding) {
80
83
}
81
84
82
85
private static Charset getJavaCharset (String name ) {
83
- Charset charset = JAVA_CHARSETS .get (name );
84
- if (charset == null ) {
86
+ return JAVA_CHARSETS .computeIfAbsent (name , key -> {
85
87
// Important: When adding additional ICU4J charset, the implementation class needs to be
86
88
// added to reflect-config.json
87
89
if (name .equals ("UTF-7" ) || name .equals ("HZ" )) {
88
90
try {
89
- charset = CharsetICU .forNameICU (name );
91
+ return CharsetICU .forNameICU (name );
90
92
} catch (UnsupportedCharsetException e ) {
91
- // Let it stay null
93
+ return null ;
92
94
}
93
95
} else {
94
96
try {
95
- charset = Charset .forName (name );
97
+ return Charset .forName (name );
96
98
} catch (UnsupportedCharsetException e ) {
97
- // Let it stay null
99
+ return null ;
98
100
}
99
101
}
100
- JAVA_CHARSETS .put (name , charset );
101
- }
102
- return charset ;
102
+ });
103
103
}
104
104
105
105
private static void addMapping (String pythonName , String javaName ) {
0 commit comments