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