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 * "class" 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 "text" primary MIME
83 * type may have a "charset" parameter. Refer to
84 * <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a> and
85 * {@link #selectBestTextFlavor} for details on "text" MIME types and
86 * the "charset" 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, "X-test/test; class=<java.lang.Object>; foo=bar");
94 * DataFlavor flavor2 = new DataFlavor(Object.class, "X-test/test; class=<java.lang.Object>; x=y");
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>The Java Tutorial</em>.
106 *
107 * @author Blake Sullivan
108 * @author Laurence P. G. Cable
109 * @author Jeff Dunn
110 * @since 1.1
111 */
112 public class DataFlavor implements Externalizable, Cloneable {
113
114 private static final long serialVersionUID = 8367026044764648243L;
115 private static final Class<InputStream> ioInputStreamClass = InputStream.class;
116
117 /**
118 * Tries to load a class from: the bootstrap loader, the system loader, the
119 * context loader (if one is present) and finally the loader specified.
120 *
121 * @param className the name of the class to be loaded
122 * @param fallback the fallback loader
123 * @return the class loaded
124 * @throws ClassNotFoundException if class is not found
125 */
126 protected static final Class<?> tryToLoadClass(String className,
127 ClassLoader fallback)
128 throws ClassNotFoundException
129 {
130 ReflectUtil.checkPackageAccess(className);
131 try {
132 SecurityManager sm = System.getSecurityManager();
133 if (sm != null) {
134 sm.checkPermission(new RuntimePermission("getClassLoader"));
135 }
136 ClassLoader loader = ClassLoader.getSystemClassLoader();
137 try {
138 // bootstrap class loader and system class loader if present
139 return Class.forName(className, true, loader);
140 }
141 catch (ClassNotFoundException exception) {
142 // thread context class loader if and only if present
143 loader = Thread.currentThread().getContextClassLoader();
144 if (loader != null) {
145 try {
146 return Class.forName(className, true, loader);
147 }
148 catch (ClassNotFoundException e) {
149 // fallback to user's class loader
150 }
151 }
152 }
153 } catch (SecurityException exception) {
154 // ignore secured class loaders
155 }
156 return Class.forName(className, true, fallback);
157 }
158
159 /*
160 * private initializer
161 */
162 private static DataFlavor createConstant(Class<?> rc, String prn) {
163 try {
164 return new DataFlavor(rc, prn);
165 } catch (Exception e) {
166 return null;
167 }
168 }
169
170 /*
171 * private initializer
172 */
173 private static DataFlavor createConstant(String mt, String prn) {
174 try {
175 return new DataFlavor(mt, prn);
176 } catch (Exception e) {
177 return null;
178 }
179 }
180
181 /*
182 * private initializer
183 */
184 private static DataFlavor initHtmlDataFlavor(String htmlFlavorType) {
185 try {
186 return new DataFlavor ("text/html; class=java.lang.String;document=" +
187 htmlFlavorType + ";charset=Unicode");
188 } catch (Exception e) {
189 return null;
190 }
191 }
192
193 /**
194 * The {@code DataFlavor} representing a Java Unicode String class, where:
195 * <pre>
196 * representationClass = java.lang.String
197 * mimeType = "application/x-java-serialized-object"
198 * </pre>
199 */
200 public static final DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
201
202 /**
203 * The {@code DataFlavor} representing a Java Image class, where:
204 * <pre>
205 * representationClass = java.awt.Image
206 * mimeType = "image/x-java-image"
207 * </pre>
208 * Will be {@code null} if {@code java.awt.Image} is not visible, the
209 * {@code java.desktop} module is not loaded, or the {@code java.desktop}
210 * module is not in the run-time image.
211 */
212 public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
213
214 /**
215 * The {@code DataFlavor} representing plain text with Unicode encoding,
216 * where:
217 * <pre>
218 * representationClass = InputStream
219 * mimeType = "text/plain; charset=unicode"
220 * </pre>
221 * This {@code DataFlavor} has been <b>deprecated</b> because:
222 * <ul>
223 * <li>Its representation is an InputStream, an 8-bit based representation,
224 * while Unicode is a 16-bit character set</li>
225 * <li>The charset "unicode" is not well-defined. "unicode" implies a
226 * particular platform's implementation of Unicode, not a cross-platform
227 * implementation</li>
228 * </ul>
229 *
230 * @deprecated as of 1.3. Use {@link #getReaderForText} instead of
231 * {@code Transferable.getTransferData(DataFlavor.plainTextFlavor)}.
232 */
233 @Deprecated
234 public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
235
236 /**
237 * A MIME Content-Type of application/x-java-serialized-object represents a
238 * graph of Java object(s) that have been made persistent.
239 * <p>
240 * The representation class associated with this {@code DataFlavor}
241 * identifies the Java type of an object returned as a reference from an
242 * invocation {@code java.awt.datatransfer.getTransferData}.
243 */
244 public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";
245
246 /**
247 * To transfer a list of files to/from Java (and the underlying platform) a
248 * {@code DataFlavor} of this type/subtype and representation class of
249 * {@code java.util.List} is used. Each element of the list is
250 * required/guaranteed to be of type {@code java.io.File}.
251 */
252 public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
253
254 /**
255 * To transfer a reference to an arbitrary Java object reference that has no
256 * associated MIME Content-type, across a {@code Transferable} interface
257 * WITHIN THE SAME JVM, a {@code DataFlavor} with this type/subtype is used,
258 * with a {@code representationClass} equal to the type of the
259 * class/interface being passed across the {@code Transferable}.
260 * <p>
261 * The object reference returned from {@code Transferable.getTransferData}
262 * for a {@code DataFlavor} with this MIME Content-Type is required to be an
263 * instance of the representation Class of the {@code DataFlavor}.
264 */
265 public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
266
267 /**
268 * In order to pass a live link to a Remote object via a Drag and Drop
269 * {@code ACTION_LINK} operation a Mime Content Type of
270 * application/x-java-remote-object should be used, where the representation
271 * class of the {@code DataFlavor} represents the type of the {@code Remote}
272 * interface to be transferred.
273 */
274 public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";
275
276 /**
277 * Represents a piece of an HTML markup. The markup consists of the part
278 * selected on the source side. Therefore some tags in the markup may be
279 * unpaired. If the flavor is used to represent the data in a
280 * {@link Transferable} instance, no additional changes will be made. This
281 * DataFlavor instance represents the same HTML markup as DataFlavor
282 * instances which content MIME type does not contain document parameter
283 * and representation class is the String class.
284 * <pre>
285 * representationClass = String
286 * mimeType = "text/html"
287 * </pre>
288 *
289 * @since 1.8
290 */
291 public static DataFlavor selectionHtmlFlavor = initHtmlDataFlavor("selection");
292
293 /**
294 * Represents a piece of an HTML markup. If possible, the markup received
295 * from a native system is supplemented with pair tags to be a well-formed
296 * HTML markup. If the flavor is used to represent the data in a
297 * {@link Transferable} instance, no additional changes will be made.
298 * <pre>
299 * representationClass = String
300 * mimeType = "text/html"
301 * </pre>
302 *
303 * @since 1.8
304 */
305 public static DataFlavor fragmentHtmlFlavor = initHtmlDataFlavor("fragment");
306
307 /**
308 * Represents a piece of an HTML markup. If possible, the markup received
309 * from a native system is supplemented with additional tags to make up a
310 * well-formed HTML document. If the flavor is used to represent the data in
311 * a {@link Transferable} instance, no additional changes will be made.
312 * <pre>
313 * representationClass = String
314 * mimeType = "text/html"
315 * </pre>
316 *
317 * @since 1.8
318 */
319 public static DataFlavor allHtmlFlavor = initHtmlDataFlavor("all");
320
321 /**
322 * Constructs a new {@code DataFlavor}. This constructor is provided only
323 * for the purpose of supporting the {@code Externalizable} interface. It is
324 * not intended for public (client) use.
325 *
326 * @since 1.2
327 */
328 public DataFlavor() {
329 super();
330 }
331
332 /**
333 * Constructs a fully specified {@code DataFlavor}.
334 *
335 * @throws NullPointerException if either {@code primaryType},
336 * {@code subType} or {@code representationClass} is {@code null}
337 */
338 private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class<?> representationClass, String humanPresentableName) {
339 super();
340 if (primaryType == null) {
341 throw new NullPointerException("primaryType");
342 }
343 if (subType == null) {
344 throw new NullPointerException("subType");
345 }
346 if (representationClass == null) {
347 throw new NullPointerException("representationClass");
348 }
349
350 if (params == null) params = new MimeTypeParameterList();
351
352 params.set("class", representationClass.getName());
353
354 if (humanPresentableName == null) {
355 humanPresentableName = params.get("humanPresentableName");
356
357 if (humanPresentableName == null)
358 humanPresentableName = primaryType + "/" + subType;
359 }
360
361 try {
362 mimeType = new MimeType(primaryType, subType, params);
363 } catch (MimeTypeParseException mtpe) {
364 throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
365 }
366
367 this.representationClass = representationClass;
368 this.humanPresentableName = humanPresentableName;
369
370 mimeType.removeParameter("humanPresentableName");
371 }
372
373 /**
374 * Constructs a {@code DataFlavor} that represents a Java class.
375 * <p>
376 * The returned {@code DataFlavor} will have the following characteristics:
377 * <pre>
378 * representationClass = representationClass
379 * mimeType = application/x-java-serialized-object
380 * </pre>
381 *
382 * @param representationClass the class used to transfer data in this
383 * flavor
384 * @param humanPresentableName the human-readable string used to identify
385 * this flavor; if this parameter is {@code null} then the value of
386 * the MIME Content Type is used
387 * @throws NullPointerException if {@code representationClass} is
388 * {@code null}
389 */
390 public DataFlavor(Class<?> representationClass, String humanPresentableName) {
391 this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
392 if (representationClass == null) {
393 throw new NullPointerException("representationClass");
394 }
395 }
396
397 /**
398 * Constructs a {@code DataFlavor} that represents a {@code MimeType}.
399 * <p>
400 * The returned {@code DataFlavor} will have the following characteristics:
401 * <p>
402 * If the {@code mimeType} is
403 * "application/x-java-serialized-object; class=<representation class>",
404 * the result is the same as calling
405 * {@code new DataFlavor(Class.forName(<representation class>)}.
406 * <p>
407 * Otherwise:
408 * <pre>
409 * representationClass = InputStream
410 * mimeType = mimeType
411 * </pre>
412 *
413 * @param mimeType the string used to identify the MIME type for this
414 * flavor; if the {@code mimeType} does not specify a "class="
415 * parameter, or if the class is not successfully loaded, then an
416 * {@code IllegalArgumentException} is thrown
417 * @param humanPresentableName the human-readable string used to identify
418 * this flavor; if this parameter is {@code null} then the value of
419 * the MIME Content Type is used
420 * @throws IllegalArgumentException if {@code mimeType} is invalid or if the
421 * class is not successfully loaded
422 * @throws NullPointerException if {@code mimeType} is {@code null}
423 */
424 public DataFlavor(String mimeType, String humanPresentableName) {
425 super();
426 if (mimeType == null) {
427 throw new NullPointerException("mimeType");
428 }
429 try {
430 initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
431 } catch (MimeTypeParseException mtpe) {
432 throw new IllegalArgumentException("failed to parse:" + mimeType);
433 } catch (ClassNotFoundException cnfe) {
434 throw new IllegalArgumentException("can't find specified class: " + cnfe.getMessage());
435 }
436 }
437
438 /**
439 * Constructs a {@code DataFlavor} that represents a {@code MimeType}.
440 * <p>
441 * The returned {@code DataFlavor} will have the following characteristics:
442 * <p>
443 * If the mimeType is
444 * "application/x-java-serialized-object; class=<representation class>",
445 * the result is the same as calling
446 * {@code new DataFlavor(Class.forName(<representation class>)}.
447 * <p>
448 * Otherwise:
449 * <pre>
450 * representationClass = InputStream
451 * mimeType = mimeType
452 * </pre>
453 *
454 * @param mimeType the string used to identify the MIME type for this
455 * flavor
456 * @param humanPresentableName the human-readable string used to identify
457 * this flavor
458 * @param classLoader the class loader to use
459 * @throws ClassNotFoundException if the class is not loaded
460 * @throws IllegalArgumentException if {@code mimeType} is invalid
461 * @throws NullPointerException if {@code mimeType} is {@code null}
462 */
463 public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
464 super();
465 if (mimeType == null) {
466 throw new NullPointerException("mimeType");
467 }
468 try {
469 initialize(mimeType, humanPresentableName, classLoader);
470 } catch (MimeTypeParseException mtpe) {
471 throw new IllegalArgumentException("failed to parse:" + mimeType);
472 }
473 }
474
475 /**
476 * Constructs a {@code DataFlavor} from a {@code mimeType} string. The
477 * string can specify a "class=<fully specified Java class name>"
478 * parameter to create a {@code DataFlavor} with the desired representation
479 * class. If the string does not contain "class=" parameter,
480 * {@code java.io.InputStream} is used as default.
481 *
482 * @param mimeType the string used to identify the MIME type for this
483 * flavor; if the class specified by "class=" parameter is not
484 * successfully loaded, then a {@code ClassNotFoundException} is
485 * thrown
486 * @throws ClassNotFoundException if the class is not loaded
487 * @throws IllegalArgumentException if {@code mimeType} is invalid
488 * @throws NullPointerException if {@code mimeType} is {@code null}
489 */
490 public DataFlavor(String mimeType) throws ClassNotFoundException {
491 super();
492 if (mimeType == null) {
493 throw new NullPointerException("mimeType");
494 }
495 try {
496 initialize(mimeType, null, this.getClass().getClassLoader());
497 } catch (MimeTypeParseException mtpe) {
498 throw new IllegalArgumentException("failed to parse:" + mimeType);
499 }
500 }
501
502 /**
503 * Common initialization code called from various constructors.
504 *
505 * @param mimeType the MIME Content Type (must have a class= param)
506 * @param humanPresentableName the human Presentable Name or {@code null}
507 * @param classLoader the fallback class loader to resolve against
508 * @throws MimeTypeParseException
509 * @throws ClassNotFoundException
510 * @throws NullPointerException if {@code mimeType} is {@code null}
511 * @see #tryToLoadClass
512 */
513 private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException {
514 if (mimeType == null) {
515 throw new NullPointerException("mimeType");
516 }
517
518 this.mimeType = new MimeType(mimeType); // throws
519
520 String rcn = getParameter("class");
521
522 if (rcn == null) {
523 if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
524
525 throw new IllegalArgumentException("no representation class specified for:" + mimeType);
526 else
527 representationClass = java.io.InputStream.class; // default
528 } else { // got a class name
529 representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
530 }
531
532 this.mimeType.setParameter("class", representationClass.getName());
533
534 if (humanPresentableName == null) {
535 humanPresentableName = this.mimeType.getParameter("humanPresentableName");
536 if (humanPresentableName == null)
537 humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
538 }
539
540 this.humanPresentableName = humanPresentableName; // set it.
541
542 this.mimeType.removeParameter("humanPresentableName"); // just in case
543 }
544
545 /**
546 * String representation of this {@code DataFlavor} and its parameters. The
547 * resulting {@code String} contains the name of the {@code DataFlavor}
548 * class, this flavor's MIME type, and its representation class. If this
549 * flavor has a primary MIME type of "text", supports the charset parameter,
550 * and has an encoded representation, the flavor's charset is also included.
551 * See {@code selectBestTextFlavor} for a list of text flavors which support
552 * the charset parameter.
553 *
554 * @return string representation of this {@code DataFlavor}
555 * @see #selectBestTextFlavor
556 */
557 public String toString() {
558 String string = getClass().getName();
559 string += "["+paramString()+"]";
560 return string;
561 }
562
563 private String paramString() {
564 String params = "";
565 params += "mimetype=";
566 if (mimeType == null) {
567 params += "null";
568 } else {
569 params += mimeType.getBaseType();
570 }
571 params += ";representationclass=";
572 if (representationClass == null) {
573 params += "null";
574 } else {
575 params += representationClass.getName();
576 }
577 if (DataFlavorUtil.isFlavorCharsetTextType(this) &&
578 (isRepresentationClassInputStream() ||
579 isRepresentationClassByteBuffer() ||
580 byte[].class.equals(representationClass)))
581 {
582 params += ";charset=" + DataFlavorUtil.getTextCharset(this);
583 }
584 return params;
585 }
586
587 /**
588 * Returns a {@code DataFlavor} representing plain text with Unicode
589 * encoding, where:
590 * <pre>
591 * representationClass = java.io.InputStream
592 * mimeType = "text/plain;
593 * charset=<platform default Unicode encoding>"
594 * </pre>
595 * @implNote Oracle's implementation for Microsoft Windows and macOS uses
596 * the encoding {@code utf-16le}. Oracle's implementation for Solaris and
597 * Linux uses the encoding {@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/<other>"
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/<other>" 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/<other>" 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/<other>" 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 * <all others>.
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 * <all others>.
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 * <all others>.
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 * {@code java.util.List} of {@code java.io.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
1410