jdk/src/java.desktop/share/classes/java/awt/datatransfer/DataFlavor.java
changeset 26037 508779ce6619
parent 26010 9a3cf8ee0776
parent 25859 3317bb8137f4
child 28059 e576535359cc
equal deleted inserted replaced
25992:e9b05e933ddd 26037:508779ce6619
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.awt.datatransfer;
    26 package java.awt.datatransfer;
    27 
    27 
    28 import sun.awt.datatransfer.DataTransferer;
    28 import sun.datatransfer.DataFlavorUtil;
    29 import sun.reflect.misc.ReflectUtil;
    29 import sun.reflect.misc.ReflectUtil;
    30 
    30 
    31 import java.io.ByteArrayInputStream;
    31 import java.io.ByteArrayInputStream;
    32 import java.io.CharArrayReader;
    32 import java.io.CharArrayReader;
    33 import java.io.Externalizable;
    33 import java.io.Externalizable;
    42 import java.io.UnsupportedEncodingException;
    42 import java.io.UnsupportedEncodingException;
    43 import java.nio.ByteBuffer;
    43 import java.nio.ByteBuffer;
    44 import java.nio.CharBuffer;
    44 import java.nio.CharBuffer;
    45 import java.util.Arrays;
    45 import java.util.Arrays;
    46 import java.util.Collections;
    46 import java.util.Collections;
    47 import java.util.Comparator;
       
    48 import java.util.Objects;
    47 import java.util.Objects;
    49 
    48 
    50 import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
    49 import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
    51 
    50 
    52 /**
    51 /**
   580         if (representationClass == null) {
   579         if (representationClass == null) {
   581            params += "null";
   580            params += "null";
   582         } else {
   581         } else {
   583            params += representationClass.getName();
   582            params += representationClass.getName();
   584         }
   583         }
   585         if (DataTransferer.isFlavorCharsetTextType(this) &&
   584         if (DataFlavorUtil.isFlavorCharsetTextType(this) &&
   586             (isRepresentationClassInputStream() ||
   585             (isRepresentationClassInputStream() ||
   587              isRepresentationClassByteBuffer() ||
   586              isRepresentationClassByteBuffer() ||
   588              byte[].class.equals(representationClass)))
   587              byte[].class.equals(representationClass)))
   589         {
   588         {
   590             params += ";charset=" + DataTransferer.getTextCharset(this);
   589             params += ";charset=" + DataFlavorUtil.getTextCharset(this);
   591         }
   590         }
   592         return params;
   591         return params;
   593     }
   592     }
   594 
   593 
   595     /**
   594     /**
   607      * @return a <code>DataFlavor</code> representing plain text
   606      * @return a <code>DataFlavor</code> representing plain text
   608      *    with Unicode encoding
   607      *    with Unicode encoding
   609      * @since 1.3
   608      * @since 1.3
   610      */
   609      */
   611     public static final DataFlavor getTextPlainUnicodeFlavor() {
   610     public static final DataFlavor getTextPlainUnicodeFlavor() {
   612         String encoding = null;
       
   613         DataTransferer transferer = DataTransferer.getInstance();
       
   614         if (transferer != null) {
       
   615             encoding = transferer.getDefaultUnicodeEncoding();
       
   616         }
       
   617         return new DataFlavor(
   611         return new DataFlavor(
   618             "text/plain;charset="+encoding
   612             "text/plain;charset=" + DataFlavorUtil.getDesktopService().getDefaultUnicodeEncoding()
   619             +";class=java.io.InputStream", "Plain Text");
   613             +";class=java.io.InputStream", "Plain Text");
   620     }
   614     }
   621 
   615 
   622     /**
   616     /**
   623      * Selects the best text <code>DataFlavor</code> from an array of <code>
   617      * Selects the best text <code>DataFlavor</code> from an array of <code>
   739                                        DataFlavor[] availableFlavors) {
   733                                        DataFlavor[] availableFlavors) {
   740         if (availableFlavors == null || availableFlavors.length == 0) {
   734         if (availableFlavors == null || availableFlavors.length == 0) {
   741             return null;
   735             return null;
   742         }
   736         }
   743 
   737 
   744         if (textFlavorComparator == null) {
       
   745             textFlavorComparator = new TextFlavorComparator();
       
   746         }
       
   747 
       
   748         DataFlavor bestFlavor = Collections.max(Arrays.asList(availableFlavors),
   738         DataFlavor bestFlavor = Collections.max(Arrays.asList(availableFlavors),
   749                                                 textFlavorComparator);
   739                                                 DataFlavorUtil.getTextFlavorComparator());
   750 
   740 
   751         if (!bestFlavor.isFlavorTextType()) {
   741         if (!bestFlavor.isFlavorTextType()) {
   752             return null;
   742             return null;
   753         }
   743         }
   754 
   744 
   755         return bestFlavor;
   745         return bestFlavor;
   756     }
       
   757 
       
   758     private static Comparator<DataFlavor> textFlavorComparator;
       
   759 
       
   760     static class TextFlavorComparator
       
   761             extends DataTransferer.DataFlavorComparator {
       
   762 
       
   763         /**
       
   764          * Compares two <code>DataFlavor</code> objects. Returns a negative
       
   765          * integer, zero, or a positive integer as the first
       
   766          * <code>DataFlavor</code> is worse than, equal to, or better than the
       
   767          * second.
       
   768          * <p>
       
   769          * <code>DataFlavor</code>s are ordered according to the rules outlined
       
   770          * for <code>selectBestTextFlavor</code>.
       
   771          *
       
   772          * @param flavor1 the first <code>DataFlavor</code> to be compared
       
   773          * @param flavor2 the second <code>DataFlavor</code> to be compared
       
   774          * @return a negative integer, zero, or a positive integer as the first
       
   775          *         argument is worse, equal to, or better than the second
       
   776          * @throws ClassCastException if either of the arguments is not an
       
   777          *         instance of <code>DataFlavor</code>
       
   778          * @throws NullPointerException if either of the arguments is
       
   779          *         <code>null</code>
       
   780          *
       
   781          * @see #selectBestTextFlavor
       
   782          */
       
   783         public int compare(DataFlavor flavor1, DataFlavor flavor2) {
       
   784             if (flavor1.isFlavorTextType()) {
       
   785                 if (flavor2.isFlavorTextType()) {
       
   786                     return super.compare(flavor1, flavor2);
       
   787                 } else {
       
   788                     return 1;
       
   789                 }
       
   790             } else if (flavor2.isFlavorTextType()) {
       
   791                 return -1;
       
   792             } else {
       
   793                 return 0;
       
   794             }
       
   795         }
       
   796     }
   746     }
   797 
   747 
   798     /**
   748     /**
   799      * Gets a Reader for a text flavor, decoded, if necessary, for the expected
   749      * Gets a Reader for a text flavor, decoded, if necessary, for the expected
   800      * charset (encoding). The supported representation classes are
   750      * charset (encoding). The supported representation classes are
  1013             if (!mimeType.match(that.mimeType)) {
   963             if (!mimeType.match(that.mimeType)) {
  1014                 return false;
   964                 return false;
  1015             }
   965             }
  1016 
   966 
  1017             if ("text".equals(getPrimaryType())) {
   967             if ("text".equals(getPrimaryType())) {
  1018                 if (DataTransferer.doesSubtypeSupportCharset(this)
   968                 if (DataFlavorUtil.doesSubtypeSupportCharset(this)
  1019                         && representationClass != null
   969                         && representationClass != null
  1020                         && !isStandardTextRepresentationClass()) {
   970                         && !isStandardTextRepresentationClass()) {
  1021                     String thisCharset =
   971                     String thisCharset =
  1022                             DataTransferer.canonicalName(this.getParameter("charset"));
   972                             DataFlavorUtil.canonicalName(this.getParameter("charset"));
  1023                     String thatCharset =
   973                     String thatCharset =
  1024                             DataTransferer.canonicalName(that.getParameter("charset"));
   974                             DataFlavorUtil.canonicalName(that.getParameter("charset"));
  1025                     if (!Objects.equals(thisCharset, thatCharset)) {
   975                     if (!Objects.equals(thisCharset, thatCharset)) {
  1026                         return false;
   976                         return false;
  1027                     }
   977                     }
  1028                 }
   978                 }
  1029 
   979 
  1086             // Do not add subType.hashCode() to the total. equals uses
  1036             // Do not add subType.hashCode() to the total. equals uses
  1087             // MimeType.match which reports a match if one or both of the
  1037             // MimeType.match which reports a match if one or both of the
  1088             // subTypes is '*', regardless of the other subType.
  1038             // subTypes is '*', regardless of the other subType.
  1089 
  1039 
  1090             if ("text".equals(primaryType)) {
  1040             if ("text".equals(primaryType)) {
  1091                 if (DataTransferer.doesSubtypeSupportCharset(this)
  1041                 if (DataFlavorUtil.doesSubtypeSupportCharset(this)
  1092                         && representationClass != null
  1042                         && representationClass != null
  1093                         && !isStandardTextRepresentationClass()) {
  1043                         && !isStandardTextRepresentationClass()) {
  1094                     String charset = DataTransferer.canonicalName(getParameter("charset"));
  1044                     String charset = DataFlavorUtil.canonicalName(getParameter("charset"));
  1095                     if (charset != null) {
  1045                     if (charset != null) {
  1096                         total += charset.hashCode();
  1046                         total += charset.hashCode();
  1097                     }
  1047                     }
  1098                 }
  1048                 }
  1099 
  1049 
  1278 
  1228 
  1279    /**
  1229    /**
  1280     * Returns true if the representation class is <code>Remote</code>.
  1230     * Returns true if the representation class is <code>Remote</code>.
  1281     * @return true if the representation class is <code>Remote</code>
  1231     * @return true if the representation class is <code>Remote</code>
  1282     */
  1232     */
  1283 
       
  1284     public boolean isRepresentationClassRemote() {
  1233     public boolean isRepresentationClassRemote() {
  1285         return DataTransferer.isRemote(representationClass);
  1234         return DataFlavorUtil.RMI.isRemote(representationClass);
  1286     }
  1235     }
  1287 
  1236 
  1288    /**
  1237    /**
  1289     * Returns true if the <code>DataFlavor</code> specified represents
  1238     * Returns true if the <code>DataFlavor</code> specified represents
  1290     * a serialized object.
  1239     * a serialized object.
  1354      *         text flavor as described above; <code>false</code> otherwise
  1303      *         text flavor as described above; <code>false</code> otherwise
  1355      * @see #selectBestTextFlavor
  1304      * @see #selectBestTextFlavor
  1356      * @since 1.4
  1305      * @since 1.4
  1357      */
  1306      */
  1358     public boolean isFlavorTextType() {
  1307     public boolean isFlavorTextType() {
  1359         return (DataTransferer.isFlavorCharsetTextType(this) ||
  1308         return (DataFlavorUtil.isFlavorCharsetTextType(this) ||
  1360                 DataTransferer.isFlavorNoncharsetTextType(this));
  1309                 DataFlavorUtil.isFlavorNoncharsetTextType(this));
  1361     }
  1310     }
  1362 
  1311 
  1363    /**
  1312    /**
  1364     * Serializes this <code>DataFlavor</code>.
  1313     * Serializes this <code>DataFlavor</code>.
  1365     */
  1314     */