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