< prev index next >

src/java.base/share/classes/java/lang/StringCoding.java

Print this page
8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
Reviewed-by: sherman


  35 import java.nio.charset.CharacterCodingException;
  36 import java.nio.charset.CoderResult;
  37 import java.nio.charset.CodingErrorAction;
  38 import java.nio.charset.IllegalCharsetNameException;
  39 import java.nio.charset.UnsupportedCharsetException;
  40 import java.util.Arrays;
  41 import jdk.internal.HotSpotIntrinsicCandidate;
  42 import sun.nio.cs.HistoricallyNamedCharset;
  43 import sun.nio.cs.ArrayDecoder;
  44 import sun.nio.cs.ArrayEncoder;
  45 import sun.nio.cs.StandardCharsets;
  46 
  47 import static java.lang.String.LATIN1;
  48 import static java.lang.String.UTF16;
  49 import static java.lang.String.COMPACT_STRINGS;
  50 import static java.lang.Character.isSurrogate;
  51 import static java.lang.Character.highSurrogate;
  52 import static java.lang.Character.lowSurrogate;
  53 import static java.lang.Character.isSupplementaryCodePoint;
  54 import static java.lang.StringUTF16.putChar;



  55 
  56 /**
  57  * Utility class for string encoding and decoding.
  58  */
  59 
  60 class StringCoding {
  61 
  62     private StringCoding() { }
  63 
  64     /** The cached coders for each thread */
  65     private static final ThreadLocal<SoftReference<StringDecoder>> decoder =
  66         new ThreadLocal<>();
  67     private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
  68         new ThreadLocal<>();
  69 
  70     private static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
  71     private static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
  72     private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
  73 
  74     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
  75         SoftReference<T> sr = tl.get();
  76         if (sr == null)
  77             return null;
  78         return sr.get();
  79     }
  80 
  81     private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
  82         tl.set(new SoftReference<>(ob));
  83     }
  84 
  85     // Trim the given byte array to the given length
  86     private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
  87         if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
  88             return ba;
  89         else
  90             return Arrays.copyOf(ba, len);
  91     }
  92 




  35 import java.nio.charset.CharacterCodingException;
  36 import java.nio.charset.CoderResult;
  37 import java.nio.charset.CodingErrorAction;
  38 import java.nio.charset.IllegalCharsetNameException;
  39 import java.nio.charset.UnsupportedCharsetException;
  40 import java.util.Arrays;
  41 import jdk.internal.HotSpotIntrinsicCandidate;
  42 import sun.nio.cs.HistoricallyNamedCharset;
  43 import sun.nio.cs.ArrayDecoder;
  44 import sun.nio.cs.ArrayEncoder;
  45 import sun.nio.cs.StandardCharsets;
  46 
  47 import static java.lang.String.LATIN1;
  48 import static java.lang.String.UTF16;
  49 import static java.lang.String.COMPACT_STRINGS;
  50 import static java.lang.Character.isSurrogate;
  51 import static java.lang.Character.highSurrogate;
  52 import static java.lang.Character.lowSurrogate;
  53 import static java.lang.Character.isSupplementaryCodePoint;
  54 import static java.lang.StringUTF16.putChar;
  55 import static java.nio.charset.StandardCharsets.ISO_8859_1;
  56 import static java.nio.charset.StandardCharsets.US_ASCII;
  57 import static java.nio.charset.StandardCharsets.UTF_8;
  58 
  59 /**
  60  * Utility class for string encoding and decoding.
  61  */
  62 
  63 class StringCoding {
  64 
  65     private StringCoding() { }
  66 
  67     /** The cached coders for each thread */
  68     private static final ThreadLocal<SoftReference<StringDecoder>> decoder =
  69         new ThreadLocal<>();
  70     private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
  71         new ThreadLocal<>();




  72 
  73     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
  74         SoftReference<T> sr = tl.get();
  75         if (sr == null)
  76             return null;
  77         return sr.get();
  78     }
  79 
  80     private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
  81         tl.set(new SoftReference<>(ob));
  82     }
  83 
  84     // Trim the given byte array to the given length
  85     private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
  86         if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
  87             return ba;
  88         else
  89             return Arrays.copyOf(ba, len);
  90     }
  91 


< prev index next >