mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cache Native2AsciiCharset instances (there are too many strings retained via charset name otherwise)
This commit is contained in:
@@ -17,18 +17,21 @@
|
||||
package com.intellij.lang.properties.charset;
|
||||
|
||||
import java.nio.charset.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* @author Alexey
|
||||
*/
|
||||
|
||||
public class Native2AsciiCharset extends Charset {
|
||||
private static final String[] ALIASES = new String[0];
|
||||
private final Charset myBaseCharset;
|
||||
@SuppressWarnings({"HardCodedStringLiteral"}) private static final String NAME_PREFIX = "NATIVE_TO_ASCII_";
|
||||
@SuppressWarnings({"HardCodedStringLiteral"}) private static final String DEFAULT_ENCODING_NAME = "ISO-8859-1";
|
||||
|
||||
private Native2AsciiCharset(String canonicalName) {
|
||||
super(canonicalName, null);
|
||||
super(canonicalName, ALIASES);
|
||||
String baseCharsetName = canonicalName.substring(NAME_PREFIX.length());
|
||||
Charset baseCharset = null;
|
||||
try {
|
||||
@@ -70,14 +73,21 @@ public class Native2AsciiCharset extends Charset {
|
||||
if (baseCharsetName == null) baseCharsetName = DEFAULT_ENCODING_NAME;
|
||||
return NAME_PREFIX + baseCharsetName;
|
||||
}
|
||||
|
||||
public static Charset forName(String charsetName) {
|
||||
if (charsetName.startsWith(NAME_PREFIX)) {
|
||||
return new Native2AsciiCharset(charsetName);
|
||||
Native2AsciiCharset cached = cache.get(charsetName);
|
||||
if (cached == null) {
|
||||
cached = new Native2AsciiCharset(charsetName);
|
||||
Native2AsciiCharset prev = cache.putIfAbsent(charsetName, cached);
|
||||
if (prev != null) cached = prev;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static Charset wrap(Charset baseCharset) {
|
||||
return new Native2AsciiCharset(NAME_PREFIX + baseCharset.name());
|
||||
return forName(NAME_PREFIX + baseCharset.name());
|
||||
}
|
||||
|
||||
public static Charset nativeToBaseCharset(Charset charset) {
|
||||
@@ -86,4 +96,6 @@ public class Native2AsciiCharset extends Charset {
|
||||
}
|
||||
return charset;
|
||||
}
|
||||
|
||||
private static final ConcurrentMap<String, Native2AsciiCharset> cache = new ConcurrentHashMap<String, Native2AsciiCharset>();
|
||||
}
|
||||
Reference in New Issue
Block a user