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