1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.datatransfer;
  27 
  28 import java.io.*;
  29 import java.nio.*;
  30 import java.util.*;
  31 
  32 import sun.awt.datatransfer.DataTransferer;
  33 import sun.reflect.misc.ReflectUtil;
  34 
  35 import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
  36 
  37 /**
  38  * A {@code DataFlavor} provides meta information about data. {@code DataFlavor}
  39  * is typically used to access data on the clipboard, or during
  40  * a drag and drop operation.
  41  * <p>
  42  * An instance of {@code DataFlavor} encapsulates a content type as
  43  * defined in <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
  44  * and <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>.
  45  * A content type is typically referred to as a MIME type.
  46  * <p>
  47  * A content type consists of a media type (referred
  48  * to as the primary type), a subtype, and optional parameters. See
  49  * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
  50  * for details on the syntax of a MIME type.
  51  * <p>
  52  * The JRE data transfer implementation interprets the parameter &quot;class&quot;
  53  * of a MIME type as <B>a representation class</b>.
  54  * The representation class reflects the class of the object being
  55  * transferred. In other words, the representation class is the type of
  56  * object returned by {@link Transferable#getTransferData}.
  57  * For example, the MIME type of {@link #imageFlavor} is
  58  * {@code "image/x-java-image;class=java.awt.Image"},
  59  * the primary type is {@code image}, the subtype is
  60  * {@code x-java-image}, and the representation class is
  61  * {@code java.awt.Image}. When {@code getTransferData} is invoked
  62  * with a {@code DataFlavor} of {@code imageFlavor}, an instance of
  63  * {@code java.awt.Image} is returned.
  64  * It's important to note that {@code DataFlavor} does no error checking
  65  * against the representation class. It is up to consumers of
  66  * {@code DataFlavor}, such as {@code Transferable}, to honor the representation
  67  * class.
  68  * <br>
  69  * Note, if you do not specify a representation class when
  70  * creating a {@code DataFlavor}, the default
  71  * representation class is used. See appropriate documentation for
  72  * {@code DataFlavor}'s constructors.
  73  * <p>
  74  * Also, {@code DataFlavor} instances with the &quot;text&quot; primary
  75  * MIME type may have a &quot;charset&quot; parameter. Refer to
  76  * <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a> and
  77  * {@link #selectBestTextFlavor} for details on &quot;text&quot; MIME types
  78  * and the &quot;charset&quot; parameter.
  79  * <p>
  80  * Equality of {@code DataFlavors} is determined by the primary type,
  81  * subtype, and representation class. Refer to {@link #equals(DataFlavor)} for
  82  * details. When determining equality, any optional parameters are ignored.
  83  * For example, the following produces two {@code DataFlavors} that
  84  * are considered identical:
  85  * <pre>
  86  *   DataFlavor flavor1 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; foo=bar&quot;);
  87  *   DataFlavor flavor2 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; x=y&quot;);
  88  *   // The following returns true.
  89  *   flavor1.equals(flavor2);
  90  * </pre>
  91  * As mentioned, {@code flavor1} and {@code flavor2} are considered identical.
  92  * As such, asking a {@code Transferable} for either {@code DataFlavor} returns
  93  * the same results.
  94  * <p>
  95  * For more information on the using data transfer with Swing see
  96  * the <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">
  97  * How to Use Drag and Drop and Data Transfer</a>,
  98  * section in <em>Java Tutorial</em>.
  99  *
 100  * @author      Blake Sullivan
 101  * @author      Laurence P. G. Cable
 102  * @author      Jeff Dunn
 103  */
 104 public class DataFlavor implements Externalizable, Cloneable {
 105 
 106     private static final long serialVersionUID = 8367026044764648243L;
 107     private static final Class<InputStream> ioInputStreamClass = InputStream.class;
 108 
 109     /**
 110      * Tries to load a class from: the bootstrap loader, the system loader,
 111      * the context loader (if one is present) and finally the loader specified.
 112      *
 113      * @param className the name of the class to be loaded
 114      * @param fallback the fallback loader
 115      * @return the class loaded
 116      * @exception ClassNotFoundException if class is not found
 117      */
 118     protected final static Class<?> tryToLoadClass(String className,
 119                                                    ClassLoader fallback)
 120         throws ClassNotFoundException
 121     {
 122         ReflectUtil.checkPackageAccess(className);
 123         try {
 124             SecurityManager sm = System.getSecurityManager();
 125             if (sm != null) {
 126                 sm.checkPermission(GET_CLASSLOADER_PERMISSION);
 127             }
 128             ClassLoader loader = ClassLoader.getSystemClassLoader();
 129             try {
 130                 // bootstrap class loader and system class loader if present
 131                 return Class.forName(className, true, loader);
 132             }
 133             catch (ClassNotFoundException exception) {
 134                 // thread context class loader if and only if present
 135                 loader = Thread.currentThread().getContextClassLoader();
 136                 if (loader != null) {
 137                     try {
 138                         return Class.forName(className, true, loader);
 139                     }
 140                     catch (ClassNotFoundException e) {
 141                         // fallback to user's class loader
 142                     }
 143                 }
 144             }
 145         } catch (SecurityException exception) {
 146             // ignore secured class loaders
 147         }
 148         return Class.forName(className, true, fallback);
 149     }
 150 
 151     /*
 152      * private initializer
 153      */
 154     static private DataFlavor createConstant(Class<?> rc, String prn) {
 155         try {
 156             return new DataFlavor(rc, prn);
 157         } catch (Exception e) {
 158             return null;
 159         }
 160     }
 161 
 162     /*
 163      * private initializer
 164      */
 165     static private DataFlavor createConstant(String mt, String prn) {
 166         try {
 167             return new DataFlavor(mt, prn);
 168         } catch (Exception e) {
 169             return null;
 170         }
 171     }
 172 
 173     /*
 174      * private initializer
 175      */
 176     static private DataFlavor initHtmlDataFlavor(String htmlFlavorType) {
 177         try {
 178             return new DataFlavor ("text/html; class=java.lang.String;document=" +
 179                                        htmlFlavorType + ";charset=Unicode");
 180         } catch (Exception e) {
 181             return null;
 182         }
 183     }
 184 
 185     /**
 186      * The <code>DataFlavor</code> representing a Java Unicode String class,
 187      * where:
 188      * <pre>
 189      *     representationClass = java.lang.String
 190      *     mimeType           = "application/x-java-serialized-object"
 191      * </pre>
 192      */
 193     public static final DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
 194 
 195     /**
 196      * The <code>DataFlavor</code> representing a Java Image class,
 197      * where:
 198      * <pre>
 199      *     representationClass = java.awt.Image
 200      *     mimeType            = "image/x-java-image"
 201      * </pre>
 202      */
 203     public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
 204 
 205     /**
 206      * The <code>DataFlavor</code> representing plain text with Unicode
 207      * encoding, where:
 208      * <pre>
 209      *     representationClass = InputStream
 210      *     mimeType            = "text/plain; charset=unicode"
 211      * </pre>
 212      * This <code>DataFlavor</code> has been <b>deprecated</b> because
 213      * (1) Its representation is an InputStream, an 8-bit based representation,
 214      * while Unicode is a 16-bit character set; and (2) The charset "unicode"
 215      * is not well-defined. "unicode" implies a particular platform's
 216      * implementation of Unicode, not a cross-platform implementation.
 217      *
 218      * @deprecated as of 1.3. Use <code>DataFlavor.getReaderForText(Transferable)</code>
 219      *             instead of <code>Transferable.getTransferData(DataFlavor.plainTextFlavor)</code>.
 220      */
 221     @Deprecated
 222     public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
 223 
 224     /**
 225      * A MIME Content-Type of application/x-java-serialized-object represents
 226      * a graph of Java object(s) that have been made persistent.
 227      *
 228      * The representation class associated with this <code>DataFlavor</code>
 229      * identifies the Java type of an object returned as a reference
 230      * from an invocation <code>java.awt.datatransfer.getTransferData</code>.
 231      */
 232     public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";
 233 
 234     /**
 235      * To transfer a list of files to/from Java (and the underlying
 236      * platform) a <code>DataFlavor</code> of this type/subtype and
 237      * representation class of <code>java.util.List</code> is used.
 238      * Each element of the list is required/guaranteed to be of type
 239      * <code>java.io.File</code>.
 240      */
 241     public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
 242 
 243     /**
 244      * To transfer a reference to an arbitrary Java object reference that
 245      * has no associated MIME Content-type, across a <code>Transferable</code>
 246      * interface WITHIN THE SAME JVM, a <code>DataFlavor</code>
 247      * with this type/subtype is used, with a <code>representationClass</code>
 248      * equal to the type of the class/interface being passed across the
 249      * <code>Transferable</code>.
 250      * <p>
 251      * The object reference returned from
 252      * <code>Transferable.getTransferData</code> for a <code>DataFlavor</code>
 253      * with this MIME Content-Type is required to be
 254      * an instance of the representation Class of the <code>DataFlavor</code>.
 255      */
 256     public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
 257 
 258     /**
 259      * In order to pass a live link to a Remote object via a Drag and Drop
 260      * <code>ACTION_LINK</code> operation a Mime Content Type of
 261      * application/x-java-remote-object should be used,
 262      * where the representation class of the <code>DataFlavor</code>
 263      * represents the type of the <code>Remote</code> interface to be
 264      * transferred.
 265      */
 266     public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";
 267 
 268     /**
 269      * Represents a piece of an HTML markup. The markup consists of the part
 270      * selected on the source side. Therefore some tags in the markup may be
 271      * unpaired. If the flavor is used to represent the data in
 272      * a {@link Transferable} instance, no additional changes will be made.
 273      * This DataFlavor instance represents the same HTML markup as DataFlavor
 274      * instances which content MIME type does not contain document parameter
 275      * and representation class is the String class.
 276      * <pre>
 277      *     representationClass = String
 278      *     mimeType           = "text/html"
 279      * </pre>
 280      */
 281     public static DataFlavor selectionHtmlFlavor = initHtmlDataFlavor("selection");
 282 
 283     /**
 284      * Represents a piece of an HTML markup. If possible, the markup received
 285      * from a native system is supplemented with pair tags to be
 286      * a well-formed HTML markup. If the flavor is used to represent the data in
 287      * a {@link Transferable} instance, no additional changes will be made.
 288      * <pre>
 289      *     representationClass = String
 290      *     mimeType           = "text/html"
 291      * </pre>
 292      */
 293     public static DataFlavor fragmentHtmlFlavor = initHtmlDataFlavor("fragment");
 294 
 295     /**
 296      * Represents a piece of an HTML markup. If possible, the markup
 297      * received from a native system is supplemented with additional
 298      * tags to make up a well-formed HTML document. If the flavor is used to
 299      * represent the data in a {@link Transferable} instance,
 300      * no additional changes will be made.
 301      * <pre>
 302      *     representationClass = String
 303      *     mimeType           = "text/html"
 304      * </pre>
 305      */
 306     public static  DataFlavor allHtmlFlavor = initHtmlDataFlavor("all");
 307 
 308     /**
 309      * Constructs a new <code>DataFlavor</code>.  This constructor is
 310      * provided only for the purpose of supporting the
 311      * <code>Externalizable</code> interface.  It is not
 312      * intended for public (client) use.
 313      *
 314      * @since 1.2
 315      */
 316     public DataFlavor() {
 317         super();
 318     }
 319 
 320     /**
 321      * Constructs a fully specified <code>DataFlavor</code>.
 322      *
 323      * @exception NullPointerException if either <code>primaryType</code>,
 324      *            <code>subType</code> or <code>representationClass</code> is null
 325      */
 326     private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class<?> representationClass, String humanPresentableName) {
 327         super();
 328         if (primaryType == null) {
 329             throw new NullPointerException("primaryType");
 330         }
 331         if (subType == null) {
 332             throw new NullPointerException("subType");
 333         }
 334         if (representationClass == null) {
 335             throw new NullPointerException("representationClass");
 336         }
 337 
 338         if (params == null) params = new MimeTypeParameterList();
 339 
 340         params.set("class", representationClass.getName());
 341 
 342         if (humanPresentableName == null) {
 343             humanPresentableName = params.get("humanPresentableName");
 344 
 345             if (humanPresentableName == null)
 346                 humanPresentableName = primaryType + "/" + subType;
 347         }
 348 
 349         try {
 350             mimeType = new MimeType(primaryType, subType, params);
 351         } catch (MimeTypeParseException mtpe) {
 352             throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
 353         }
 354 
 355         this.representationClass  = representationClass;
 356         this.humanPresentableName = humanPresentableName;
 357 
 358         mimeType.removeParameter("humanPresentableName");
 359     }
 360 
 361     /**
 362      * Constructs a <code>DataFlavor</code> that represents a Java class.
 363      * <p>
 364      * The returned <code>DataFlavor</code> will have the following
 365      * characteristics:
 366      * <pre>
 367      *    representationClass = representationClass
 368      *    mimeType            = application/x-java-serialized-object
 369      * </pre>
 370      * @param representationClass the class used to transfer data in this flavor
 371      * @param humanPresentableName the human-readable string used to identify
 372      *                 this flavor; if this parameter is <code>null</code>
 373      *                 then the value of the the MIME Content Type is used
 374      * @exception NullPointerException if <code>representationClass</code> is null
 375      */
 376     public DataFlavor(Class<?> representationClass, String humanPresentableName) {
 377         this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
 378         if (representationClass == null) {
 379             throw new NullPointerException("representationClass");
 380         }
 381     }
 382 
 383     /**
 384      * Constructs a <code>DataFlavor</code> that represents a
 385      * <code>MimeType</code>.
 386      * <p>
 387      * The returned <code>DataFlavor</code> will have the following
 388      * characteristics:
 389      * <p>
 390      * If the <code>mimeType</code> is
 391      * "application/x-java-serialized-object; class=&lt;representation class&gt;",
 392      * the result is the same as calling
 393      * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
 394      * <p>
 395      * Otherwise:
 396      * <pre>
 397      *     representationClass = InputStream
 398      *     mimeType            = mimeType
 399      * </pre>
 400      * @param mimeType the string used to identify the MIME type for this flavor;
 401      *                 if the the <code>mimeType</code> does not specify a
 402      *                 "class=" parameter, or if the class is not successfully
 403      *                 loaded, then an <code>IllegalArgumentException</code>
 404      *                 is thrown
 405      * @param humanPresentableName the human-readable string used to identify
 406      *                 this flavor; if this parameter is <code>null</code>
 407      *                 then the value of the the MIME Content Type is used
 408      * @exception IllegalArgumentException if <code>mimeType</code> is
 409      *                 invalid or if the class is not successfully loaded
 410      * @exception NullPointerException if <code>mimeType</code> is null
 411      */
 412     public DataFlavor(String mimeType, String humanPresentableName) {
 413         super();
 414         if (mimeType == null) {
 415             throw new NullPointerException("mimeType");
 416         }
 417         try {
 418             initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
 419         } catch (MimeTypeParseException mtpe) {
 420             throw new IllegalArgumentException("failed to parse:" + mimeType);
 421         } catch (ClassNotFoundException cnfe) {
 422             throw new IllegalArgumentException("can't find specified class: " + cnfe.getMessage());
 423         }
 424     }
 425 
 426     /**
 427      * Constructs a <code>DataFlavor</code> that represents a
 428      * <code>MimeType</code>.
 429      * <p>
 430      * The returned <code>DataFlavor</code> will have the following
 431      * characteristics:
 432      * <p>
 433      * If the mimeType is
 434      * "application/x-java-serialized-object; class=&lt;representation class&gt;",
 435      * the result is the same as calling
 436      * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
 437      * <p>
 438      * Otherwise:
 439      * <pre>
 440      *     representationClass = InputStream
 441      *     mimeType            = mimeType
 442      * </pre>
 443      * @param mimeType the string used to identify the MIME type for this flavor
 444      * @param humanPresentableName the human-readable string used to
 445      *          identify this flavor
 446      * @param classLoader the class loader to use
 447      * @exception ClassNotFoundException if the class is not loaded
 448      * @exception IllegalArgumentException if <code>mimeType</code> is
 449      *                 invalid
 450      * @exception NullPointerException if <code>mimeType</code> is null
 451      */
 452     public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
 453         super();
 454         if (mimeType == null) {
 455             throw new NullPointerException("mimeType");
 456         }
 457         try {
 458             initialize(mimeType, humanPresentableName, classLoader);
 459         } catch (MimeTypeParseException mtpe) {
 460             throw new IllegalArgumentException("failed to parse:" + mimeType);
 461         }
 462     }
 463 
 464     /**
 465      * Constructs a <code>DataFlavor</code> from a <code>mimeType</code> string.
 466      * The string can specify a "class=&lt;fully specified Java class name&gt;"
 467      * parameter to create a <code>DataFlavor</code> with the desired
 468      * representation class. If the string does not contain "class=" parameter,
 469      * <code>java.io.InputStream</code> is used as default.
 470      *
 471      * @param mimeType the string used to identify the MIME type for this flavor;
 472      *                 if the class specified by "class=" parameter is not
 473      *                 successfully loaded, then an
 474      *                 <code>ClassNotFoundException</code> is thrown
 475      * @exception ClassNotFoundException if the class is not loaded
 476      * @exception IllegalArgumentException if <code>mimeType</code> is
 477      *                 invalid
 478      * @exception NullPointerException if <code>mimeType</code> is null
 479      */
 480     public DataFlavor(String mimeType) throws ClassNotFoundException {
 481         super();
 482         if (mimeType == null) {
 483             throw new NullPointerException("mimeType");
 484         }
 485         try {
 486             initialize(mimeType, null, this.getClass().getClassLoader());
 487         } catch (MimeTypeParseException mtpe) {
 488             throw new IllegalArgumentException("failed to parse:" + mimeType);
 489         }
 490     }
 491 
 492    /**
 493     * Common initialization code called from various constructors.
 494     *
 495     * @param mimeType the MIME Content Type (must have a class= param)
 496     * @param humanPresentableName the human Presentable Name or
 497     *                 <code>null</code>
 498     * @param classLoader the fallback class loader to resolve against
 499     *
 500     * @throws MimeTypeParseException
 501     * @throws ClassNotFoundException
 502     * @throws  NullPointerException if <code>mimeType</code> is null
 503     *
 504     * @see tryToLoadClass
 505     */
 506     private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException {
 507         if (mimeType == null) {
 508             throw new NullPointerException("mimeType");
 509         }
 510 
 511         this.mimeType = new MimeType(mimeType); // throws
 512 
 513         String rcn = getParameter("class");
 514 
 515         if (rcn == null) {
 516             if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
 517 
 518                 throw new IllegalArgumentException("no representation class specified for:" + mimeType);
 519             else
 520                 representationClass = java.io.InputStream.class; // default
 521         } else { // got a class name
 522             representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
 523         }
 524 
 525         this.mimeType.setParameter("class", representationClass.getName());
 526 
 527         if (humanPresentableName == null) {
 528             humanPresentableName = this.mimeType.getParameter("humanPresentableName");
 529             if (humanPresentableName == null)
 530                 humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
 531         }
 532 
 533         this.humanPresentableName = humanPresentableName; // set it.
 534 
 535         this.mimeType.removeParameter("humanPresentableName"); // just in case
 536     }
 537 
 538     /**
 539      * String representation of this <code>DataFlavor</code> and its
 540      * parameters. The resulting <code>String</code> contains the name of
 541      * the <code>DataFlavor</code> class, this flavor's MIME type, and its
 542      * representation class. If this flavor has a primary MIME type of "text",
 543      * supports the charset parameter, and has an encoded representation, the
 544      * flavor's charset is also included. See <code>selectBestTextFlavor</code>
 545      * for a list of text flavors which support the charset parameter.
 546      *
 547      * @return  string representation of this <code>DataFlavor</code>
 548      * @see #selectBestTextFlavor
 549      */
 550     public String toString() {
 551         String string = getClass().getName();
 552         string += "["+paramString()+"]";
 553         return string;
 554     }
 555 
 556     private String paramString() {
 557         String params = "";
 558         params += "mimetype=";
 559         if (mimeType == null) {
 560             params += "null";
 561         } else {
 562             params += mimeType.getBaseType();
 563         }
 564         params += ";representationclass=";
 565         if (representationClass == null) {
 566            params += "null";
 567         } else {
 568            params += representationClass.getName();
 569         }
 570         if (DataTransferer.isFlavorCharsetTextType(this) &&
 571             (isRepresentationClassInputStream() ||
 572              isRepresentationClassByteBuffer() ||
 573              DataTransferer.byteArrayClass.equals(representationClass)))
 574         {
 575             params += ";charset=" + DataTransferer.getTextCharset(this);
 576         }
 577         return params;
 578     }
 579 
 580     /**
 581      * Returns a <code>DataFlavor</code> representing plain text with Unicode
 582      * encoding, where:
 583      * <pre>
 584      *     representationClass = java.io.InputStream
 585      *     mimeType            = "text/plain;
 586      *                            charset=&lt;platform default Unicode encoding&gt;"
 587      * </pre>
 588      * Sun's implementation for Microsoft Windows uses the encoding <code>utf-16le</code>.
 589      * Sun's implementation for Solaris and Linux uses the encoding
 590      * <code>iso-10646-ucs-2</code>.
 591      *
 592      * @return a <code>DataFlavor</code> representing plain text
 593      *    with Unicode encoding
 594      * @since 1.3
 595      */
 596     public static final DataFlavor getTextPlainUnicodeFlavor() {
 597         String encoding = null;
 598         DataTransferer transferer = DataTransferer.getInstance();
 599         if (transferer != null) {
 600             encoding = transferer.getDefaultUnicodeEncoding();
 601         }
 602         return new DataFlavor(
 603             "text/plain;charset="+encoding
 604             +";class=java.io.InputStream", "Plain Text");
 605     }
 606 
 607     /**
 608      * Selects the best text <code>DataFlavor</code> from an array of <code>
 609      * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and
 610      * equivalent flavors, and flavors that have a primary MIME type of "text",
 611      * are considered for selection.
 612      * <p>
 613      * Flavors are first sorted by their MIME types in the following order:
 614      * <ul>
 615      * <li>"text/sgml"
 616      * <li>"text/xml"
 617      * <li>"text/html"
 618      * <li>"text/rtf"
 619      * <li>"text/enriched"
 620      * <li>"text/richtext"
 621      * <li>"text/uri-list"
 622      * <li>"text/tab-separated-values"
 623      * <li>"text/t140"
 624      * <li>"text/rfc822-headers"
 625      * <li>"text/parityfec"
 626      * <li>"text/directory"
 627      * <li>"text/css"
 628      * <li>"text/calendar"
 629      * <li>"application/x-java-serialized-object"
 630      * <li>"text/plain"
 631      * <li>"text/&lt;other&gt;"
 632      * </ul>
 633      * <p>For example, "text/sgml" will be selected over
 634      * "text/html", and <code>DataFlavor.stringFlavor</code> will be chosen
 635      * over <code>DataFlavor.plainTextFlavor</code>.
 636      * <p>
 637      * If two or more flavors share the best MIME type in the array, then that
 638      * MIME type will be checked to see if it supports the charset parameter.
 639      * <p>
 640      * The following MIME types support, or are treated as though they support,
 641      * the charset parameter:
 642      * <ul>
 643      * <li>"text/sgml"
 644      * <li>"text/xml"
 645      * <li>"text/html"
 646      * <li>"text/enriched"
 647      * <li>"text/richtext"
 648      * <li>"text/uri-list"
 649      * <li>"text/directory"
 650      * <li>"text/css"
 651      * <li>"text/calendar"
 652      * <li>"application/x-java-serialized-object"
 653      * <li>"text/plain"
 654      * </ul>
 655      * The following MIME types do not support, or are treated as though they
 656      * do not support, the charset parameter:
 657      * <ul>
 658      * <li>"text/rtf"
 659      * <li>"text/tab-separated-values"
 660      * <li>"text/t140"
 661      * <li>"text/rfc822-headers"
 662      * <li>"text/parityfec"
 663      * </ul>
 664      * For "text/&lt;other&gt;" MIME types, the first time the JRE needs to
 665      * determine whether the MIME type supports the charset parameter, it will
 666      * check whether the parameter is explicitly listed in an arbitrarily
 667      * chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE
 668      * will assume from that point on that the MIME type supports the charset
 669      * parameter and will not check again. If the parameter is not explicitly
 670      * listed, the JRE will assume from that point on that the MIME type does
 671      * not support the charset parameter and will not check again. Because
 672      * this check is performed on an arbitrarily chosen
 673      * <code>DataFlavor</code>, developers must ensure that all
 674      * <code>DataFlavor</code>s with a "text/&lt;other&gt;" MIME type specify
 675      * the charset parameter if it is supported by that MIME type. Developers
 676      * should never rely on the JRE to substitute the platform's default
 677      * charset for a "text/&lt;other&gt;" DataFlavor. Failure to adhere to this
 678      * restriction will lead to undefined behavior.
 679      * <p>
 680      * If the best MIME type in the array does not support the charset
 681      * parameter, the flavors which share that MIME type will then be sorted by
 682      * their representation classes in the following order:
 683      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
 684      * <code>[B</code>, &lt;all others&gt;.
 685      * <p>
 686      * If two or more flavors share the best representation class, or if no
 687      * flavor has one of the three specified representations, then one of those
 688      * flavors will be chosen non-deterministically.
 689      * <p>
 690      * If the best MIME type in the array does support the charset parameter,
 691      * the flavors which share that MIME type will then be sorted by their
 692      * representation classes in the following order:
 693      * <code>java.io.Reader</code>, <code>java.lang.String</code>,
 694      * <code>java.nio.CharBuffer</code>, <code>[C</code>, &lt;all others&gt;.
 695      * <p>
 696      * If two or more flavors share the best representation class, and that
 697      * representation is one of the four explicitly listed, then one of those
 698      * flavors will be chosen non-deterministically. If, however, no flavor has
 699      * one of the four specified representations, the flavors will then be
 700      * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8",
 701      * "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After
 702      * them, the platform default charset and its aliases are selected.
 703      * "US-ASCII" and its aliases are worst. All other charsets are chosen in
 704      * alphabetical order, but only charsets supported by this implementation
 705      * of the Java platform will be considered.
 706      * <p>
 707      * If two or more flavors share the best charset, the flavors will then
 708      * again be sorted by their representation classes in the following order:
 709      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
 710      * <code>[B</code>, &lt;all others&gt;.
 711      * <p>
 712      * If two or more flavors share the best representation class, or if no
 713      * flavor has one of the three specified representations, then one of those
 714      * flavors will be chosen non-deterministically.
 715      *
 716      * @param availableFlavors an array of available <code>DataFlavor</code>s
 717      * @return the best (highest fidelity) flavor according to the rules
 718      *         specified above, or <code>null</code>,
 719      *         if <code>availableFlavors</code> is <code>null</code>,
 720      *         has zero length, or contains no text flavors
 721      * @since 1.3
 722      */
 723     public static final DataFlavor selectBestTextFlavor(
 724                                        DataFlavor[] availableFlavors) {
 725         if (availableFlavors == null || availableFlavors.length == 0) {
 726             return null;
 727         }
 728 
 729         if (textFlavorComparator == null) {
 730             textFlavorComparator = new TextFlavorComparator();
 731         }
 732 
 733         DataFlavor bestFlavor = Collections.max(Arrays.asList(availableFlavors),
 734                                                 textFlavorComparator);
 735 
 736         if (!bestFlavor.isFlavorTextType()) {
 737             return null;
 738         }
 739 
 740         return bestFlavor;
 741     }
 742 
 743     private static Comparator<DataFlavor> textFlavorComparator;
 744 
 745     static class TextFlavorComparator
 746         extends DataTransferer.DataFlavorComparator {
 747 
 748         /**
 749          * Compares two <code>DataFlavor</code> objects. Returns a negative
 750          * integer, zero, or a positive integer as the first
 751          * <code>DataFlavor</code> is worse than, equal to, or better than the
 752          * second.
 753          * <p>
 754          * <code>DataFlavor</code>s are ordered according to the rules outlined
 755          * for <code>selectBestTextFlavor</code>.
 756          *
 757          * @param obj1 the first <code>DataFlavor</code> to be compared
 758          * @param obj2 the second <code>DataFlavor</code> to be compared
 759          * @return a negative integer, zero, or a positive integer as the first
 760          *         argument is worse, equal to, or better than the second
 761          * @throws ClassCastException if either of the arguments is not an
 762          *         instance of <code>DataFlavor</code>
 763          * @throws NullPointerException if either of the arguments is
 764          *         <code>null</code>
 765          *
 766          * @see #selectBestTextFlavor
 767          */
 768         public int compare(Object obj1, Object obj2) {
 769             DataFlavor flavor1 = (DataFlavor)obj1;
 770             DataFlavor flavor2 = (DataFlavor)obj2;
 771 
 772             if (flavor1.isFlavorTextType()) {
 773                 if (flavor2.isFlavorTextType()) {
 774                     return super.compare(obj1, obj2);
 775                 } else {
 776                     return 1;
 777                 }
 778             } else if (flavor2.isFlavorTextType()) {
 779                 return -1;
 780             } else {
 781                 return 0;
 782             }
 783         }
 784     }
 785 
 786     /**
 787      * Gets a Reader for a text flavor, decoded, if necessary, for the expected
 788      * charset (encoding). The supported representation classes are
 789      * <code>java.io.Reader</code>, <code>java.lang.String</code>,
 790      * <code>java.nio.CharBuffer</code>, <code>[C</code>,
 791      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
 792      * and <code>[B</code>.
 793      * <p>
 794      * Because text flavors which do not support the charset parameter are
 795      * encoded in a non-standard format, this method should not be called for
 796      * such flavors. However, in order to maintain backward-compatibility,
 797      * if this method is called for such a flavor, this method will treat the
 798      * flavor as though it supports the charset parameter and attempt to
 799      * decode it accordingly. See <code>selectBestTextFlavor</code> for a list
 800      * of text flavors which do not support the charset parameter.
 801      *
 802      * @param transferable the <code>Transferable</code> whose data will be
 803      *        requested in this flavor
 804      *
 805      * @return a <code>Reader</code> to read the <code>Transferable</code>'s
 806      *         data
 807      *
 808      * @exception IllegalArgumentException if the representation class
 809      *            is not one of the seven listed above
 810      * @exception IllegalArgumentException if the <code>Transferable</code>
 811      *            has <code>null</code> data
 812      * @exception NullPointerException if the <code>Transferable</code> is
 813      *            <code>null</code>
 814      * @exception UnsupportedEncodingException if this flavor's representation
 815      *            is <code>java.io.InputStream</code>,
 816      *            <code>java.nio.ByteBuffer</code>, or <code>[B</code> and
 817      *            this flavor's encoding is not supported by this
 818      *            implementation of the Java platform
 819      * @exception UnsupportedFlavorException if the <code>Transferable</code>
 820      *            does not support this flavor
 821      * @exception IOException if the data cannot be read because of an
 822      *            I/O error
 823      * @see #selectBestTextFlavor
 824      * @since 1.3
 825      */
 826     public Reader getReaderForText(Transferable transferable)
 827         throws UnsupportedFlavorException, IOException
 828     {
 829         Object transferObject = transferable.getTransferData(this);
 830         if (transferObject == null) {
 831             throw new IllegalArgumentException
 832                 ("getTransferData() returned null");
 833         }
 834 
 835         if (transferObject instanceof Reader) {
 836             return (Reader)transferObject;
 837         } else if (transferObject instanceof String) {
 838             return new StringReader((String)transferObject);
 839         } else if (transferObject instanceof CharBuffer) {
 840             CharBuffer buffer = (CharBuffer)transferObject;
 841             int size = buffer.remaining();
 842             char[] chars = new char[size];
 843             buffer.get(chars, 0, size);
 844             return new CharArrayReader(chars);
 845         } else if (transferObject instanceof char[]) {
 846             return new CharArrayReader((char[])transferObject);
 847         }
 848 
 849         InputStream stream = null;
 850 
 851         if (transferObject instanceof InputStream) {
 852             stream = (InputStream)transferObject;
 853         } else if (transferObject instanceof ByteBuffer) {
 854             ByteBuffer buffer = (ByteBuffer)transferObject;
 855             int size = buffer.remaining();
 856             byte[] bytes = new byte[size];
 857             buffer.get(bytes, 0, size);
 858             stream = new ByteArrayInputStream(bytes);
 859         } else if (transferObject instanceof byte[]) {
 860             stream = new ByteArrayInputStream((byte[])transferObject);
 861         }
 862 
 863         if (stream == null) {
 864             throw new IllegalArgumentException("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
 865         }
 866 
 867         String encoding = getParameter("charset");
 868         return (encoding == null)
 869             ? new InputStreamReader(stream)
 870             : new InputStreamReader(stream, encoding);
 871     }
 872 
 873     /**
 874      * Returns the MIME type string for this <code>DataFlavor</code>.
 875      * @return the MIME type string for this flavor
 876      */
 877     public String getMimeType() {
 878         return (mimeType != null) ? mimeType.toString() : null;
 879     }
 880 
 881     /**
 882      * Returns the <code>Class</code> which objects supporting this
 883      * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
 884      * is requested.
 885      * @return the <code>Class</code> which objects supporting this
 886      * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
 887      * is requested
 888      */
 889     public Class<?> getRepresentationClass() {
 890         return representationClass;
 891     }
 892 
 893     /**
 894      * Returns the human presentable name for the data format that this
 895      * <code>DataFlavor</code> represents.  This name would be localized
 896      * for different countries.
 897      * @return the human presentable name for the data format that this
 898      *    <code>DataFlavor</code> represents
 899      */
 900     public String getHumanPresentableName() {
 901         return humanPresentableName;
 902     }
 903 
 904     /**
 905      * Returns the primary MIME type for this <code>DataFlavor</code>.
 906      * @return the primary MIME type of this <code>DataFlavor</code>
 907      */
 908     public String getPrimaryType() {
 909         return (mimeType != null) ? mimeType.getPrimaryType() : null;
 910     }
 911 
 912     /**
 913      * Returns the sub MIME type of this <code>DataFlavor</code>.
 914      * @return the Sub MIME type of this <code>DataFlavor</code>
 915      */
 916     public String getSubType() {
 917         return (mimeType != null) ? mimeType.getSubType() : null;
 918     }
 919 
 920     /**
 921      * Returns the human presentable name for this <code>DataFlavor</code>
 922      * if <code>paramName</code> equals "humanPresentableName".  Otherwise
 923      * returns the MIME type value associated with <code>paramName</code>.
 924      *
 925      * @param paramName the parameter name requested
 926      * @return the value of the name parameter, or <code>null</code>
 927      *  if there is no associated value
 928      */
 929     public String getParameter(String paramName) {
 930         if (paramName.equals("humanPresentableName")) {
 931             return humanPresentableName;
 932         } else {
 933             return (mimeType != null)
 934                 ? mimeType.getParameter(paramName) : null;
 935         }
 936     }
 937 
 938     /**
 939      * Sets the human presentable name for the data format that this
 940      * <code>DataFlavor</code> represents. This name would be localized
 941      * for different countries.
 942      * @param humanPresentableName the new human presentable name
 943      */
 944     public void setHumanPresentableName(String humanPresentableName) {
 945         this.humanPresentableName = humanPresentableName;
 946     }
 947 
 948     /**
 949      * {@inheritDoc}
 950      * <p>
 951      * The equals comparison for the {@code DataFlavor} class is implemented
 952      * as follows: Two <code>DataFlavor</code>s are considered equal if and
 953      * only if their MIME primary type and subtype and representation class are
 954      * equal. Additionally, if the primary type is "text", the subtype denotes
 955      * a text flavor which supports the charset parameter, and the
 956      * representation class is not <code>java.io.Reader</code>,
 957      * <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or
 958      * <code>[C</code>, the <code>charset</code> parameter must also be equal.
 959      * If a charset is not explicitly specified for one or both
 960      * <code>DataFlavor</code>s, the platform default encoding is assumed. See
 961      * <code>selectBestTextFlavor</code> for a list of text flavors which
 962      * support the charset parameter.
 963      *
 964      * @param o the <code>Object</code> to compare with <code>this</code>
 965      * @return <code>true</code> if <code>that</code> is equivalent to this
 966      *         <code>DataFlavor</code>; <code>false</code> otherwise
 967      * @see #selectBestTextFlavor
 968      */
 969     public boolean equals(Object o) {
 970         return ((o instanceof DataFlavor) && equals((DataFlavor)o));
 971     }
 972 
 973     /**
 974      * This method has the same behavior as {@link #equals(Object)}.
 975      * The only difference being that it takes a {@code DataFlavor} instance
 976      * as a parameter.
 977      *
 978      * @param that the <code>DataFlavor</code> to compare with
 979      *        <code>this</code>
 980      * @return <code>true</code> if <code>that</code> is equivalent to this
 981      *         <code>DataFlavor</code>; <code>false</code> otherwise
 982      * @see #selectBestTextFlavor
 983      */
 984     public boolean equals(DataFlavor that) {
 985         if (that == null) {
 986             return false;
 987         }
 988         if (this == that) {
 989             return true;
 990         }
 991 
 992         if (representationClass == null) {
 993             if (that.getRepresentationClass() != null) {
 994                 return false;
 995             }
 996         } else {
 997             if (!representationClass.equals(that.getRepresentationClass())) {
 998                 return false;
 999             }
1000         }
1001 
1002         if (mimeType == null) {
1003             if (that.mimeType != null) {
1004                 return false;
1005             }
1006         } else {
1007             if (!mimeType.match(that.mimeType)) {
1008                 return false;
1009             }
1010 
1011             if ("text".equals(getPrimaryType())) {
1012                 if (DataTransferer.doesSubtypeSupportCharset(this) &&
1013                     representationClass != null &&
1014                     !(isRepresentationClassReader() ||
1015                         String.class.equals(representationClass) ||
1016                         isRepresentationClassCharBuffer() ||
1017                         DataTransferer.charArrayClass.equals(representationClass)))
1018                 {
1019                     String thisCharset =
1020                         DataTransferer.canonicalName(getParameter("charset"));
1021                     String thatCharset =
1022                         DataTransferer.canonicalName(that.getParameter("charset"));
1023                     if (thisCharset == null) {
1024                         if (thatCharset != null) {
1025                             return false;
1026                         }
1027                     } else {
1028                         if (!thisCharset.equals(thatCharset)) {
1029                             return false;
1030                         }
1031                     }
1032                 }
1033 
1034                 if ("html".equals(getSubType()) &&
1035                         this.getParameter("document") != null )
1036                 {
1037                    if (!this.getParameter("document").
1038                             equals(that.getParameter("document")))
1039                     {
1040                         return false;
1041                     }
1042                 }
1043             }
1044         }
1045 
1046         return true;
1047     }
1048 
1049     /**
1050      * Compares only the <code>mimeType</code> against the passed in
1051      * <code>String</code> and <code>representationClass</code> is
1052      * not considered in the comparison.
1053      *
1054      * If <code>representationClass</code> needs to be compared, then
1055      * <code>equals(new DataFlavor(s))</code> may be used.
1056      * @deprecated As inconsistent with <code>hashCode()</code> contract,
1057      *             use <code>isMimeTypeEqual(String)</code> instead.
1058      * @param s the {@code mimeType} to compare.
1059      * @return true if the String (MimeType) is equal; false otherwise or if
1060      *         {@code s} is {@code null}
1061      */
1062     @Deprecated
1063     public boolean equals(String s) {
1064         if (s == null || mimeType == null)
1065             return false;
1066         return isMimeTypeEqual(s);
1067     }
1068 
1069     /**
1070      * Returns hash code for this <code>DataFlavor</code>.
1071      * For two equal <code>DataFlavor</code>s, hash codes are equal.
1072      * For the <code>String</code>
1073      * that matches <code>DataFlavor.equals(String)</code>, it is not
1074      * guaranteed that <code>DataFlavor</code>'s hash code is equal
1075      * to the hash code of the <code>String</code>.
1076      *
1077      * @return a hash code for this <code>DataFlavor</code>
1078      */
1079     public int hashCode() {
1080         int total = 0;
1081 
1082         if (representationClass != null) {
1083             total += representationClass.hashCode();
1084         }
1085 
1086         if (mimeType != null) {
1087             String primaryType = mimeType.getPrimaryType();
1088             if (primaryType != null) {
1089                 total += primaryType.hashCode();
1090             }
1091 
1092             // Do not add subType.hashCode() to the total. equals uses
1093             // MimeType.match which reports a match if one or both of the
1094             // subTypes is '*', regardless of the other subType.
1095 
1096             if ("text".equals(primaryType) &&
1097                 DataTransferer.doesSubtypeSupportCharset(this) &&
1098                 representationClass != null &&
1099                 !(isRepresentationClassReader() ||
1100                   String.class.equals(representationClass) ||
1101                   isRepresentationClassCharBuffer() ||
1102                   DataTransferer.charArrayClass.equals
1103                   (representationClass)))
1104             {
1105                 String charset =
1106                     DataTransferer.canonicalName(getParameter("charset"));
1107                 if (charset != null) {
1108                     total += charset.hashCode();
1109                 }
1110             }
1111         }
1112 
1113         return total;
1114     }
1115 
1116     /**
1117      * Identical to {@link #equals(DataFlavor)}.
1118      *
1119      * @param that the <code>DataFlavor</code> to compare with
1120      *        <code>this</code>
1121      * @return <code>true</code> if <code>that</code> is equivalent to this
1122      *         <code>DataFlavor</code>; <code>false</code> otherwise
1123      * @see #selectBestTextFlavor
1124      * @since 1.3
1125      */
1126     public boolean match(DataFlavor that) {
1127         return equals(that);
1128     }
1129 
1130     /**
1131      * Returns whether the string representation of the MIME type passed in
1132      * is equivalent to the MIME type of this <code>DataFlavor</code>.
1133      * Parameters are not included in the comparison.
1134      *
1135      * @param mimeType the string representation of the MIME type
1136      * @return true if the string representation of the MIME type passed in is
1137      *         equivalent to the MIME type of this <code>DataFlavor</code>;
1138      *         false otherwise
1139      * @throws NullPointerException if mimeType is <code>null</code>
1140      */
1141     public boolean isMimeTypeEqual(String mimeType) {
1142         // JCK Test DataFlavor0117: if 'mimeType' is null, throw NPE
1143         if (mimeType == null) {
1144             throw new NullPointerException("mimeType");
1145         }
1146         if (this.mimeType == null) {
1147             return false;
1148         }
1149         try {
1150             return this.mimeType.match(new MimeType(mimeType));
1151         } catch (MimeTypeParseException mtpe) {
1152             return false;
1153         }
1154     }
1155 
1156     /**
1157      * Compares the <code>mimeType</code> of two <code>DataFlavor</code>
1158      * objects. No parameters are considered.
1159      *
1160      * @param dataFlavor the <code>DataFlavor</code> to be compared
1161      * @return true if the <code>MimeType</code>s are equal,
1162      *  otherwise false
1163      */
1164 
1165     public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
1166         return isMimeTypeEqual(dataFlavor.mimeType);
1167     }
1168 
1169     /**
1170      * Compares the <code>mimeType</code> of two <code>DataFlavor</code>
1171      * objects.  No parameters are considered.
1172      *
1173      * @return true if the <code>MimeType</code>s are equal,
1174      *  otherwise false
1175      */
1176 
1177     private boolean isMimeTypeEqual(MimeType mtype) {
1178         if (this.mimeType == null) {
1179             return (mtype == null);
1180         }
1181         return mimeType.match(mtype);
1182     }
1183 
1184    /**
1185     * Does the <code>DataFlavor</code> represent a serialized object?
1186     * @return whether or not a serialized object is represented
1187     */
1188     public boolean isMimeTypeSerializedObject() {
1189         return isMimeTypeEqual(javaSerializedObjectMimeType);
1190     }
1191 
1192     /**
1193      * Returns the default representation class.
1194      * @return the default representation class
1195      */
1196     public final Class<?> getDefaultRepresentationClass() {
1197         return ioInputStreamClass;
1198     }
1199 
1200     /**
1201      * Returns the name of the default representation class.
1202      * @return the name of the default representation class
1203      */
1204     public final String getDefaultRepresentationClassAsString() {
1205         return getDefaultRepresentationClass().getName();
1206     }
1207 
1208    /**
1209     * Does the <code>DataFlavor</code> represent a
1210     * <code>java.io.InputStream</code>?
1211     * @return whether or not this {@code DataFlavor} represent a
1212     * {@code java.io.InputStream}
1213     */
1214     public boolean isRepresentationClassInputStream() {
1215         return ioInputStreamClass.isAssignableFrom(representationClass);
1216     }
1217 
1218     /**
1219      * Returns whether the representation class for this
1220      * <code>DataFlavor</code> is <code>java.io.Reader</code> or a subclass
1221      * thereof.
1222      * @return whether or not the representation class for this
1223      * {@code DataFlavor} is {@code java.io.Reader} or a subclass
1224      * thereof
1225      *
1226      * @since 1.4
1227      */
1228     public boolean isRepresentationClassReader() {
1229         return java.io.Reader.class.isAssignableFrom(representationClass);
1230     }
1231 
1232     /**
1233      * Returns whether the representation class for this
1234      * <code>DataFlavor</code> is <code>java.nio.CharBuffer</code> or a
1235      * subclass thereof.
1236      * @return whether or not the representation class for this
1237      * {@code DataFlavor} is {@code java.nio.CharBuffer} or a subclass
1238      * thereof
1239      *
1240      * @since 1.4
1241      */
1242     public boolean isRepresentationClassCharBuffer() {
1243         return java.nio.CharBuffer.class.isAssignableFrom(representationClass);
1244     }
1245 
1246     /**
1247      * Returns whether the representation class for this
1248      * <code>DataFlavor</code> is <code>java.nio.ByteBuffer</code> or a
1249      * subclass thereof.
1250      * @return whether or not the representation class for this
1251      * {@code DataFlavor} is {@code java.nio.ByteBuffer} or a subclass
1252      * thereof
1253      *
1254      * @since 1.4
1255      */
1256     public boolean isRepresentationClassByteBuffer() {
1257         return java.nio.ByteBuffer.class.isAssignableFrom(representationClass);
1258     }
1259 
1260    /**
1261     * Returns true if the representation class can be serialized.
1262     * @return true if the representation class can be serialized
1263     */
1264 
1265     public boolean isRepresentationClassSerializable() {
1266         return java.io.Serializable.class.isAssignableFrom(representationClass);
1267     }
1268 
1269    /**
1270     * Returns true if the representation class is <code>Remote</code>.
1271     * @return true if the representation class is <code>Remote</code>
1272     */
1273 
1274     public boolean isRepresentationClassRemote() {
1275         return DataTransferer.isRemote(representationClass);
1276     }
1277 
1278    /**
1279     * Returns true if the <code>DataFlavor</code> specified represents
1280     * a serialized object.
1281     * @return true if the <code>DataFlavor</code> specified represents
1282     *   a Serialized Object
1283     */
1284 
1285     public boolean isFlavorSerializedObjectType() {
1286         return isRepresentationClassSerializable() && isMimeTypeEqual(javaSerializedObjectMimeType);
1287     }
1288 
1289     /**
1290      * Returns true if the <code>DataFlavor</code> specified represents
1291      * a remote object.
1292      * @return true if the <code>DataFlavor</code> specified represents
1293      *  a Remote Object
1294      */
1295 
1296     public boolean isFlavorRemoteObjectType() {
1297         return isRepresentationClassRemote()
1298             && isRepresentationClassSerializable()
1299             && isMimeTypeEqual(javaRemoteObjectMimeType);
1300     }
1301 
1302 
1303    /**
1304     * Returns true if the <code>DataFlavor</code> specified represents
1305     * a list of file objects.
1306     * @return true if the <code>DataFlavor</code> specified represents
1307     *   a List of File objects
1308     */
1309 
1310    public boolean isFlavorJavaFileListType() {
1311         if (mimeType == null || representationClass == null)
1312             return false;
1313         return java.util.List.class.isAssignableFrom(representationClass) &&
1314                mimeType.match(javaFileListFlavor.mimeType);
1315 
1316    }
1317 
1318     /**
1319      * Returns whether this <code>DataFlavor</code> is a valid text flavor for
1320      * this implementation of the Java platform. Only flavors equivalent to
1321      * <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with
1322      * a primary MIME type of "text" can be valid text flavors.
1323      * <p>
1324      * If this flavor supports the charset parameter, it must be equivalent to
1325      * <code>DataFlavor.stringFlavor</code>, or its representation must be
1326      * <code>java.io.Reader</code>, <code>java.lang.String</code>,
1327      * <code>java.nio.CharBuffer</code>, <code>[C</code>,
1328      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
1329      * <code>[B</code>. If the representation is
1330      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
1331      * <code>[B</code>, then this flavor's <code>charset</code> parameter must
1332      * be supported by this implementation of the Java platform. If a charset
1333      * is not specified, then the platform default charset, which is always
1334      * supported, is assumed.
1335      * <p>
1336      * If this flavor does not support the charset parameter, its
1337      * representation must be <code>java.io.InputStream</code>,
1338      * <code>java.nio.ByteBuffer</code>, or <code>[B</code>.
1339      * <p>
1340      * See <code>selectBestTextFlavor</code> for a list of text flavors which
1341      * support the charset parameter.
1342      *
1343      * @return <code>true</code> if this <code>DataFlavor</code> is a valid
1344      *         text flavor as described above; <code>false</code> otherwise
1345      * @see #selectBestTextFlavor
1346      * @since 1.4
1347      */
1348     public boolean isFlavorTextType() {
1349         return (DataTransferer.isFlavorCharsetTextType(this) ||
1350                 DataTransferer.isFlavorNoncharsetTextType(this));
1351     }
1352 
1353    /**
1354     * Serializes this <code>DataFlavor</code>.
1355     */
1356 
1357    public synchronized void writeExternal(ObjectOutput os) throws IOException {
1358        if (mimeType != null) {
1359            mimeType.setParameter("humanPresentableName", humanPresentableName);
1360            os.writeObject(mimeType);
1361            mimeType.removeParameter("humanPresentableName");
1362        } else {
1363            os.writeObject(null);
1364        }
1365 
1366        os.writeObject(representationClass);
1367    }
1368 
1369    /**
1370     * Restores this <code>DataFlavor</code> from a Serialized state.
1371     */
1372 
1373    public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException {
1374        String rcn = null;
1375         mimeType = (MimeType)is.readObject();
1376 
1377         if (mimeType != null) {
1378             humanPresentableName =
1379                 mimeType.getParameter("humanPresentableName");
1380             mimeType.removeParameter("humanPresentableName");
1381             rcn = mimeType.getParameter("class");
1382             if (rcn == null) {
1383                 throw new IOException("no class parameter specified in: " +
1384                                       mimeType);
1385             }
1386         }
1387 
1388         try {
1389             representationClass = (Class)is.readObject();
1390         } catch (OptionalDataException ode) {
1391             if (!ode.eof || ode.length != 0) {
1392                 throw ode;
1393             }
1394             // Ensure backward compatibility.
1395             // Old versions didn't write the representation class to the stream.
1396             if (rcn != null) {
1397                 representationClass =
1398                     DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
1399             }
1400         }
1401    }
1402 
1403    /**
1404     * Returns a clone of this <code>DataFlavor</code>.
1405     * @return a clone of this <code>DataFlavor</code>
1406     */
1407 
1408     public Object clone() throws CloneNotSupportedException {
1409         Object newObj = super.clone();
1410         if (mimeType != null) {
1411             ((DataFlavor)newObj).mimeType = (MimeType)mimeType.clone();
1412         }
1413         return newObj;
1414     } // clone()
1415 
1416    /**
1417     * Called on <code>DataFlavor</code> for every MIME Type parameter
1418     * to allow <code>DataFlavor</code> subclasses to handle special
1419     * parameters like the text/plain <code>charset</code>
1420     * parameters, whose values are case insensitive.  (MIME type parameter
1421     * values are supposed to be case sensitive.
1422     * <p>
1423     * This method is called for each parameter name/value pair and should
1424     * return the normalized representation of the <code>parameterValue</code>.
1425     *
1426     * This method is never invoked by this implementation from 1.1 onwards.
1427     *
1428     * @param parameterName the parameter name
1429     * @param parameterValue the parameter value
1430     * @return the parameter value
1431     * @deprecated
1432     */
1433     @Deprecated
1434     protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
1435         return parameterValue;
1436     }
1437 
1438    /**
1439     * Called for each MIME type string to give <code>DataFlavor</code> subtypes
1440     * the opportunity to change how the normalization of MIME types is
1441     * accomplished.  One possible use would be to add default
1442     * parameter/value pairs in cases where none are present in the MIME
1443     * type string passed in.
1444     *
1445     * This method is never invoked by this implementation from 1.1 onwards.
1446     *
1447     * @param mimeType the mime type
1448     * @return the mime type
1449     * @deprecated
1450     */
1451     @Deprecated
1452     protected String normalizeMimeType(String mimeType) {
1453         return mimeType;
1454     }
1455 
1456     /*
1457      * fields
1458      */
1459 
1460     /* placeholder for caching any platform-specific data for flavor */
1461 
1462     transient int       atom;
1463 
1464     /* Mime Type of DataFlavor */
1465 
1466     MimeType            mimeType;
1467 
1468     private String      humanPresentableName;
1469 
1470     /** Java class of objects this DataFlavor represents **/
1471 
1472     private Class<?>       representationClass;
1473 
1474 } // class DataFlavor