8039567: Duplicated code in DataTransferer
authorpchelko
Wed, 23 Apr 2014 18:07:12 +0400
changeset 24530 479661529a54
parent 24529 c580bcb3aabc
child 24531 1a1a02d4a1fb
8039567: Duplicated code in DataTransferer Reviewed-by: serb, azvegint
jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java
jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java
jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java
jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java
--- a/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java	Wed Apr 23 17:56:05 2014 +0400
+++ b/jdk/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java	Wed Apr 23 18:07:12 2014 +0400
@@ -31,6 +31,7 @@
 
 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 @@
 
             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");
--- a/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java	Wed Apr 23 17:56:05 2014 +0400
+++ b/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java	Wed Apr 23 18:07:12 2014 +0400
@@ -57,6 +57,7 @@
 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 @@
             tempSet.add("UTF-16BE");
             tempSet.add("UTF-16LE");
             tempSet.add("UTF-16");
-            tempSet.add(getDefaultTextCharset());
+            tempSet.add(Charset.defaultCharset().name());
             return Collections.unmodifiableSortedSet(tempSet);
         }
     }
@@ -163,12 +164,6 @@
     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".
      */
@@ -266,17 +261,7 @@
 
         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 @@
 
         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 @@
      * 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 @@
         }
         if (charset == null) {
             // Only happens when we have a custom text type.
-            charset = getDefaultTextCharset();
+            charset = Charset.defaultCharset().name();
         }
         return charset;
     }
@@ -1716,28 +1699,8 @@
         {
             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 @@
      */
     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;
@@ -2339,8 +2301,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.putIfAbsent(Charset.defaultCharset().name(), DEFAULT_CHARSET_INDEX);
 
             charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX);
 
--- a/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java	Wed Apr 23 17:56:05 2014 +0400
+++ b/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java	Wed Apr 23 18:07:12 2014 +0400
@@ -259,28 +259,9 @@
                                   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<URI> uriList = new ArrayList<>();
             URI uri;
@@ -293,9 +274,6 @@
                 uriList.add(uri);
             }
             return uriList.toArray(new URI[uriList.size()]);
-        } finally {
-            if (reader != null)
-                reader.close();
         }
     }
 
--- a/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java	Wed Apr 23 17:56:05 2014 +0400
+++ b/jdk/src/windows/classes/sun/awt/windows/WDataTransferer.java	Wed Apr 23 18:07:12 2014 +0400
@@ -59,6 +59,7 @@
 
 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 @@
         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.