diff --git a/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java b/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java index 26d2347ff15..c07356057ee 100644 --- a/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java +++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java @@ -31,6 +31,7 @@ import sun.awt.image.ImageRepresentation; import java.io.*; import java.net.URL; +import java.nio.charset.Charset; import java.text.Normalizer; import java.text.Normalizer.Form; import java.util.*; @@ -126,7 +127,7 @@ public class CDataTransferer extends DataTransferer { if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) { - String charset = getDefaultTextCharset(); + String charset = Charset.defaultCharset().name(); if (transferable != null && transferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { charset = new String((byte[])transferable.getTransferData(javaTextEncodingFlavor), "UTF-8"); diff --git a/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java b/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java index 3f22bd86fe9..d9c8334df8a 100644 --- a/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java +++ b/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java @@ -57,6 +57,7 @@ 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; @@ -149,7 +150,7 @@ public abstract class DataTransferer { tempSet.add("UTF-16BE"); tempSet.add("UTF-16LE"); tempSet.add("UTF-16"); - tempSet.add(getDefaultTextCharset()); + tempSet.add(Charset.defaultCharset().name()); return Collections.unmodifiableSortedSet(tempSet); } } @@ -162,12 +163,6 @@ public abstract class DataTransferer { */ private static final Map 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". @@ -266,17 +261,7 @@ public abstract class DataTransferer { 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(); + return (encoding != null) ? encoding : Charset.defaultCharset().name(); } /** @@ -470,7 +455,7 @@ public abstract class DataTransferer { textNatives.add(format); nativeCharsets.put(format, (charset != null && charset.length() != 0) - ? charset : getDefaultTextCharset()); + ? charset : Charset.defaultCharset().name()); if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) { nativeEOLNs.put(format, eoln); } @@ -771,19 +756,17 @@ public abstract class DataTransferer { * clipboard string encoding/decoding, basing on clipboard * format and localeTransferable(on decoding, if available) */ - private String getBestCharsetForTextFormat(Long lFormat, + protected String getBestCharsetForTextFormat(Long lFormat, Transferable localeTransferable) throws IOException { String charset = null; if (localeTransferable != null && isLocaleDependentTextFormat(lFormat) && - localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) - { + localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { - charset = new String( - (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor), - "UTF-8" - ); + byte[] charsetNameBytes = (byte[])localeTransferable + .getTransferData(javaTextEncodingFlavor); + charset = new String(charsetNameBytes, StandardCharsets.UTF_8); } catch (UnsupportedFlavorException cannotHappen) { } } else { @@ -791,7 +774,7 @@ public abstract class DataTransferer { } if (charset == null) { // Only happens when we have a custom text type. - charset = getDefaultTextCharset(); + charset = Charset.defaultCharset().name(); } return charset; } @@ -1716,28 +1699,8 @@ search: { 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)); + String sourceEncoding = getBestCharsetForTextFormat(format, localeTransferable); + wrapped = new BufferedReader(new InputStreamReader(bytestream, sourceEncoding)); if (targetEncoding == null) { // Throw NullPointerException for compatibility with the former @@ -2318,7 +2281,6 @@ search: */ public static class CharsetComparator extends IndexedComparator { private static final Map charsets; - private static final String defaultEncoding; private static final Integer DEFAULT_CHARSET_INDEX = 2; private static final Integer OTHER_CHARSET_INDEX = 1; @@ -2339,8 +2301,7 @@ search: // 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.putIfAbsent(Charset.defaultCharset().name(), DEFAULT_CHARSET_INDEX); charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); diff --git a/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java b/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java index 554e19507dc..fd9cbb8f13c 100644 --- a/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java +++ b/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java @@ -259,28 +259,9 @@ public class XDataTransferer extends DataTransferer { Transferable localeTransferable) throws IOException { - String charset = null; - if (localeTransferable != null && - isLocaleDependentTextFormat(format) && - localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { - try { - charset = new String( - (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor), - "UTF-8" - ); - } catch (UnsupportedFlavorException cannotHappen) { - } - } else { - charset = getCharsetForTextFormat(format); - } - if (charset == null) { - // Only happens when we have a custom text type. - charset = getDefaultTextCharset(); - } - - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(stream, charset)); + String charset = getBestCharsetForTextFormat(format, localeTransferable); + try (InputStreamReader isr = new InputStreamReader(stream, charset); + BufferedReader reader = new BufferedReader(isr)) { String line; ArrayList uriList = new ArrayList<>(); URI uri; @@ -293,9 +274,6 @@ public class XDataTransferer extends DataTransferer { uriList.add(uri); } return uriList.toArray(new URI[uriList.size()]); - } finally { - if (reader != null) - reader.close(); } } diff --git a/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java b/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java index 4b16e214052..2ccb76462aa 100644 --- a/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java +++ b/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java @@ -59,6 +59,7 @@ import java.io.File; import java.net.URL; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -268,9 +269,9 @@ final class WDataTransferer extends DataTransferer { if (format == CFSTR_INETURL && URL.class.equals(flavor.getRepresentationClass())) { - String charset = getDefaultTextCharset(); - if (localeTransferable != null && localeTransferable. - isDataFlavorSupported(javaTextEncodingFlavor)) + String charset = Charset.defaultCharset().name(); + if (localeTransferable != null + && localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { charset = new String((byte[])localeTransferable.