25
26 package java.lang;
27
28 import java.io.UnsupportedEncodingException;
29 import java.lang.ref.SoftReference;
30 import java.nio.ByteBuffer;
31 import java.nio.CharBuffer;
32 import java.nio.charset.Charset;
33 import java.nio.charset.CharsetDecoder;
34 import java.nio.charset.CharsetEncoder;
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
46 import static java.lang.String.LATIN1;
47 import static java.lang.String.UTF16;
48 import static java.lang.String.COMPACT_STRINGS;
49
50 /**
51 * Utility class for string encoding and decoding.
52 */
53
54 class StringCoding {
55
56 private StringCoding() { }
57
58 /** The cached coders for each thread */
59 private static final ThreadLocal<SoftReference<StringDecoder>> decoder =
60 new ThreadLocal<>();
61 private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
62 new ThreadLocal<>();
63
64 private static final Charset ISO_8859_1 = Charset.forName("iso-8859-1");
65 private static final Charset US_ASCII = Charset.forName("us-ascii");
66 private static final Charset UTF_8 = Charset.forName("utf-8");
67
68 private static boolean warnUnsupportedCharset = true;
69
70 private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
71 SoftReference<T> sr = tl.get();
72 if (sr == null)
73 return null;
74 return sr.get();
75 }
76
77 private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
78 tl.set(new SoftReference<>(ob));
79 }
80
81 // Trim the given byte array to the given length
82 //
83 private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
84 if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
85 return ba;
86 else
|
25
26 package java.lang;
27
28 import java.io.UnsupportedEncodingException;
29 import java.lang.ref.SoftReference;
30 import java.nio.ByteBuffer;
31 import java.nio.CharBuffer;
32 import java.nio.charset.Charset;
33 import java.nio.charset.CharsetDecoder;
34 import java.nio.charset.CharsetEncoder;
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
51 /**
52 * Utility class for string encoding and decoding.
53 */
54
55 class StringCoding {
56
57 private StringCoding() { }
58
59 /** The cached coders for each thread */
60 private static final ThreadLocal<SoftReference<StringDecoder>> decoder =
61 new ThreadLocal<>();
62 private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
63 new ThreadLocal<>();
64
65 private static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
66 private static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
67 private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
68
69 private static boolean warnUnsupportedCharset = true;
70
71 private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
72 SoftReference<T> sr = tl.get();
73 if (sr == null)
74 return null;
75 return sr.get();
76 }
77
78 private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
79 tl.set(new SoftReference<>(ob));
80 }
81
82 // Trim the given byte array to the given length
83 //
84 private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
85 if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
86 return ba;
87 else
|