src/share/classes/sun/awt/datatransfer/DataTransferer.java

Print this page

        

*** 55,64 **** --- 55,65 ---- import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import java.nio.charset.IllegalCharsetNameException; + import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
*** 162,172 **** tempSet.add("ISO-8859-1"); tempSet.add("UTF-8"); tempSet.add("UTF-16BE"); tempSet.add("UTF-16LE"); tempSet.add("UTF-16"); ! tempSet.add(getDefaultTextCharset()); return Collections.unmodifiableSortedSet(tempSet); } } /** --- 163,173 ---- tempSet.add("ISO-8859-1"); tempSet.add("UTF-8"); tempSet.add("UTF-16BE"); tempSet.add("UTF-16LE"); tempSet.add("UTF-16"); ! tempSet.add(Charset.defaultCharset().name()); return Collections.unmodifiableSortedSet(tempSet); } } /**
*** 176,191 **** * entries may be added during the life of the JRE for text/<other> types. */ private static final Map<String, Boolean> textMIMESubtypeCharsetSupport; /** - * Cache of the platform default encoding as specified in the - * "file.encoding" system property. - */ - private static String defaultEncoding; - - /** * A collection of all natives listed in flavormap.properties with * a primary MIME type of "text". */ private static final Set<Long> textNatives = Collections.synchronizedSet(new HashSet<>()); --- 177,186 ----
*** 279,299 **** return null; } String encoding = flavor.getParameter("charset"); ! return (encoding != null) ? encoding : getDefaultTextCharset(); ! } ! ! /** ! * Returns the platform's default character encoding. ! */ ! public static String getDefaultTextCharset() { ! if (defaultEncoding != null) { ! return defaultEncoding; ! } ! return defaultEncoding = Charset.defaultCharset().name(); } /** * Tests only whether the flavor's MIME type supports the charset * parameter. Must only be called for flavors with a primary type of --- 274,284 ---- return null; } String encoding = flavor.getParameter("charset"); ! return (encoding != null) ? encoding : Charset.defaultCharset().name(); } /** * Tests only whether the flavor's MIME type supports the charset * parameter. Must only be called for flavors with a primary type of
*** 483,493 **** String eoln, String terminators) { Long format = getFormatForNativeAsLong(nat); textNatives.add(format); nativeCharsets.put(format, (charset != null && charset.length() != 0) ! ? charset : getDefaultTextCharset()); if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) { nativeEOLNs.put(format, eoln); } if (terminators != null && terminators.length() != 0) { Integer iTerminators = Integer.valueOf(terminators); --- 468,478 ---- String eoln, String terminators) { Long format = getFormatForNativeAsLong(nat); textNatives.add(format); nativeCharsets.put(format, (charset != null && charset.length() != 0) ! ? charset : Charset.defaultCharset().name()); if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) { nativeEOLNs.put(format, eoln); } if (terminators != null && terminators.length() != 0) { Integer iTerminators = Integer.valueOf(terminators);
*** 784,814 **** /* Contains common code for finding the best charset for * clipboard string encoding/decoding, basing on clipboard * format and localeTransferable(on decoding, if available) */ ! private String getBestCharsetForTextFormat(Long lFormat, Transferable localeTransferable) throws IOException { String charset = null; if (localeTransferable != null && isLocaleDependentTextFormat(lFormat) && ! localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) ! { try { ! charset = new String( ! (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor), ! "UTF-8" ! ); } catch (UnsupportedFlavorException cannotHappen) { } } else { charset = getCharsetForTextFormat(lFormat); } if (charset == null) { // Only happens when we have a custom text type. ! charset = getDefaultTextCharset(); } return charset; } /** --- 769,797 ---- /* Contains common code for finding the best charset for * clipboard string encoding/decoding, basing on clipboard * format and localeTransferable(on decoding, if available) */ ! protected String getBestCharsetForTextFormat(Long lFormat, Transferable localeTransferable) throws IOException { String charset = null; if (localeTransferable != null && isLocaleDependentTextFormat(lFormat) && ! localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { ! byte[] charsetNameBytes = (byte[])localeTransferable ! .getTransferData(javaTextEncodingFlavor); ! charset = new String(charsetNameBytes, StandardCharsets.UTF_8); } catch (UnsupportedFlavorException cannotHappen) { } } else { charset = getCharsetForTextFormat(lFormat); } if (charset == null) { // Only happens when we have a custom text type. ! charset = Charset.defaultCharset().name(); } return charset; } /**
*** 1729,1760 **** Transferable localeTransferable) throws IOException { Long lFormat = format; ! String sourceEncoding = null; ! if (isLocaleDependentTextFormat(format) && ! localeTransferable != null && ! localeTransferable. ! isDataFlavorSupported(javaTextEncodingFlavor)) ! { ! try { ! sourceEncoding = new String((byte[])localeTransferable. ! getTransferData(javaTextEncodingFlavor), ! "UTF-8"); ! } catch (UnsupportedFlavorException cannotHappen) { ! } ! } else { ! sourceEncoding = getCharsetForTextFormat(lFormat); ! } ! ! if (sourceEncoding == null) { ! // Only happens when we have a custom text type. ! sourceEncoding = getDefaultTextCharset(); ! } ! wrapped = new BufferedReader ! (new InputStreamReader(bytestream, sourceEncoding)); if (targetEncoding == null) { // Throw NullPointerException for compatibility with the former // call to sun.io.CharToByteConverter.getConverter(null) // (Charset.forName(null) throws unspecified IllegalArgumentException --- 1712,1723 ---- Transferable localeTransferable) throws IOException { Long lFormat = format; ! String sourceEncoding = getBestCharsetForTextFormat(format, localeTransferable); ! wrapped = new BufferedReader(new InputStreamReader(bytestream, sourceEncoding)); if (targetEncoding == null) { // Throw NullPointerException for compatibility with the former // call to sun.io.CharToByteConverter.getConverter(null) // (Charset.forName(null) throws unspecified IllegalArgumentException
*** 2331,2341 **** * in alphabetical order, charsets are not automatically converted to their * canonical forms. */ public static class CharsetComparator extends IndexedComparator<String> { private static final Map<String, Integer> charsets; - private static final String defaultEncoding; private static final Integer DEFAULT_CHARSET_INDEX = 2; private static final Integer OTHER_CHARSET_INDEX = 1; private static final Integer WORST_CHARSET_INDEX = 0; private static final Integer UNSUPPORTED_CHARSET_INDEX = Integer.MIN_VALUE; --- 2294,2303 ----
*** 2352,2363 **** charsetsMap.put(canonicalName("UTF-16"), 7); // US-ASCII is the worst charset supported charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX); ! defaultEncoding = DataTransferer.canonicalName(DataTransferer.getDefaultTextCharset()); ! charsetsMap.putIfAbsent(defaultEncoding, DEFAULT_CHARSET_INDEX); charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); charsets = Collections.unmodifiableMap(charsetsMap); } --- 2314,2324 ---- charsetsMap.put(canonicalName("UTF-16"), 7); // US-ASCII is the worst charset supported charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX); ! charsetsMap.putIfAbsent(Charset.defaultCharset().name(), DEFAULT_CHARSET_INDEX); charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); charsets = Collections.unmodifiableMap(charsetsMap); }