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

Print this page
rev 3186 : 6880112: Project Coin: Port JDK core library code to use diamond operator


  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 sun.misc.MessageUtils;
  42 import sun.nio.cs.HistoricallyNamedCharset;
  43 import sun.nio.cs.ArrayDecoder;
  44 import sun.nio.cs.ArrayEncoder;
  45 
  46 /**
  47  * Utility class for string encoding and decoding.
  48  */
  49 
  50 class StringCoding {
  51 
  52     private StringCoding() { }
  53 
  54     /** The cached coders for each thread */
  55     private final static ThreadLocal<SoftReference<StringDecoder>> decoder =
  56         new ThreadLocal<SoftReference<StringDecoder>>();
  57     private final static ThreadLocal<SoftReference<StringEncoder>> encoder =
  58         new ThreadLocal<SoftReference<StringEncoder>>();
  59 
  60     private static boolean warnUnsupportedCharset = true;
  61 
  62     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
  63         SoftReference<T> sr = tl.get();
  64         if (sr == null)
  65             return null;
  66         return sr.get();
  67     }
  68 
  69     private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
  70         tl.set(new SoftReference<T>(ob));
  71     }
  72 
  73     // Trim the given byte array to the given length
  74     //
  75     private static byte[] safeTrim(byte[] ba, int len, Charset cs, boolean isTrusted) {
  76         if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
  77             return ba;
  78         else
  79             return Arrays.copyOf(ba, len);
  80     }
  81 
  82     // Trim the given char array to the given length
  83     //
  84     private static char[] safeTrim(char[] ca, int len,
  85                                    Charset cs, boolean isTrusted) {
  86         if (len == ca.length && (isTrusted || System.getSecurityManager() == null))
  87             return ca;
  88         else
  89             return Arrays.copyOf(ca, len);
  90     }




  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 sun.misc.MessageUtils;
  42 import sun.nio.cs.HistoricallyNamedCharset;
  43 import sun.nio.cs.ArrayDecoder;
  44 import sun.nio.cs.ArrayEncoder;
  45 
  46 /**
  47  * Utility class for string encoding and decoding.
  48  */
  49 
  50 class StringCoding {
  51 
  52     private StringCoding() { }
  53 
  54     /** The cached coders for each thread */
  55     private final static ThreadLocal<SoftReference<StringDecoder>> decoder =
  56         new ThreadLocal<>();
  57     private final static ThreadLocal<SoftReference<StringEncoder>> encoder =
  58         new ThreadLocal<>();
  59 
  60     private static boolean warnUnsupportedCharset = true;
  61 
  62     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
  63         SoftReference<T> sr = tl.get();
  64         if (sr == null)
  65             return null;
  66         return sr.get();
  67     }
  68 
  69     private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
  70         tl.set(new SoftReference<>(ob));
  71     }
  72 
  73     // Trim the given byte array to the given length
  74     //
  75     private static byte[] safeTrim(byte[] ba, int len, Charset cs, boolean isTrusted) {
  76         if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
  77             return ba;
  78         else
  79             return Arrays.copyOf(ba, len);
  80     }
  81 
  82     // Trim the given char array to the given length
  83     //
  84     private static char[] safeTrim(char[] ca, int len,
  85                                    Charset cs, boolean isTrusted) {
  86         if (len == ca.length && (isTrusted || System.getSecurityManager() == null))
  87             return ca;
  88         else
  89             return Arrays.copyOf(ca, len);
  90     }