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