1 /* 2 * Copyright (c) 2000, 2008, 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 sun.awt.datatransfer; 27 28 import java.awt.AWTError; 29 import java.awt.EventQueue; 30 import java.awt.Image; 31 import java.awt.Graphics; 32 33 import java.awt.datatransfer.DataFlavor; 34 import java.awt.datatransfer.FlavorMap; 35 import java.awt.datatransfer.FlavorTable; 36 import java.awt.datatransfer.Transferable; 37 import java.awt.datatransfer.UnsupportedFlavorException; 38 39 import java.io.BufferedReader; 40 import java.io.ByteArrayInputStream; 41 import java.io.ByteArrayOutputStream; 42 import java.io.File; 43 import java.io.InputStream; 44 import java.io.InputStreamReader; 45 import java.io.IOException; 46 import java.io.ObjectInputStream; 47 import java.io.ObjectOutputStream; 48 import java.io.Reader; 49 import java.io.SequenceInputStream; 50 import java.io.StringReader; 51 52 import java.net.URI; 53 import java.net.URISyntaxException; 54 55 import java.nio.ByteBuffer; 56 import java.nio.CharBuffer; 57 import java.nio.charset.Charset; 58 import java.nio.charset.CharsetEncoder; 59 import java.nio.charset.IllegalCharsetNameException; 60 import java.nio.charset.UnsupportedCharsetException; 61 62 import java.lang.reflect.Constructor; 63 import java.lang.reflect.InvocationTargetException; 64 import java.lang.reflect.Method; 65 import java.lang.reflect.Modifier; 66 67 import java.security.AccessController; 68 import java.security.PrivilegedAction; 69 import java.security.PrivilegedActionException; 70 import java.security.PrivilegedExceptionAction; 71 import java.security.ProtectionDomain; 72 73 import java.util.ArrayList; 74 import java.util.Arrays; 75 import java.util.Collections; 76 import java.util.Comparator; 77 import java.util.HashMap; 78 import java.util.HashSet; 79 import java.util.Iterator; 80 import java.util.List; 81 import java.util.Map; 82 import java.util.SortedMap; 83 import java.util.SortedSet; 84 import java.util.Set; 85 import java.util.Stack; 86 import java.util.TreeMap; 87 import java.util.TreeSet; 88 89 import sun.util.logging.PlatformLogger; 90 91 import sun.awt.AppContext; 92 import sun.awt.SunToolkit; 93 94 import java.awt.image.BufferedImage; 95 import java.awt.image.ImageObserver; 96 import java.awt.image.RenderedImage; 97 import java.awt.image.WritableRaster; 98 import java.awt.image.ColorModel; 99 100 import javax.imageio.ImageIO; 101 import javax.imageio.ImageReader; 102 import javax.imageio.ImageReadParam; 103 import javax.imageio.ImageWriter; 104 import javax.imageio.ImageTypeSpecifier; 105 106 import javax.imageio.spi.ImageWriterSpi; 107 108 import javax.imageio.stream.ImageInputStream; 109 import javax.imageio.stream.ImageOutputStream; 110 111 import sun.awt.image.ImageRepresentation; 112 import sun.awt.image.ToolkitImage; 113 114 import java.io.FilePermission; 115 116 117 /** 118 * Provides a set of functions to be shared among the DataFlavor class and 119 * platform-specific data transfer implementations. 120 * 121 * The concept of "flavors" and "natives" is extended to include "formats", 122 * which are the numeric values Win32 and X11 use to express particular data 123 * types. Like FlavorMap, which provides getNativesForFlavors(DataFlavor[]) and 124 * getFlavorsForNatives(String[]) functions, DataTransferer provides a set 125 * of getFormatsFor(Transferable|Flavor|Flavors) and 126 * getFlavorsFor(Format|Formats) functions. 127 * 128 * Also provided are functions for translating a Transferable into a byte 129 * array, given a source DataFlavor and a target format, and for translating 130 * a byte array or InputStream into an Object, given a source format and 131 * a target DataFlavor. 132 * 133 * @author David Mendenhall 134 * @author Danila Sinopalnikov 135 * 136 * @since 1.3.1 137 */ 138 public abstract class DataTransferer { 139 140 /** 141 * Cached value of Class.forName("[C"); 142 */ 143 public static final Class charArrayClass; 144 145 /** 146 * Cached value of Class.forName("[B"); 147 */ 148 public static final Class byteArrayClass; 149 150 /** 151 * The <code>DataFlavor</code> representing plain text with Unicode 152 * encoding, where: 153 * <pre> 154 * representationClass = java.lang.String 155 * mimeType = "text/plain; charset=Unicode" 156 * </pre> 157 */ 158 public static final DataFlavor plainTextStringFlavor; 159 160 /** 161 * The <code>DataFlavor</code> representing a Java text encoding String 162 * encoded in UTF-8, where 163 * <pre> 164 * representationClass = [B 165 * mimeType = "application/x-java-text-encoding" 166 * </pre> 167 */ 168 public static final DataFlavor javaTextEncodingFlavor; 169 170 /** 171 * Lazy initialization of Standard Encodings. 172 */ 173 private static class StandardEncodingsHolder { 174 private static final SortedSet<String> standardEncodings = load(); 175 176 private static SortedSet<String> load() { 177 final Comparator comparator = 178 new CharsetComparator(IndexedComparator.SELECT_WORST); 179 final SortedSet<String> tempSet = new TreeSet<String>(comparator); 180 tempSet.add("US-ASCII"); 181 tempSet.add("ISO-8859-1"); 182 tempSet.add("UTF-8"); 183 tempSet.add("UTF-16BE"); 184 tempSet.add("UTF-16LE"); 185 tempSet.add("UTF-16"); 186 tempSet.add(getDefaultTextCharset()); 187 return Collections.unmodifiableSortedSet(tempSet); 188 } 189 } 190 191 /** 192 * Tracks whether a particular text/* MIME type supports the charset 193 * parameter. The Map is initialized with all of the standard MIME types 194 * listed in the DataFlavor.selectBestTextFlavor method comment. Additional 195 * entries may be added during the life of the JRE for text/<other> types. 196 */ 197 private static final Map textMIMESubtypeCharsetSupport; 198 199 /** 200 * Cache of the platform default encoding as specified in the 201 * "file.encoding" system property. 202 */ 203 private static String defaultEncoding; 204 205 /** 206 * A collection of all natives listed in flavormap.properties with 207 * a primary MIME type of "text". 208 */ 209 private static final Set textNatives = 210 Collections.synchronizedSet(new HashSet()); 211 212 /** 213 * The native encodings/charsets for the Set of textNatives. 214 */ 215 private static final Map nativeCharsets = 216 Collections.synchronizedMap(new HashMap()); 217 218 /** 219 * The end-of-line markers for the Set of textNatives. 220 */ 221 private static final Map nativeEOLNs = 222 Collections.synchronizedMap(new HashMap()); 223 224 /** 225 * The number of terminating NUL bytes for the Set of textNatives. 226 */ 227 private static final Map nativeTerminators = 228 Collections.synchronizedMap(new HashMap()); 229 230 /** 231 * The key used to store pending data conversion requests for an AppContext. 232 */ 233 private static final String DATA_CONVERTER_KEY = "DATA_CONVERTER_KEY"; 234 235 /** 236 * The singleton DataTransferer instance. It is created during MToolkit 237 * or WToolkit initialization. 238 */ 239 private static DataTransferer transferer; 240 241 private static final PlatformLogger dtLog = PlatformLogger.getLogger("sun.awt.datatransfer.DataTransfer"); 242 243 static { 244 Class tCharArrayClass = null, tByteArrayClass = null; 245 try { 246 tCharArrayClass = Class.forName("[C"); 247 tByteArrayClass = Class.forName("[B"); 248 } catch (ClassNotFoundException cannotHappen) { 249 } 250 charArrayClass = tCharArrayClass; 251 byteArrayClass = tByteArrayClass; 252 253 DataFlavor tPlainTextStringFlavor = null; 254 try { 255 tPlainTextStringFlavor = new DataFlavor 256 ("text/plain;charset=Unicode;class=java.lang.String"); 257 } catch (ClassNotFoundException cannotHappen) { 258 } 259 plainTextStringFlavor = tPlainTextStringFlavor; 260 261 DataFlavor tJavaTextEncodingFlavor = null; 262 try { 263 tJavaTextEncodingFlavor = new DataFlavor 264 ("application/x-java-text-encoding;class=\"[B\""); 265 } catch (ClassNotFoundException cannotHappen) { 266 } 267 javaTextEncodingFlavor = tJavaTextEncodingFlavor; 268 269 Map tempMap = new HashMap(17); 270 tempMap.put("sgml", Boolean.TRUE); 271 tempMap.put("xml", Boolean.TRUE); 272 tempMap.put("html", Boolean.TRUE); 273 tempMap.put("enriched", Boolean.TRUE); 274 tempMap.put("richtext", Boolean.TRUE); 275 tempMap.put("uri-list", Boolean.TRUE); 276 tempMap.put("directory", Boolean.TRUE); 277 tempMap.put("css", Boolean.TRUE); 278 tempMap.put("calendar", Boolean.TRUE); 279 tempMap.put("plain", Boolean.TRUE); 280 tempMap.put("rtf", Boolean.FALSE); 281 tempMap.put("tab-separated-values", Boolean.FALSE); 282 tempMap.put("t140", Boolean.FALSE); 283 tempMap.put("rfc822-headers", Boolean.FALSE); 284 tempMap.put("parityfec", Boolean.FALSE); 285 textMIMESubtypeCharsetSupport = Collections.synchronizedMap(tempMap); 286 } 287 288 /** 289 * The accessor method for the singleton DataTransferer instance. Note 290 * that in a headless environment, there may be no DataTransferer instance; 291 * instead, null will be returned. 292 */ 293 public static DataTransferer getInstance() { 294 synchronized (DataTransferer.class) { 295 if (transferer == null) { 296 final String name = SunToolkit.getDataTransfererClassName(); 297 if (name != null) { 298 PrivilegedAction<DataTransferer> action = new PrivilegedAction<DataTransferer>() 299 { 300 public DataTransferer run() { 301 Class cls = null; 302 Method method = null; 303 DataTransferer ret = null; 304 305 try { 306 cls = Class.forName(name); 307 } catch (ClassNotFoundException e) { 308 ClassLoader cl = ClassLoader. 309 getSystemClassLoader(); 310 if (cl != null) { 311 try { 312 cls = cl.loadClass(name); 313 } catch (ClassNotFoundException ee) { 314 ee.printStackTrace(); 315 throw new AWTError("DataTransferer not found: " + name); 316 } 317 } 318 } 319 if (cls != null) { 320 try { 321 method = cls.getDeclaredMethod("getInstanceImpl"); 322 method.setAccessible(true); 323 } catch (NoSuchMethodException e) { 324 e.printStackTrace(); 325 throw new AWTError("Cannot instantiate DataTransferer: " + name); 326 } catch (SecurityException e) { 327 e.printStackTrace(); 328 throw new AWTError("Access is denied for DataTransferer: " + name); 329 } 330 } 331 if (method != null) { 332 try { 333 ret = (DataTransferer) method.invoke(null); 334 } catch (InvocationTargetException e) { 335 e.printStackTrace(); 336 throw new AWTError("Cannot instantiate DataTransferer: " + name); 337 } catch (IllegalAccessException e) { 338 e.printStackTrace(); 339 throw new AWTError("Cannot access DataTransferer: " + name); 340 } 341 } 342 return ret; 343 } 344 }; 345 transferer = AccessController.doPrivileged(action); 346 } 347 } 348 } 349 return transferer; 350 } 351 352 /** 353 * Converts an arbitrary text encoding to its canonical name. 354 */ 355 public static String canonicalName(String encoding) { 356 if (encoding == null) { 357 return null; 358 } 359 try { 360 return Charset.forName(encoding).name(); 361 } catch (IllegalCharsetNameException icne) { 362 return encoding; 363 } catch (UnsupportedCharsetException uce) { 364 return encoding; 365 } 366 } 367 368 /** 369 * If the specified flavor is a text flavor which supports the "charset" 370 * parameter, then this method returns that parameter, or the default 371 * charset if no such parameter was specified at construction. For non- 372 * text DataFlavors, and for non-charset text flavors, this method returns 373 * null. 374 */ 375 public static String getTextCharset(DataFlavor flavor) { 376 if (!isFlavorCharsetTextType(flavor)) { 377 return null; 378 } 379 380 String encoding = flavor.getParameter("charset"); 381 382 return (encoding != null) ? encoding : getDefaultTextCharset(); 383 } 384 385 /** 386 * Returns the platform's default character encoding. 387 */ 388 public static String getDefaultTextCharset() { 389 if (defaultEncoding != null) { 390 return defaultEncoding; 391 } 392 return defaultEncoding = Charset.defaultCharset().name(); 393 } 394 395 /** 396 * Tests only whether the flavor's MIME type supports the charset 397 * parameter. Must only be called for flavors with a primary type of 398 * "text". 399 */ 400 public static boolean doesSubtypeSupportCharset(DataFlavor flavor) { 401 if (dtLog.isLoggable(PlatformLogger.Level.FINE)) { 402 if (!"text".equals(flavor.getPrimaryType())) { 403 dtLog.fine("Assertion (\"text\".equals(flavor.getPrimaryType())) failed"); 404 } 405 } 406 407 String subType = flavor.getSubType(); 408 if (subType == null) { 409 return false; 410 } 411 412 Object support = textMIMESubtypeCharsetSupport.get(subType); 413 414 if (support != null) { 415 return (support == Boolean.TRUE); 416 } 417 418 boolean ret_val = (flavor.getParameter("charset") != null); 419 textMIMESubtypeCharsetSupport.put 420 (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE); 421 return ret_val; 422 } 423 public static boolean doesSubtypeSupportCharset(String subType, 424 String charset) 425 { 426 Object support = textMIMESubtypeCharsetSupport.get(subType); 427 428 if (support != null) { 429 return (support == Boolean.TRUE); 430 } 431 432 boolean ret_val = (charset != null); 433 textMIMESubtypeCharsetSupport.put 434 (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE); 435 return ret_val; 436 } 437 438 /** 439 * Returns whether this flavor is a text type which supports the 440 * 'charset' parameter. 441 */ 442 public static boolean isFlavorCharsetTextType(DataFlavor flavor) { 443 // Although stringFlavor doesn't actually support the charset 444 // parameter (because its primary MIME type is not "text"), it should 445 // be treated as though it does. stringFlavor is semantically 446 // equivalent to "text/plain" data. 447 if (DataFlavor.stringFlavor.equals(flavor)) { 448 return true; 449 } 450 451 if (!"text".equals(flavor.getPrimaryType()) || 452 !doesSubtypeSupportCharset(flavor)) 453 { 454 return false; 455 } 456 457 Class rep_class = flavor.getRepresentationClass(); 458 459 if (flavor.isRepresentationClassReader() || 460 String.class.equals(rep_class) || 461 flavor.isRepresentationClassCharBuffer() || 462 DataTransferer.charArrayClass.equals(rep_class)) 463 { 464 return true; 465 } 466 467 if (!(flavor.isRepresentationClassInputStream() || 468 flavor.isRepresentationClassByteBuffer() || 469 DataTransferer.byteArrayClass.equals(rep_class))) { 470 return false; 471 } 472 473 String charset = flavor.getParameter("charset"); 474 475 return (charset != null) 476 ? DataTransferer.isEncodingSupported(charset) 477 : true; // null equals default encoding which is always supported 478 } 479 480 /** 481 * Returns whether this flavor is a text type which does not support the 482 * 'charset' parameter. 483 */ 484 public static boolean isFlavorNoncharsetTextType(DataFlavor flavor) { 485 if (!"text".equals(flavor.getPrimaryType()) || 486 doesSubtypeSupportCharset(flavor)) 487 { 488 return false; 489 } 490 491 return (flavor.isRepresentationClassInputStream() || 492 flavor.isRepresentationClassByteBuffer() || 493 DataTransferer.byteArrayClass. 494 equals(flavor.getRepresentationClass())); 495 } 496 497 /** 498 * Determines whether this JRE can both encode and decode text in the 499 * specified encoding. 500 */ 501 public static boolean isEncodingSupported(String encoding) { 502 if (encoding == null) { 503 return false; 504 } 505 try { 506 return Charset.isSupported(encoding); 507 } catch (IllegalCharsetNameException icne) { 508 return false; 509 } 510 } 511 512 /** 513 * Returns {@code true} if the given type is a java.rmi.Remote. 514 */ 515 public static boolean isRemote(Class<?> type) { 516 return RMI.isRemote(type); 517 } 518 519 /** 520 * Returns an Iterator which traverses a SortedSet of Strings which are 521 * a total order of the standard character sets supported by the JRE. The 522 * ordering follows the same principles as DataFlavor.selectBestTextFlavor. 523 * So as to avoid loading all available character converters, optional, 524 * non-standard, character sets are not included. 525 */ 526 public static Set <String> standardEncodings() { 527 return StandardEncodingsHolder.standardEncodings; 528 } 529 530 /** 531 * Converts a FlavorMap to a FlavorTable. 532 */ 533 public static FlavorTable adaptFlavorMap(final FlavorMap map) { 534 if (map instanceof FlavorTable) { 535 return (FlavorTable)map; 536 } 537 538 return new FlavorTable() { 539 public Map getNativesForFlavors(DataFlavor[] flavors) { 540 return map.getNativesForFlavors(flavors); 541 } 542 public Map getFlavorsForNatives(String[] natives) { 543 return map.getFlavorsForNatives(natives); 544 } 545 public List getNativesForFlavor(DataFlavor flav) { 546 Map natives = 547 getNativesForFlavors(new DataFlavor[] { flav } ); 548 String nat = (String)natives.get(flav); 549 if (nat != null) { 550 List list = new ArrayList(1); 551 list.add(nat); 552 return list; 553 } else { 554 return Collections.EMPTY_LIST; 555 } 556 } 557 public List getFlavorsForNative(String nat) { 558 Map flavors = 559 getFlavorsForNatives(new String[] { nat } ); 560 DataFlavor flavor = (DataFlavor)flavors.get(nat); 561 if (flavor != null) { 562 List list = new ArrayList(1); 563 list.add(flavor); 564 return list; 565 } else { 566 return Collections.EMPTY_LIST; 567 } 568 } 569 }; 570 } 571 572 /** 573 * Returns the default Unicode encoding for the platform. The encoding 574 * need not be canonical. This method is only used by the archaic function 575 * DataFlavor.getTextPlainUnicodeFlavor(). 576 */ 577 public abstract String getDefaultUnicodeEncoding(); 578 579 /** 580 * This method is called for text flavor mappings established while parsing 581 * the flavormap.properties file. It stores the "eoln" and "terminators" 582 * parameters which are not officially part of the MIME type. They are 583 * MIME parameters specific to the flavormap.properties file format. 584 */ 585 public void registerTextFlavorProperties(String nat, String charset, 586 String eoln, String terminators) { 587 Long format = getFormatForNativeAsLong(nat); 588 589 textNatives.add(format); 590 nativeCharsets.put(format, (charset != null && charset.length() != 0) 591 ? charset : getDefaultTextCharset()); 592 if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) { 593 nativeEOLNs.put(format, eoln); 594 } 595 if (terminators != null && terminators.length() != 0) { 596 Integer iTerminators = Integer.valueOf(terminators); 597 if (iTerminators.intValue() > 0) { 598 nativeTerminators.put(format, iTerminators); 599 } 600 } 601 } 602 603 /** 604 * Determines whether the native corresponding to the specified long format 605 * was listed in the flavormap.properties file. 606 */ 607 protected boolean isTextFormat(long format) { 608 return textNatives.contains(Long.valueOf(format)); 609 } 610 611 protected String getCharsetForTextFormat(Long lFormat) { 612 return (String)nativeCharsets.get(lFormat); 613 } 614 615 /** 616 * Specifies whether text imported from the native system in the specified 617 * format is locale-dependent. If so, when decoding such text, 618 * 'nativeCharsets' should be ignored, and instead, the Transferable should 619 * be queried for its javaTextEncodingFlavor data for the correct encoding. 620 */ 621 public abstract boolean isLocaleDependentTextFormat(long format); 622 623 /** 624 * Determines whether the DataFlavor corresponding to the specified long 625 * format is DataFlavor.javaFileListFlavor. 626 */ 627 public abstract boolean isFileFormat(long format); 628 629 /** 630 * Determines whether the DataFlavor corresponding to the specified long 631 * format is DataFlavor.imageFlavor. 632 */ 633 public abstract boolean isImageFormat(long format); 634 635 /** 636 * Determines whether the format is a URI list we can convert to 637 * a DataFlavor.javaFileListFlavor. 638 */ 639 protected boolean isURIListFormat(long format) { 640 return false; 641 } 642 643 /** 644 * Returns a Map whose keys are all of the possible formats into which the 645 * Transferable's transfer data flavors can be translated. The value of 646 * each key is the DataFlavor in which the Transferable's data should be 647 * requested when converting to the format. 648 * <p> 649 * The map keys are sorted according to the native formats preference 650 * order. 651 */ 652 public SortedMap<Long,DataFlavor> getFormatsForTransferable( 653 Transferable contents, FlavorTable map) 654 { 655 DataFlavor[] flavors = contents.getTransferDataFlavors(); 656 if (flavors == null) { 657 return new TreeMap(); 658 } 659 return getFormatsForFlavors(flavors, map); 660 } 661 662 /** 663 * Returns a Map whose keys are all of the possible formats into which data 664 * in the specified DataFlavor can be translated. The value of each key 665 * is the DataFlavor in which a Transferable's data should be requested 666 * when converting to the format. 667 * <p> 668 * The map keys are sorted according to the native formats preference 669 * order. 670 */ 671 public SortedMap getFormatsForFlavor(DataFlavor flavor, FlavorTable map) { 672 return getFormatsForFlavors(new DataFlavor[] { flavor }, 673 map); 674 } 675 676 /** 677 * Returns a Map whose keys are all of the possible formats into which data 678 * in the specified DataFlavors can be translated. The value of each key 679 * is the DataFlavor in which the Transferable's data should be requested 680 * when converting to the format. 681 * <p> 682 * The map keys are sorted according to the native formats preference 683 * order. 684 * 685 * @param flavors the data flavors 686 * @param map the FlavorTable which contains mappings between 687 * DataFlavors and data formats 688 * @throws NullPointerException if flavors or map is <code>null</code> 689 */ 690 public SortedMap <Long, DataFlavor> getFormatsForFlavors( 691 DataFlavor[] flavors, FlavorTable map) 692 { 693 Map <Long,DataFlavor> formatMap = 694 new HashMap <> (flavors.length); 695 Map <Long,DataFlavor> textPlainMap = 696 new HashMap <> (flavors.length); 697 // Maps formats to indices that will be used to sort the formats 698 // according to the preference order. 699 // Larger index value corresponds to the more preferable format. 700 Map indexMap = new HashMap(flavors.length); 701 Map textPlainIndexMap = new HashMap(flavors.length); 702 703 int currentIndex = 0; 704 705 // Iterate backwards so that preferred DataFlavors are used over 706 // other DataFlavors. (See javadoc for 707 // Transferable.getTransferDataFlavors.) 708 for (int i = flavors.length - 1; i >= 0; i--) { 709 DataFlavor flavor = flavors[i]; 710 if (flavor == null) continue; 711 712 // Don't explicitly test for String, since it is just a special 713 // case of Serializable 714 if (flavor.isFlavorTextType() || 715 flavor.isFlavorJavaFileListType() || 716 DataFlavor.imageFlavor.equals(flavor) || 717 flavor.isRepresentationClassSerializable() || 718 flavor.isRepresentationClassInputStream() || 719 flavor.isRepresentationClassRemote()) 720 { 721 List natives = map.getNativesForFlavor(flavor); 722 723 currentIndex += natives.size(); 724 725 for (Iterator iter = natives.iterator(); iter.hasNext(); ) { 726 Long lFormat = 727 getFormatForNativeAsLong((String)iter.next()); 728 Integer index = Integer.valueOf(currentIndex--); 729 730 formatMap.put(lFormat, flavor); 731 indexMap.put(lFormat, index); 732 733 // SystemFlavorMap.getNativesForFlavor will return 734 // text/plain natives for all text/*. While this is good 735 // for a single text/* flavor, we would prefer that 736 // text/plain native data come from a text/plain flavor. 737 if (("text".equals(flavor.getPrimaryType()) && 738 "plain".equals(flavor.getSubType())) || 739 flavor.equals(DataFlavor.stringFlavor)) 740 { 741 textPlainMap.put(lFormat, flavor); 742 textPlainIndexMap.put(lFormat, index); 743 } 744 } 745 746 currentIndex += natives.size(); 747 } 748 } 749 750 formatMap.putAll(textPlainMap); 751 indexMap.putAll(textPlainIndexMap); 752 753 // Sort the map keys according to the formats preference order. 754 Comparator comparator = 755 new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST); 756 SortedMap sortedMap = new TreeMap(comparator); 757 sortedMap.putAll(formatMap); 758 759 return sortedMap; 760 } 761 762 /** 763 * Reduces the Map output for the root function to an array of the 764 * Map's keys. 765 */ 766 public long[] getFormatsForTransferableAsArray(Transferable contents, 767 FlavorTable map) { 768 return keysToLongArray(getFormatsForTransferable(contents, map)); 769 } 770 public long[] getFormatsForFlavorAsArray(DataFlavor flavor, 771 FlavorTable map) { 772 return keysToLongArray(getFormatsForFlavor(flavor, map)); 773 } 774 public long[] getFormatsForFlavorsAsArray(DataFlavor[] flavors, 775 FlavorTable map) { 776 return keysToLongArray(getFormatsForFlavors(flavors, map)); 777 } 778 779 /** 780 * Returns a Map whose keys are all of the possible DataFlavors into which 781 * data in the specified format can be translated. The value of each key 782 * is the format in which the Clipboard or dropped data should be requested 783 * when converting to the DataFlavor. 784 */ 785 public Map getFlavorsForFormat(long format, FlavorTable map) { 786 return getFlavorsForFormats(new long[] { format }, map); 787 } 788 789 /** 790 * Returns a Map whose keys are all of the possible DataFlavors into which 791 * data in the specified formats can be translated. The value of each key 792 * is the format in which the Clipboard or dropped data should be requested 793 * when converting to the DataFlavor. 794 */ 795 public Map getFlavorsForFormats(long[] formats, FlavorTable map) { 796 Map flavorMap = new HashMap(formats.length); 797 Set mappingSet = new HashSet(formats.length); 798 Set flavorSet = new HashSet(formats.length); 799 800 // First step: build flavorSet, mappingSet and initial flavorMap 801 // flavorSet - the set of all the DataFlavors into which 802 // data in the specified formats can be translated; 803 // mappingSet - the set of all the mappings from the specified formats 804 // into any DataFlavor; 805 // flavorMap - after this step, this map maps each of the DataFlavors 806 // from flavorSet to any of the specified formats. 807 for (int i = 0; i < formats.length; i++) { 808 long format = formats[i]; 809 String nat = getNativeForFormat(format); 810 List flavors = map.getFlavorsForNative(nat); 811 812 for (Iterator iter = flavors.iterator(); iter.hasNext(); ) { 813 DataFlavor flavor = (DataFlavor)iter.next(); 814 815 // Don't explicitly test for String, since it is just a special 816 // case of Serializable 817 if (flavor.isFlavorTextType() || 818 flavor.isFlavorJavaFileListType() || 819 DataFlavor.imageFlavor.equals(flavor) || 820 flavor.isRepresentationClassSerializable() || 821 flavor.isRepresentationClassInputStream() || 822 flavor.isRepresentationClassRemote()) 823 { 824 Long lFormat = Long.valueOf(format); 825 Object mapping = 826 DataTransferer.createMapping(lFormat, flavor); 827 flavorMap.put(flavor, lFormat); 828 mappingSet.add(mapping); 829 flavorSet.add(flavor); 830 } 831 } 832 } 833 834 // Second step: for each DataFlavor try to figure out which of the 835 // specified formats is the best to translate to this flavor. 836 // Then map each flavor to the best format. 837 // For the given flavor, FlavorTable indicates which native will 838 // best reflect data in the specified flavor to the underlying native 839 // platform. We assume that this native is the best to translate 840 // to this flavor. 841 // Note: FlavorTable allows one-way mappings, so we can occasionally 842 // map a flavor to the format for which the corresponding 843 // format-to-flavor mapping doesn't exist. For this reason we have built 844 // a mappingSet of all format-to-flavor mappings for the specified formats 845 // and check if the format-to-flavor mapping exists for the 846 // (flavor,format) pair being added. 847 for (Iterator flavorIter = flavorSet.iterator(); 848 flavorIter.hasNext(); ) { 849 DataFlavor flavor = (DataFlavor)flavorIter.next(); 850 851 List natives = map.getNativesForFlavor(flavor); 852 853 for (Iterator nativeIter = natives.iterator(); 854 nativeIter.hasNext(); ) { 855 Long lFormat = 856 getFormatForNativeAsLong((String)nativeIter.next()); 857 Object mapping = DataTransferer.createMapping(lFormat, flavor); 858 859 if (mappingSet.contains(mapping)) { 860 flavorMap.put(flavor, lFormat); 861 break; 862 } 863 } 864 } 865 866 return flavorMap; 867 } 868 869 /** 870 * Returns a Set of all DataFlavors for which 871 * 1) a mapping from at least one of the specified formats exists in the 872 * specified map and 873 * 2) the data translation for this mapping can be performed by the data 874 * transfer subsystem. 875 * 876 * @param formats the data formats 877 * @param map the FlavorTable which contains mappings between 878 * DataFlavors and data formats 879 * @throws NullPointerException if formats or map is <code>null</code> 880 */ 881 public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) { 882 Set flavorSet = new HashSet(formats.length); 883 884 for (int i = 0; i < formats.length; i++) { 885 String nat = getNativeForFormat(formats[i]); 886 List flavors = map.getFlavorsForNative(nat); 887 888 for (Iterator iter = flavors.iterator(); iter.hasNext(); ) { 889 DataFlavor flavor = (DataFlavor)iter.next(); 890 891 // Don't explicitly test for String, since it is just a special 892 // case of Serializable 893 if (flavor.isFlavorTextType() || 894 flavor.isFlavorJavaFileListType() || 895 DataFlavor.imageFlavor.equals(flavor) || 896 flavor.isRepresentationClassSerializable() || 897 flavor.isRepresentationClassInputStream() || 898 flavor.isRepresentationClassRemote()) 899 { 900 flavorSet.add(flavor); 901 } 902 } 903 } 904 905 return flavorSet; 906 } 907 908 /** 909 * Returns an array of all DataFlavors for which 910 * 1) a mapping from the specified format exists in the specified map and 911 * 2) the data translation for this mapping can be performed by the data 912 * transfer subsystem. 913 * The array will be sorted according to a 914 * <code>DataFlavorComparator</code> created with the specified 915 * map as an argument. 916 * 917 * @param format the data format 918 * @param map the FlavorTable which contains mappings between 919 * DataFlavors and data formats 920 * @throws NullPointerException if map is <code>null</code> 921 */ 922 public DataFlavor[] getFlavorsForFormatAsArray(long format, 923 FlavorTable map) { 924 return getFlavorsForFormatsAsArray(new long[] { format }, map); 925 } 926 927 /** 928 * Returns an array of all DataFlavors for which 929 * 1) a mapping from at least one of the specified formats exists in the 930 * specified map and 931 * 2) the data translation for this mapping can be performed by the data 932 * transfer subsystem. 933 * The array will be sorted according to a 934 * <code>DataFlavorComparator</code> created with the specified 935 * map as an argument. 936 * 937 * @param formats the data formats 938 * @param map the FlavorTable which contains mappings between 939 * DataFlavors and data formats 940 * @throws NullPointerException if formats or map is <code>null</code> 941 */ 942 public DataFlavor[] getFlavorsForFormatsAsArray(long[] formats, 943 FlavorTable map) { 944 // getFlavorsForFormatsAsSet() is less expensive than 945 // getFlavorsForFormats(). 946 return setToSortedDataFlavorArray(getFlavorsForFormatsAsSet(formats, map)); 947 } 948 949 /** 950 * Returns an object that represents a mapping between the specified 951 * key and value. <tt>null</tt> values and the <tt>null</tt> keys are 952 * permitted. The internal representation of the mapping object is 953 * irrelevant. The only requrement is that the two mapping objects are equal 954 * if and only if their keys are equal and their values are equal. 955 * More formally, the two mapping objects are equal if and only if 956 * <tt>(value1 == null ? value2 == null : value1.equals(value2)) 957 * && (key1 == null ? key2 == null : key1.equals(key2))</tt>. 958 */ 959 private static Object createMapping(Object key, Object value) { 960 // NOTE: Should be updated to use AbstractMap.SimpleEntry as 961 // soon as it is made public. 962 return Arrays.asList(new Object[] { key, value }); 963 } 964 965 /** 966 * Looks-up or registers the String native with the native data transfer 967 * system and returns a long format corresponding to that native. 968 */ 969 protected abstract Long getFormatForNativeAsLong(String str); 970 971 /** 972 * Looks-up the String native corresponding to the specified long format in 973 * the native data transfer system. 974 */ 975 protected abstract String getNativeForFormat(long format); 976 977 /* Contains common code for finding the best charset for 978 * clipboard string encoding/decoding, basing on clipboard 979 * format and localeTransferable(on decoding, if available) 980 */ 981 private String getBestCharsetForTextFormat(Long lFormat, 982 Transferable localeTransferable) throws IOException 983 { 984 String charset = null; 985 if (localeTransferable != null && 986 isLocaleDependentTextFormat(lFormat) && 987 localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) 988 { 989 try { 990 charset = new String( 991 (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor), 992 "UTF-8" 993 ); 994 } catch (UnsupportedFlavorException cannotHappen) { 995 } 996 } else { 997 charset = getCharsetForTextFormat(lFormat); 998 } 999 if (charset == null) { 1000 // Only happens when we have a custom text type. 1001 charset = getDefaultTextCharset(); 1002 } 1003 return charset; 1004 } 1005 1006 /** 1007 * Translation function for converting string into 1008 * a byte array. Search-and-replace EOLN. Encode into the 1009 * target format. Append terminating NUL bytes. 1010 * 1011 * Java to Native string conversion 1012 */ 1013 private byte[] translateTransferableString(String str, 1014 long format) throws IOException 1015 { 1016 Long lFormat = Long.valueOf(format); 1017 String charset = getBestCharsetForTextFormat(lFormat, null); 1018 // Search and replace EOLN. Note that if EOLN is "\n", then we 1019 // never added an entry to nativeEOLNs anyway, so we'll skip this 1020 // code altogether. 1021 // windows: "abc\nde"->"abc\r\nde" 1022 String eoln = (String)nativeEOLNs.get(lFormat); 1023 if (eoln != null) { 1024 int length = str.length(); 1025 StringBuffer buffer = 1026 new StringBuffer(length * 2); // 2 is a heuristic 1027 for (int i = 0; i < length; i++) { 1028 // Fix for 4914613 - skip native EOLN 1029 if (str.startsWith(eoln, i)) { 1030 buffer.append(eoln); 1031 i += eoln.length() - 1; 1032 continue; 1033 } 1034 char c = str.charAt(i); 1035 if (c == '\n') { 1036 buffer.append(eoln); 1037 } else { 1038 buffer.append(c); 1039 } 1040 } 1041 str = buffer.toString(); 1042 } 1043 1044 // Encode text in target format. 1045 byte[] bytes = str.getBytes(charset); 1046 1047 // Append terminating NUL bytes. Note that if terminators is 0, 1048 // the we never added an entry to nativeTerminators anyway, so 1049 // we'll skip code altogether. 1050 // "abcde" -> "abcde\0" 1051 Integer terminators = (Integer)nativeTerminators.get(lFormat); 1052 if (terminators != null) { 1053 int numTerminators = terminators.intValue(); 1054 byte[] terminatedBytes = 1055 new byte[bytes.length + numTerminators]; 1056 System.arraycopy(bytes, 0, terminatedBytes, 0, bytes.length); 1057 for (int i = bytes.length; i < terminatedBytes.length; i++) { 1058 terminatedBytes[i] = 0x0; 1059 } 1060 bytes = terminatedBytes; 1061 } 1062 return bytes; 1063 } 1064 1065 /** 1066 * Translating either a byte array or an InputStream into an String. 1067 * Strip terminators and search-and-replace EOLN. 1068 * 1069 * Native to Java string conversion 1070 */ 1071 private String translateBytesToString(byte[] bytes, long format, 1072 Transferable localeTransferable) 1073 throws IOException 1074 { 1075 1076 Long lFormat = Long.valueOf(format); 1077 String charset = getBestCharsetForTextFormat(lFormat, localeTransferable); 1078 1079 // Locate terminating NUL bytes. Note that if terminators is 0, 1080 // the we never added an entry to nativeTerminators anyway, so 1081 // we'll skip code altogether. 1082 1083 // In other words: we are doing char alignment here basing on suggestion 1084 // that count of zero-'terminators' is a number of bytes in one symbol 1085 // for selected charset (clipboard format). It is not complitly true for 1086 // multibyte coding like UTF-8, but helps understand the procedure. 1087 // "abcde\0" -> "abcde" 1088 1089 String eoln = (String)nativeEOLNs.get(lFormat); 1090 Integer terminators = (Integer)nativeTerminators.get(lFormat); 1091 int count; 1092 if (terminators != null) { 1093 int numTerminators = terminators.intValue(); 1094 search: 1095 for (count = 0; count < (bytes.length - numTerminators + 1); count += numTerminators) { 1096 for (int i = count; i < count + numTerminators; i++) { 1097 if (bytes[i] != 0x0) { 1098 continue search; 1099 } 1100 } 1101 // found terminators 1102 break search; 1103 } 1104 } else { 1105 count = bytes.length; 1106 } 1107 1108 // Decode text to chars. Don't include any terminators. 1109 String converted = new String(bytes, 0, count, charset); 1110 1111 // Search and replace EOLN. Note that if EOLN is "\n", then we 1112 // never added an entry to nativeEOLNs anyway, so we'll skip this 1113 // code altogether. 1114 // Count of NUL-terminators and EOLN coding are platform-specific and 1115 // loaded from flavormap.properties file 1116 // windows: "abc\r\nde" -> "abc\nde" 1117 1118 if (eoln != null) { 1119 1120 /* Fix for 4463560: replace EOLNs symbol-by-symbol instead 1121 * of using buf.replace() 1122 */ 1123 1124 char[] buf = converted.toCharArray(); 1125 char[] eoln_arr = eoln.toCharArray(); 1126 converted = null; 1127 int j = 0; 1128 boolean match; 1129 1130 for (int i = 0; i < buf.length; ) { 1131 // Catch last few bytes 1132 if (i + eoln_arr.length > buf.length) { 1133 buf[j++] = buf[i++]; 1134 continue; 1135 } 1136 1137 match = true; 1138 for (int k = 0, l = i; k < eoln_arr.length; k++, l++) { 1139 if (eoln_arr[k] != buf[l]) { 1140 match = false; 1141 break; 1142 } 1143 } 1144 if (match) { 1145 buf[j++] = '\n'; 1146 i += eoln_arr.length; 1147 } else { 1148 buf[j++] = buf[i++]; 1149 } 1150 } 1151 converted = new String(buf, 0, j); 1152 } 1153 1154 return converted; 1155 } 1156 1157 1158 /** 1159 * Primary translation function for translating a Transferable into 1160 * a byte array, given a source DataFlavor and target format. 1161 */ 1162 public byte[] translateTransferable(Transferable contents, 1163 DataFlavor flavor, 1164 long format) throws IOException 1165 { 1166 // Obtain the transfer data in the source DataFlavor. 1167 // 1168 // Note that we special case DataFlavor.plainTextFlavor because 1169 // StringSelection supports this flavor incorrectly -- instead of 1170 // returning an InputStream as the DataFlavor representation class 1171 // states, it returns a Reader. Instead of using this broken 1172 // functionality, we request the data in stringFlavor (the other 1173 // DataFlavor which StringSelection supports) and use the String 1174 // translator. 1175 Object obj; 1176 boolean stringSelectionHack; 1177 try { 1178 obj = contents.getTransferData(flavor); 1179 if (obj == null) { 1180 return null; 1181 } 1182 if (flavor.equals(DataFlavor.plainTextFlavor) && 1183 !(obj instanceof InputStream)) 1184 { 1185 obj = contents.getTransferData(DataFlavor.stringFlavor); 1186 if (obj == null) { 1187 return null; 1188 } 1189 stringSelectionHack = true; 1190 } else { 1191 stringSelectionHack = false; 1192 } 1193 } catch (UnsupportedFlavorException e) { 1194 throw new IOException(e.getMessage()); 1195 } 1196 1197 // Source data is a String. Search-and-replace EOLN. Encode into the 1198 // target format. Append terminating NUL bytes. 1199 if (stringSelectionHack || 1200 (String.class.equals(flavor.getRepresentationClass()) && 1201 isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1202 1203 String str = removeSuspectedData(flavor, contents, (String)obj); 1204 1205 return translateTransferableString( 1206 str, 1207 format); 1208 1209 // Source data is a Reader. Convert to a String and recur. In the 1210 // future, we may want to rewrite this so that we encode on demand. 1211 } else if (flavor.isRepresentationClassReader()) { 1212 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1213 throw new IOException 1214 ("cannot transfer non-text data as Reader"); 1215 } 1216 1217 StringBuffer buf = new StringBuffer(); 1218 try (Reader r = (Reader)obj) { 1219 int c; 1220 while ((c = r.read()) != -1) { 1221 buf.append((char)c); 1222 } 1223 } 1224 1225 return translateTransferableString( 1226 buf.toString(), 1227 format); 1228 1229 // Source data is a CharBuffer. Convert to a String and recur. 1230 } else if (flavor.isRepresentationClassCharBuffer()) { 1231 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1232 throw new IOException 1233 ("cannot transfer non-text data as CharBuffer"); 1234 } 1235 1236 CharBuffer buffer = (CharBuffer)obj; 1237 int size = buffer.remaining(); 1238 char[] chars = new char[size]; 1239 buffer.get(chars, 0, size); 1240 1241 return translateTransferableString( 1242 new String(chars), 1243 format); 1244 1245 // Source data is a char array. Convert to a String and recur. 1246 } else if (charArrayClass.equals(flavor.getRepresentationClass())) { 1247 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1248 throw new IOException 1249 ("cannot transfer non-text data as char array"); 1250 } 1251 1252 return translateTransferableString( 1253 new String((char[])obj), 1254 format); 1255 1256 // Source data is a ByteBuffer. For arbitrary flavors, simply return 1257 // the array. For text flavors, decode back to a String and recur to 1258 // reencode according to the requested format. 1259 } else if (flavor.isRepresentationClassByteBuffer()) { 1260 ByteBuffer buffer = (ByteBuffer)obj; 1261 int size = buffer.remaining(); 1262 byte[] bytes = new byte[size]; 1263 buffer.get(bytes, 0, size); 1264 1265 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1266 String sourceEncoding = DataTransferer.getTextCharset(flavor); 1267 return translateTransferableString( 1268 new String(bytes, sourceEncoding), 1269 format); 1270 } else { 1271 return bytes; 1272 } 1273 1274 // Source data is a byte array. For arbitrary flavors, simply return 1275 // the array. For text flavors, decode back to a String and recur to 1276 // reencode according to the requested format. 1277 } else if (byteArrayClass.equals(flavor.getRepresentationClass())) { 1278 byte[] bytes = (byte[])obj; 1279 1280 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1281 String sourceEncoding = DataTransferer.getTextCharset(flavor); 1282 return translateTransferableString( 1283 new String(bytes, sourceEncoding), 1284 format); 1285 } else { 1286 return bytes; 1287 } 1288 // Source data is Image 1289 } else if (DataFlavor.imageFlavor.equals(flavor)) { 1290 if (!isImageFormat(format)) { 1291 throw new IOException("Data translation failed: " + 1292 "not an image format"); 1293 } 1294 1295 Image image = (Image)obj; 1296 byte[] bytes = imageToPlatformBytes(image, format); 1297 1298 if (bytes == null) { 1299 throw new IOException("Data translation failed: " + 1300 "cannot convert java image to native format"); 1301 } 1302 return bytes; 1303 } 1304 1305 byte[] theByteArray = null; 1306 1307 // Target data is a file list. Source data must be a 1308 // java.util.List which contains java.io.File or String instances. 1309 if (isFileFormat(format)) { 1310 if (!DataFlavor.javaFileListFlavor.equals(flavor)) { 1311 throw new IOException("data translation failed"); 1312 } 1313 1314 final List list = (List)obj; 1315 1316 final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents); 1317 1318 final ArrayList<String> fileList = castToFiles(list, userProtectionDomain); 1319 1320 try (ByteArrayOutputStream bos = convertFileListToBytes(fileList)) { 1321 theByteArray = bos.toByteArray(); 1322 } 1323 1324 // Target data is a URI list. Source data must be a 1325 // java.util.List which contains java.io.File or String instances. 1326 } else if (isURIListFormat(format)) { 1327 if (!DataFlavor.javaFileListFlavor.equals(flavor)) { 1328 throw new IOException("data translation failed"); 1329 } 1330 String nat = getNativeForFormat(format); 1331 String targetCharset = null; 1332 if (nat != null) { 1333 try { 1334 targetCharset = new DataFlavor(nat).getParameter("charset"); 1335 } catch (ClassNotFoundException cnfe) { 1336 throw new IOException(cnfe); 1337 } 1338 } 1339 if (targetCharset == null) { 1340 targetCharset = "UTF-8"; 1341 } 1342 final List list = (List)obj; 1343 final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents); 1344 final ArrayList<String> fileList = castToFiles(list, userProtectionDomain); 1345 final ArrayList<String> uriList = new ArrayList<String>(fileList.size()); 1346 for (String fileObject : fileList) { 1347 final URI uri = new File(fileObject).toURI(); 1348 // Some implementations are fussy about the number of slashes (file:///path/to/file is best) 1349 try { 1350 uriList.add(new URI(uri.getScheme(), "", uri.getPath(), uri.getFragment()).toString()); 1351 } catch (URISyntaxException uriSyntaxException) { 1352 throw new IOException(uriSyntaxException); 1353 } 1354 } 1355 1356 byte[] eoln = "\r\n".getBytes(targetCharset); 1357 1358 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { 1359 for (int i = 0; i < uriList.size(); i++) { 1360 byte[] bytes = uriList.get(i).getBytes(targetCharset); 1361 bos.write(bytes, 0, bytes.length); 1362 bos.write(eoln, 0, eoln.length); 1363 } 1364 theByteArray = bos.toByteArray(); 1365 } 1366 1367 // Source data is an InputStream. For arbitrary flavors, just grab the 1368 // bytes and dump them into a byte array. For text flavors, decode back 1369 // to a String and recur to reencode according to the requested format. 1370 } else if (flavor.isRepresentationClassInputStream()) { 1371 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { 1372 try (InputStream is = (InputStream)obj) { 1373 boolean eof = false; 1374 int avail = is.available(); 1375 byte[] tmp = new byte[avail > 8192 ? avail : 8192]; 1376 do { 1377 int aValue; 1378 if (!(eof = (aValue = is.read(tmp, 0, tmp.length)) == -1)) { 1379 bos.write(tmp, 0, aValue); 1380 } 1381 } while (!eof); 1382 } 1383 1384 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1385 byte[] bytes = bos.toByteArray(); 1386 String sourceEncoding = DataTransferer.getTextCharset(flavor); 1387 return translateTransferableString( 1388 new String(bytes, sourceEncoding), 1389 format); 1390 } 1391 theByteArray = bos.toByteArray(); 1392 } 1393 1394 1395 1396 // Source data is an RMI object 1397 } else if (flavor.isRepresentationClassRemote()) { 1398 1399 Object mo = RMI.newMarshalledObject(obj); 1400 theByteArray = convertObjectToBytes(mo); 1401 1402 // Source data is Serializable 1403 } else if (flavor.isRepresentationClassSerializable()) { 1404 1405 theByteArray = convertObjectToBytes(obj); 1406 1407 } else { 1408 throw new IOException("data translation failed"); 1409 } 1410 1411 1412 1413 return theByteArray; 1414 } 1415 1416 private static byte[] convertObjectToBytes(Object object) throws IOException { 1417 try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1418 ObjectOutputStream oos = new ObjectOutputStream(bos)) 1419 { 1420 oos.writeObject(object); 1421 return bos.toByteArray(); 1422 } 1423 } 1424 1425 protected abstract ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException; 1426 1427 private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str) 1428 throws IOException 1429 { 1430 if (null == System.getSecurityManager() 1431 || !flavor.isMimeTypeEqual("text/uri-list")) 1432 { 1433 return str; 1434 } 1435 1436 1437 String ret_val = ""; 1438 final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents); 1439 1440 try { 1441 ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() { 1442 public Object run() { 1443 1444 StringBuffer allowedFiles = new StringBuffer(str.length()); 1445 String [] uriArray = str.split("(\\s)+"); 1446 1447 for (String fileName : uriArray) 1448 { 1449 File file = new File(fileName); 1450 if (file.exists() && 1451 !(isFileInWebstartedCache(file) || 1452 isForbiddenToRead(file, userProtectionDomain))) 1453 { 1454 1455 if (0 != allowedFiles.length()) 1456 { 1457 allowedFiles.append("\\r\\n"); 1458 } 1459 1460 allowedFiles.append(fileName); 1461 } 1462 } 1463 1464 return allowedFiles.toString(); 1465 } 1466 }); 1467 } catch (PrivilegedActionException pae) { 1468 throw new IOException(pae.getMessage(), pae); 1469 } 1470 1471 return ret_val; 1472 } 1473 1474 private static ProtectionDomain getUserProtectionDomain(Transferable contents) { 1475 return contents.getClass().getProtectionDomain(); 1476 } 1477 1478 private boolean isForbiddenToRead (File file, ProtectionDomain protectionDomain) 1479 { 1480 if (null == protectionDomain) { 1481 return false; 1482 } 1483 try { 1484 FilePermission filePermission = 1485 new FilePermission(file.getCanonicalPath(), "read, delete"); 1486 if (protectionDomain.implies(filePermission)) { 1487 return false; 1488 } 1489 } catch (IOException e) {} 1490 1491 return true; 1492 } 1493 1494 private ArrayList<String> castToFiles(final List files, 1495 final ProtectionDomain userProtectionDomain) throws IOException 1496 { 1497 final ArrayList<String> fileList = new ArrayList<String>(); 1498 try { 1499 AccessController.doPrivileged(new PrivilegedExceptionAction() { 1500 public Object run() throws IOException { 1501 for (Object fileObject : files) 1502 { 1503 File file = castToFile(fileObject); 1504 if (file != null && 1505 (null == System.getSecurityManager() || 1506 !(isFileInWebstartedCache(file) || 1507 isForbiddenToRead(file, userProtectionDomain)))) 1508 { 1509 fileList.add(file.getCanonicalPath()); 1510 } 1511 } 1512 return null; 1513 } 1514 }); 1515 } catch (PrivilegedActionException pae) { 1516 throw new IOException(pae.getMessage()); 1517 } 1518 return fileList; 1519 } 1520 1521 // It is important do not use user's successors 1522 // of File class. 1523 private File castToFile(Object fileObject) throws IOException { 1524 String filePath = null; 1525 if (fileObject instanceof File) { 1526 filePath = ((File)fileObject).getCanonicalPath(); 1527 } else if (fileObject instanceof String) { 1528 filePath = (String) fileObject; 1529 } else { 1530 return null; 1531 } 1532 return new File(filePath); 1533 } 1534 1535 private final static String[] DEPLOYMENT_CACHE_PROPERTIES = { 1536 "deployment.system.cachedir", 1537 "deployment.user.cachedir", 1538 "deployment.javaws.cachedir", 1539 "deployment.javapi.cachedir" 1540 }; 1541 1542 private final static ArrayList <File> deploymentCacheDirectoryList = 1543 new ArrayList<File>(); 1544 1545 private static boolean isFileInWebstartedCache(File f) { 1546 1547 if (deploymentCacheDirectoryList.isEmpty()) { 1548 for (String cacheDirectoryProperty : DEPLOYMENT_CACHE_PROPERTIES) { 1549 String cacheDirectoryPath = System.getProperty(cacheDirectoryProperty); 1550 if (cacheDirectoryPath != null) { 1551 try { 1552 File cacheDirectory = (new File(cacheDirectoryPath)).getCanonicalFile(); 1553 if (cacheDirectory != null) { 1554 deploymentCacheDirectoryList.add(cacheDirectory); 1555 } 1556 } catch (IOException ioe) {} 1557 } 1558 } 1559 } 1560 1561 for (File deploymentCacheDirectory : deploymentCacheDirectoryList) { 1562 for (File dir = f; dir != null; dir = dir.getParentFile()) { 1563 if (dir.equals(deploymentCacheDirectory)) { 1564 return true; 1565 } 1566 } 1567 } 1568 1569 return false; 1570 } 1571 1572 1573 public Object translateBytes(byte[] bytes, DataFlavor flavor, 1574 long format, Transferable localeTransferable) 1575 throws IOException 1576 { 1577 1578 Object theObject = null; 1579 1580 // Source data is a file list. Use the dragQueryFile native function to 1581 // do most of the decoding. Then wrap File objects around the String 1582 // filenames and return a List. 1583 if (isFileFormat(format)) { 1584 if (!DataFlavor.javaFileListFlavor.equals(flavor)) { 1585 throw new IOException("data translation failed"); 1586 } 1587 String[] filenames = dragQueryFile(bytes); 1588 if (filenames == null) { 1589 return null; 1590 } 1591 1592 // Convert the strings to File objects 1593 File[] files = new File[filenames.length]; 1594 for (int i = 0; i < filenames.length; i++) { 1595 files[i] = new File(filenames[i]); 1596 } 1597 1598 // Turn the list of Files into a List and return 1599 theObject = Arrays.asList(files); 1600 1601 // Target data is a String. Strip terminating NUL bytes. Decode bytes 1602 // into characters. Search-and-replace EOLN. 1603 } else if (String.class.equals(flavor.getRepresentationClass()) && 1604 isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1605 1606 theObject = translateBytesToString(bytes, format, localeTransferable); 1607 1608 // Target data is a Reader. Obtain data in InputStream format, encoded 1609 // as "Unicode" (utf-16be). Then use an InputStreamReader to decode 1610 // back to chars on demand. 1611 } else if (flavor.isRepresentationClassReader()) { 1612 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) { 1613 theObject = translateStream(bais, 1614 flavor, format, localeTransferable); 1615 } 1616 // Target data is a CharBuffer. Recur to obtain String and wrap. 1617 } else if (flavor.isRepresentationClassCharBuffer()) { 1618 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1619 throw new IOException 1620 ("cannot transfer non-text data as CharBuffer"); 1621 } 1622 1623 CharBuffer buffer = CharBuffer.wrap( 1624 translateBytesToString(bytes,format, localeTransferable)); 1625 1626 theObject = constructFlavoredObject(buffer, flavor, CharBuffer.class); 1627 1628 // Target data is a char array. Recur to obtain String and convert to 1629 // char array. 1630 } else if (charArrayClass.equals(flavor.getRepresentationClass())) { 1631 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1632 throw new IOException 1633 ("cannot transfer non-text data as char array"); 1634 } 1635 1636 theObject = translateBytesToString( 1637 bytes, format, localeTransferable).toCharArray(); 1638 1639 // Target data is a ByteBuffer. For arbitrary flavors, just return 1640 // the raw bytes. For text flavors, convert to a String to strip 1641 // terminators and search-and-replace EOLN, then reencode according to 1642 // the requested flavor. 1643 } else if (flavor.isRepresentationClassByteBuffer()) { 1644 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1645 bytes = translateBytesToString( 1646 bytes, format, localeTransferable).getBytes( 1647 DataTransferer.getTextCharset(flavor) 1648 ); 1649 } 1650 1651 ByteBuffer buffer = ByteBuffer.wrap(bytes); 1652 theObject = constructFlavoredObject(buffer, flavor, ByteBuffer.class); 1653 1654 // Target data is a byte array. For arbitrary flavors, just return 1655 // the raw bytes. For text flavors, convert to a String to strip 1656 // terminators and search-and-replace EOLN, then reencode according to 1657 // the requested flavor. 1658 } else if (byteArrayClass.equals(flavor.getRepresentationClass())) { 1659 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1660 theObject = translateBytesToString( 1661 bytes, format, localeTransferable 1662 ).getBytes(DataTransferer.getTextCharset(flavor)); 1663 } else { 1664 theObject = bytes; 1665 } 1666 1667 // Target data is an InputStream. For arbitrary flavors, just return 1668 // the raw bytes. For text flavors, decode to strip terminators and 1669 // search-and-replace EOLN, then reencode according to the requested 1670 // flavor. 1671 } else if (flavor.isRepresentationClassInputStream()) { 1672 1673 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) { 1674 theObject = translateStream(bais, flavor, format, localeTransferable); 1675 } 1676 1677 // Target data is Serializable 1678 } else if (flavor.isRepresentationClassSerializable()) { 1679 1680 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) { 1681 theObject = translateStream(bais, flavor, format, localeTransferable); 1682 } 1683 1684 // Target data is Image 1685 } else if (DataFlavor.imageFlavor.equals(flavor)) { 1686 if (!isImageFormat(format)) { 1687 throw new IOException("data translation failed"); 1688 } 1689 1690 theObject = platformImageBytesToImage(bytes, format); 1691 } 1692 1693 if (theObject == null) { 1694 throw new IOException("data translation failed"); 1695 } 1696 1697 return theObject; 1698 1699 } 1700 1701 /** 1702 * Primary translation function for translating 1703 * an InputStream into an Object, given a source format and a target 1704 * DataFlavor. 1705 */ 1706 public Object translateStream(InputStream str, DataFlavor flavor, 1707 long format, Transferable localeTransferable) 1708 throws IOException 1709 { 1710 1711 Object theObject = null; 1712 // Source data is a URI list. Convert to DataFlavor.javaFileListFlavor 1713 // where possible. 1714 if (isURIListFormat(format) 1715 && DataFlavor.javaFileListFlavor.equals(flavor)) 1716 { 1717 1718 URI uris[] = dragQueryURIs(str, format, localeTransferable); 1719 if (uris == null) { 1720 return null; 1721 } 1722 ArrayList files = new ArrayList(); 1723 for (URI uri : uris) { 1724 try { 1725 files.add(new File(uri)); 1726 } catch (IllegalArgumentException illegalArg) { 1727 // When converting from URIs to less generic files, 1728 // common practice (Wine, SWT) seems to be to 1729 // silently drop the URIs that aren't local files. 1730 } 1731 } 1732 theObject = files; 1733 1734 // Special hack to maintain backwards-compatibility with the brokenness 1735 // of StringSelection. Return a StringReader instead of an InputStream. 1736 // Recur to obtain String and encapsulate. 1737 } else if (DataFlavor.plainTextFlavor.equals(flavor)) { 1738 theObject = new StringReader(translateBytesToString( 1739 inputStreamToByteArray(str), 1740 format, localeTransferable)); 1741 1742 // Target data is an InputStream. For arbitrary flavors, just return 1743 // the raw bytes. For text flavors, decode to strip terminators and 1744 // search-and-replace EOLN, then reencode according to the requested 1745 // flavor. 1746 } else if (flavor.isRepresentationClassInputStream()) { 1747 theObject = translateStreamToInputStream(str, flavor, format, 1748 localeTransferable); 1749 1750 // Target data is a Reader. Obtain data in InputStream format, encoded 1751 // as "Unicode" (utf-16be). Then use an InputStreamReader to decode 1752 // back to chars on demand. 1753 } else if (flavor.isRepresentationClassReader()) { 1754 if (!(isFlavorCharsetTextType(flavor) && isTextFormat(format))) { 1755 throw new IOException 1756 ("cannot transfer non-text data as Reader"); 1757 } 1758 1759 InputStream is = (InputStream)translateStreamToInputStream( 1760 str, DataFlavor.plainTextFlavor, 1761 format, localeTransferable); 1762 1763 String unicode = DataTransferer.getTextCharset(DataFlavor.plainTextFlavor); 1764 1765 Reader reader = new InputStreamReader(is, unicode); 1766 1767 theObject = constructFlavoredObject(reader, flavor, Reader.class); 1768 // Target data is a byte array 1769 } else if (byteArrayClass.equals(flavor.getRepresentationClass())) { 1770 if(isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1771 theObject = translateBytesToString(inputStreamToByteArray(str), format, localeTransferable) 1772 .getBytes(DataTransferer.getTextCharset(flavor)); 1773 } else { 1774 theObject = inputStreamToByteArray(str); 1775 } 1776 // Target data is an RMI object 1777 } else if (flavor.isRepresentationClassRemote()) { 1778 1779 try (ObjectInputStream ois = 1780 new ObjectInputStream(str)) 1781 { 1782 theObject = RMI.getMarshalledObject(ois.readObject()); 1783 }catch (Exception e) { 1784 throw new IOException(e.getMessage()); 1785 } 1786 1787 // Target data is Serializable 1788 } else if (flavor.isRepresentationClassSerializable()) { 1789 try (ObjectInputStream ois = 1790 new ObjectInputStream(str)) 1791 { 1792 theObject = ois.readObject(); 1793 } catch (Exception e) { 1794 throw new IOException(e.getMessage()); 1795 } 1796 // Target data is Image 1797 } else if (DataFlavor.imageFlavor.equals(flavor)) { 1798 if (!isImageFormat(format)) { 1799 throw new IOException("data translation failed"); 1800 } 1801 theObject = platformImageBytesToImage(inputStreamToByteArray(str), format); 1802 } 1803 1804 if (theObject == null) { 1805 throw new IOException("data translation failed"); 1806 } 1807 1808 return theObject; 1809 1810 } 1811 1812 /** 1813 * For arbitrary flavors, just use the raw InputStream. For text flavors, 1814 * ReencodingInputStream will decode and reencode the InputStream on demand 1815 * so that we can strip terminators and search-and-replace EOLN. 1816 */ 1817 private Object translateStreamToInputStream 1818 (InputStream str, DataFlavor flavor, long format, 1819 Transferable localeTransferable) throws IOException 1820 { 1821 if (isFlavorCharsetTextType(flavor) && isTextFormat(format)) { 1822 str = new ReencodingInputStream 1823 (str, format, DataTransferer.getTextCharset(flavor), 1824 localeTransferable); 1825 } 1826 1827 return constructFlavoredObject(str, flavor, InputStream.class); 1828 } 1829 1830 /** 1831 * We support representations which are exactly of the specified Class, 1832 * and also arbitrary Objects which have a constructor which takes an 1833 * instance of the Class as its sole parameter. 1834 */ 1835 private Object constructFlavoredObject(Object arg, DataFlavor flavor, 1836 Class clazz) 1837 throws IOException 1838 { 1839 final Class dfrc = flavor.getRepresentationClass(); 1840 1841 if (clazz.equals(dfrc)) { 1842 return arg; // simple case 1843 } else { 1844 Constructor[] constructors = null; 1845 1846 try { 1847 constructors = (Constructor[]) 1848 AccessController.doPrivileged(new PrivilegedAction() { 1849 public Object run() { 1850 return dfrc.getConstructors(); 1851 } 1852 }); 1853 } catch (SecurityException se) { 1854 throw new IOException(se.getMessage()); 1855 } 1856 1857 Constructor constructor = null; 1858 1859 for (int j = 0; j < constructors.length; j++) { 1860 if (!Modifier.isPublic(constructors[j].getModifiers())) { 1861 continue; 1862 } 1863 1864 Class[] ptypes = constructors[j].getParameterTypes(); 1865 1866 if (ptypes != null && ptypes.length == 1 && 1867 clazz.equals(ptypes[0])) { 1868 constructor = constructors[j]; 1869 break; 1870 } 1871 } 1872 1873 if (constructor == null) { 1874 throw new IOException("can't find <init>(L"+ clazz + 1875 ";)V for class: " + dfrc.getName()); 1876 } 1877 1878 try { 1879 return constructor.newInstance(new Object[] { arg } ); 1880 } catch (Exception e) { 1881 throw new IOException(e.getMessage()); 1882 } 1883 } 1884 } 1885 1886 /** 1887 * Used for decoding and reencoding an InputStream on demand so that we 1888 * can strip NUL terminators and perform EOLN search-and-replace. 1889 */ 1890 public class ReencodingInputStream extends InputStream { 1891 protected BufferedReader wrapped; 1892 protected final char[] in = new char[2]; 1893 protected byte[] out; 1894 1895 protected CharsetEncoder encoder; 1896 protected CharBuffer inBuf; 1897 protected ByteBuffer outBuf; 1898 1899 protected char[] eoln; 1900 protected int numTerminators; 1901 1902 protected boolean eos; 1903 protected int index, limit; 1904 1905 public ReencodingInputStream(InputStream bytestream, long format, 1906 String targetEncoding, 1907 Transferable localeTransferable) 1908 throws IOException 1909 { 1910 Long lFormat = Long.valueOf(format); 1911 1912 String sourceEncoding = null; 1913 if (isLocaleDependentTextFormat(format) && 1914 localeTransferable != null && 1915 localeTransferable. 1916 isDataFlavorSupported(javaTextEncodingFlavor)) 1917 { 1918 try { 1919 sourceEncoding = new String((byte[])localeTransferable. 1920 getTransferData(javaTextEncodingFlavor), 1921 "UTF-8"); 1922 } catch (UnsupportedFlavorException cannotHappen) { 1923 } 1924 } else { 1925 sourceEncoding = getCharsetForTextFormat(lFormat); 1926 } 1927 1928 if (sourceEncoding == null) { 1929 // Only happens when we have a custom text type. 1930 sourceEncoding = getDefaultTextCharset(); 1931 } 1932 wrapped = new BufferedReader 1933 (new InputStreamReader(bytestream, sourceEncoding)); 1934 1935 if (targetEncoding == null) { 1936 // Throw NullPointerException for compatibility with the former 1937 // call to sun.io.CharToByteConverter.getConverter(null) 1938 // (Charset.forName(null) throws unspecified IllegalArgumentException 1939 // now; see 6228568) 1940 throw new NullPointerException("null target encoding"); 1941 } 1942 1943 try { 1944 encoder = Charset.forName(targetEncoding).newEncoder(); 1945 out = new byte[(int)(encoder.maxBytesPerChar() * 2 + 0.5)]; 1946 inBuf = CharBuffer.wrap(in); 1947 outBuf = ByteBuffer.wrap(out); 1948 } catch (IllegalCharsetNameException e) { 1949 throw new IOException(e.toString()); 1950 } catch (UnsupportedCharsetException e) { 1951 throw new IOException(e.toString()); 1952 } catch (UnsupportedOperationException e) { 1953 throw new IOException(e.toString()); 1954 } 1955 1956 String sEoln = (String)nativeEOLNs.get(lFormat); 1957 if (sEoln != null) { 1958 eoln = sEoln.toCharArray(); 1959 } 1960 1961 // A hope and a prayer that this works generically. This will 1962 // definitely work on Win32. 1963 Integer terminators = (Integer)nativeTerminators.get(lFormat); 1964 if (terminators != null) { 1965 numTerminators = terminators.intValue(); 1966 } 1967 } 1968 1969 private int readChar() throws IOException { 1970 int c = wrapped.read(); 1971 1972 if (c == -1) { // -1 is EOS 1973 eos = true; 1974 return -1; 1975 } 1976 1977 // "c == 0" is not quite correct, but good enough on Windows. 1978 if (numTerminators > 0 && c == 0) { 1979 eos = true; 1980 return -1; 1981 } else if (eoln != null && matchCharArray(eoln, c)) { 1982 c = '\n' & 0xFFFF; 1983 } 1984 1985 return c; 1986 } 1987 1988 public int read() throws IOException { 1989 if (eos) { 1990 return -1; 1991 } 1992 1993 if (index >= limit) { 1994 // deal with supplementary characters 1995 int c = readChar(); 1996 if (c == -1) { 1997 return -1; 1998 } 1999 2000 in[0] = (char) c; 2001 in[1] = 0; 2002 inBuf.limit(1); 2003 if (Character.isHighSurrogate((char) c)) { 2004 c = readChar(); 2005 if (c != -1) { 2006 in[1] = (char) c; 2007 inBuf.limit(2); 2008 } 2009 } 2010 2011 inBuf.rewind(); 2012 outBuf.limit(out.length).rewind(); 2013 encoder.encode(inBuf, outBuf, false); 2014 outBuf.flip(); 2015 limit = outBuf.limit(); 2016 2017 index = 0; 2018 2019 return read(); 2020 } else { 2021 return out[index++] & 0xFF; 2022 } 2023 } 2024 2025 public int available() throws IOException { 2026 return ((eos) ? 0 : (limit - index)); 2027 } 2028 2029 public void close() throws IOException { 2030 wrapped.close(); 2031 } 2032 2033 /** 2034 * Checks to see if the next array.length characters in wrapped 2035 * match array. The first character is provided as c. Subsequent 2036 * characters are read from wrapped itself. When this method returns, 2037 * the wrapped index may be different from what it was when this 2038 * method was called. 2039 */ 2040 private boolean matchCharArray(char[] array, int c) 2041 throws IOException 2042 { 2043 wrapped.mark(array.length); // BufferedReader supports mark 2044 2045 int count = 0; 2046 if ((char)c == array[0]) { 2047 for (count = 1; count < array.length; count++) { 2048 c = wrapped.read(); 2049 if (c == -1 || ((char)c) != array[count]) { 2050 break; 2051 } 2052 } 2053 } 2054 2055 if (count == array.length) { 2056 return true; 2057 } else { 2058 wrapped.reset(); 2059 return false; 2060 } 2061 } 2062 } 2063 2064 /** 2065 * Decodes a byte array into a set of String filenames. 2066 */ 2067 protected abstract String[] dragQueryFile(byte[] bytes); 2068 2069 /** 2070 * Decodes URIs from either a byte array or a stream. 2071 */ 2072 protected URI[] dragQueryURIs(InputStream stream, 2073 long format, 2074 Transferable localeTransferable) 2075 throws IOException 2076 { 2077 throw new IOException( 2078 new UnsupportedOperationException("not implemented on this platform")); 2079 } 2080 2081 /** 2082 * Translates either a byte array or an input stream which contain 2083 * platform-specific image data in the given format into an Image. 2084 */ 2085 2086 2087 protected abstract Image platformImageBytesToImage( 2088 byte[] bytes,long format) throws IOException; 2089 2090 /** 2091 * Translates either a byte array or an input stream which contain 2092 * an image data in the given standard format into an Image. 2093 * 2094 * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif 2095 */ 2096 protected Image standardImageBytesToImage( 2097 byte[] bytes, String mimeType) throws IOException 2098 { 2099 2100 Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType); 2101 2102 if (!readerIterator.hasNext()) { 2103 throw new IOException("No registered service provider can decode " + 2104 " an image from " + mimeType); 2105 } 2106 2107 IOException ioe = null; 2108 2109 while (readerIterator.hasNext()) { 2110 ImageReader imageReader = (ImageReader)readerIterator.next(); 2111 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) { 2112 ImageInputStream imageInputStream = 2113 ImageIO.createImageInputStream(bais); 2114 2115 try { 2116 ImageReadParam param = imageReader.getDefaultReadParam(); 2117 imageReader.setInput(imageInputStream, true, true); 2118 BufferedImage bufferedImage = 2119 imageReader.read(imageReader.getMinIndex(), param); 2120 if (bufferedImage != null) { 2121 return bufferedImage; 2122 } 2123 } finally { 2124 imageInputStream.close(); 2125 imageReader.dispose(); 2126 } 2127 } catch (IOException e) { 2128 ioe = e; 2129 continue; 2130 } 2131 } 2132 2133 if (ioe == null) { 2134 ioe = new IOException("Registered service providers failed to decode" 2135 + " an image from " + mimeType); 2136 } 2137 2138 throw ioe; 2139 } 2140 2141 /** 2142 * Translates a Java Image into a byte array which contains platform- 2143 * specific image data in the given format. 2144 */ 2145 protected abstract byte[] imageToPlatformBytes(Image image, long format) 2146 throws IOException; 2147 2148 /** 2149 * Translates a Java Image into a byte array which contains 2150 * an image data in the given standard format. 2151 * 2152 * @param mimeType image MIME type, such as: image/png, image/jpeg 2153 */ 2154 protected byte[] imageToStandardBytes(Image image, String mimeType) 2155 throws IOException { 2156 IOException originalIOE = null; 2157 2158 Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); 2159 2160 if (!writerIterator.hasNext()) { 2161 throw new IOException("No registered service provider can encode " + 2162 " an image to " + mimeType); 2163 } 2164 2165 if (image instanceof RenderedImage) { 2166 // Try to encode the original image. 2167 try { 2168 return imageToStandardBytesImpl((RenderedImage)image, mimeType); 2169 } catch (IOException ioe) { 2170 originalIOE = ioe; 2171 } 2172 } 2173 2174 // Retry with a BufferedImage. 2175 int width = 0; 2176 int height = 0; 2177 if (image instanceof ToolkitImage) { 2178 ImageRepresentation ir = ((ToolkitImage)image).getImageRep(); 2179 ir.reconstruct(ImageObserver.ALLBITS); 2180 width = ir.getWidth(); 2181 height = ir.getHeight(); 2182 } else { 2183 width = image.getWidth(null); 2184 height = image.getHeight(null); 2185 } 2186 2187 ColorModel model = ColorModel.getRGBdefault(); 2188 WritableRaster raster = 2189 model.createCompatibleWritableRaster(width, height); 2190 2191 BufferedImage bufferedImage = 2192 new BufferedImage(model, raster, model.isAlphaPremultiplied(), 2193 null); 2194 2195 Graphics g = bufferedImage.getGraphics(); 2196 try { 2197 g.drawImage(image, 0, 0, width, height, null); 2198 } finally { 2199 g.dispose(); 2200 } 2201 2202 try { 2203 return imageToStandardBytesImpl(bufferedImage, mimeType); 2204 } catch (IOException ioe) { 2205 if (originalIOE != null) { 2206 throw originalIOE; 2207 } else { 2208 throw ioe; 2209 } 2210 } 2211 } 2212 2213 protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage, 2214 String mimeType) 2215 throws IOException { 2216 2217 Iterator writerIterator = ImageIO.getImageWritersByMIMEType(mimeType); 2218 2219 ImageTypeSpecifier typeSpecifier = 2220 new ImageTypeSpecifier(renderedImage); 2221 2222 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 2223 IOException ioe = null; 2224 2225 while (writerIterator.hasNext()) { 2226 ImageWriter imageWriter = (ImageWriter)writerIterator.next(); 2227 ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider(); 2228 2229 if (!writerSpi.canEncodeImage(typeSpecifier)) { 2230 continue; 2231 } 2232 2233 try { 2234 ImageOutputStream imageOutputStream = 2235 ImageIO.createImageOutputStream(baos); 2236 try { 2237 imageWriter.setOutput(imageOutputStream); 2238 imageWriter.write(renderedImage); 2239 imageOutputStream.flush(); 2240 } finally { 2241 imageOutputStream.close(); 2242 } 2243 } catch (IOException e) { 2244 imageWriter.dispose(); 2245 baos.reset(); 2246 ioe = e; 2247 continue; 2248 } 2249 2250 imageWriter.dispose(); 2251 baos.close(); 2252 return baos.toByteArray(); 2253 } 2254 2255 baos.close(); 2256 2257 if (ioe == null) { 2258 ioe = new IOException("Registered service providers failed to encode " 2259 + renderedImage + " to " + mimeType); 2260 } 2261 2262 throw ioe; 2263 } 2264 2265 /** 2266 * Concatenates the data represented by two objects. Objects can be either 2267 * byte arrays or instances of <code>InputStream</code>. If both arguments 2268 * are byte arrays byte array will be returned. Otherwise an 2269 * <code>InputStream</code> will be returned. 2270 * <p> 2271 * Currently is only called from native code to prepend palette data to 2272 * platform-specific image data during image transfer on Win32. 2273 * 2274 * @param obj1 the first object to be concatenated. 2275 * @param obj2 the second object to be concatenated. 2276 * @return a byte array or an <code>InputStream</code> which represents 2277 * a logical concatenation of the two arguments. 2278 * @throws NullPointerException is either of the arguments is 2279 * <code>null</code> 2280 * @throws ClassCastException is either of the arguments is 2281 * neither byte array nor an instance of <code>InputStream</code>. 2282 */ 2283 private Object concatData(Object obj1, Object obj2) { 2284 InputStream str1 = null; 2285 InputStream str2 = null; 2286 2287 if (obj1 instanceof byte[]) { 2288 byte[] arr1 = (byte[])obj1; 2289 if (obj2 instanceof byte[]) { 2290 byte[] arr2 = (byte[])obj2; 2291 byte[] ret = new byte[arr1.length + arr2.length]; 2292 System.arraycopy(arr1, 0, ret, 0, arr1.length); 2293 System.arraycopy(arr2, 0, ret, arr1.length, arr2.length); 2294 return ret; 2295 } else { 2296 str1 = new ByteArrayInputStream(arr1); 2297 str2 = (InputStream)obj2; 2298 } 2299 } else { 2300 str1 = (InputStream)obj1; 2301 if (obj2 instanceof byte[]) { 2302 str2 = new ByteArrayInputStream((byte[])obj2); 2303 } else { 2304 str2 = (InputStream)obj2; 2305 } 2306 } 2307 2308 return new SequenceInputStream(str1, str2); 2309 } 2310 2311 public byte[] convertData(final Object source, 2312 final Transferable contents, 2313 final long format, 2314 final Map formatMap, 2315 final boolean isToolkitThread) 2316 throws IOException 2317 { 2318 byte[] ret = null; 2319 2320 /* 2321 * If the current thread is the Toolkit thread we should post a 2322 * Runnable to the event dispatch thread associated with source Object, 2323 * since translateTransferable() calls Transferable.getTransferData() 2324 * that may contain client code. 2325 */ 2326 if (isToolkitThread) try { 2327 final Stack stack = new Stack(); 2328 final Runnable dataConverter = new Runnable() { 2329 // Guard against multiple executions. 2330 private boolean done = false; 2331 public void run() { 2332 if (done) { 2333 return; 2334 } 2335 byte[] data = null; 2336 try { 2337 DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format)); 2338 if (flavor != null) { 2339 data = translateTransferable(contents, flavor, format); 2340 } 2341 } catch (Exception e) { 2342 e.printStackTrace(); 2343 data = null; 2344 } 2345 try { 2346 getToolkitThreadBlockedHandler().lock(); 2347 stack.push(data); 2348 getToolkitThreadBlockedHandler().exit(); 2349 } finally { 2350 getToolkitThreadBlockedHandler().unlock(); 2351 done = true; 2352 } 2353 } 2354 }; 2355 2356 final AppContext appContext = SunToolkit.targetToAppContext(source); 2357 2358 getToolkitThreadBlockedHandler().lock(); 2359 2360 if (appContext != null) { 2361 appContext.put(DATA_CONVERTER_KEY, dataConverter); 2362 } 2363 2364 SunToolkit.executeOnEventHandlerThread(source, dataConverter); 2365 2366 while (stack.empty()) { 2367 getToolkitThreadBlockedHandler().enter(); 2368 } 2369 2370 if (appContext != null) { 2371 appContext.remove(DATA_CONVERTER_KEY); 2372 } 2373 2374 ret = (byte[])stack.pop(); 2375 } finally { 2376 getToolkitThreadBlockedHandler().unlock(); 2377 } else { 2378 DataFlavor flavor = (DataFlavor) 2379 formatMap.get(Long.valueOf(format)); 2380 if (flavor != null) { 2381 ret = translateTransferable(contents, flavor, format); 2382 } 2383 } 2384 2385 return ret; 2386 } 2387 2388 public void processDataConversionRequests() { 2389 if (EventQueue.isDispatchThread()) { 2390 AppContext appContext = AppContext.getAppContext(); 2391 getToolkitThreadBlockedHandler().lock(); 2392 try { 2393 Runnable dataConverter = 2394 (Runnable)appContext.get(DATA_CONVERTER_KEY); 2395 if (dataConverter != null) { 2396 dataConverter.run(); 2397 appContext.remove(DATA_CONVERTER_KEY); 2398 } 2399 } finally { 2400 getToolkitThreadBlockedHandler().unlock(); 2401 } 2402 } 2403 } 2404 2405 public abstract ToolkitThreadBlockedHandler 2406 getToolkitThreadBlockedHandler(); 2407 2408 /** 2409 * Helper function to reduce a Map with Long keys to a long array. 2410 * <p> 2411 * The map keys are sorted according to the native formats preference 2412 * order. 2413 */ 2414 public static long[] keysToLongArray(SortedMap map) { 2415 Set keySet = map.keySet(); 2416 long[] retval = new long[keySet.size()]; 2417 int i = 0; 2418 for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) { 2419 retval[i] = ((Long)iter.next()).longValue(); 2420 } 2421 return retval; 2422 } 2423 2424 /** 2425 * Helper function to convert a Set of DataFlavors to a sorted array. 2426 * The array will be sorted according to <code>DataFlavorComparator</code>. 2427 */ 2428 public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) { 2429 DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; 2430 flavorsSet.toArray(flavors); 2431 final Comparator comparator = 2432 new DataFlavorComparator(IndexedComparator.SELECT_WORST); 2433 Arrays.sort(flavors, comparator); 2434 return flavors; 2435 } 2436 2437 /** 2438 * Helper function to convert an InputStream to a byte[] array. 2439 */ 2440 protected static byte[] inputStreamToByteArray(InputStream str) 2441 throws IOException 2442 { 2443 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 2444 int len = 0; 2445 byte[] buf = new byte[8192]; 2446 2447 while ((len = str.read(buf)) != -1) { 2448 baos.write(buf, 0, len); 2449 } 2450 2451 return baos.toByteArray(); 2452 } 2453 } 2454 2455 /** 2456 * Returns platform-specific mappings for the specified native. 2457 * If there are no platform-specific mappings for this native, the method 2458 * returns an empty <code>List</code>. 2459 */ 2460 public List getPlatformMappingsForNative(String nat) { 2461 return new ArrayList(); 2462 } 2463 2464 /** 2465 * Returns platform-specific mappings for the specified flavor. 2466 * If there are no platform-specific mappings for this flavor, the method 2467 * returns an empty <code>List</code>. 2468 */ 2469 public List getPlatformMappingsForFlavor(DataFlavor df) { 2470 return new ArrayList(); 2471 } 2472 2473 /** 2474 * A Comparator which includes a helper function for comparing two Objects 2475 * which are likely to be keys in the specified Map. 2476 */ 2477 public abstract static class IndexedComparator implements Comparator { 2478 2479 /** 2480 * The best Object (e.g., DataFlavor) will be the last in sequence. 2481 */ 2482 public static final boolean SELECT_BEST = true; 2483 2484 /** 2485 * The best Object (e.g., DataFlavor) will be the first in sequence. 2486 */ 2487 public static final boolean SELECT_WORST = false; 2488 2489 protected final boolean order; 2490 2491 public IndexedComparator() { 2492 this(SELECT_BEST); 2493 } 2494 2495 public IndexedComparator(boolean order) { 2496 this.order = order; 2497 } 2498 2499 /** 2500 * Helper method to compare two objects by their Integer indices in the 2501 * given map. If the map doesn't contain an entry for either of the 2502 * objects, the fallback index will be used for the object instead. 2503 * 2504 * @param indexMap the map which maps objects into Integer indexes. 2505 * @param obj1 the first object to be compared. 2506 * @param obj2 the second object to be compared. 2507 * @param fallbackIndex the Integer to be used as a fallback index. 2508 * @return a negative integer, zero, or a positive integer as the 2509 * first object is mapped to a less, equal to, or greater 2510 * index than the second. 2511 */ 2512 protected static int compareIndices(Map indexMap, 2513 Object obj1, Object obj2, 2514 Integer fallbackIndex) { 2515 Integer index1 = (Integer)indexMap.get(obj1); 2516 Integer index2 = (Integer)indexMap.get(obj2); 2517 2518 if (index1 == null) { 2519 index1 = fallbackIndex; 2520 } 2521 if (index2 == null) { 2522 index2 = fallbackIndex; 2523 } 2524 2525 return index1.compareTo(index2); 2526 } 2527 2528 /** 2529 * Helper method to compare two objects by their Long indices in the 2530 * given map. If the map doesn't contain an entry for either of the 2531 * objects, the fallback index will be used for the object instead. 2532 * 2533 * @param indexMap the map which maps objects into Long indexes. 2534 * @param obj1 the first object to be compared. 2535 * @param obj2 the second object to be compared. 2536 * @param fallbackIndex the Long to be used as a fallback index. 2537 * @return a negative integer, zero, or a positive integer as the 2538 * first object is mapped to a less, equal to, or greater 2539 * index than the second. 2540 */ 2541 protected static int compareLongs(Map indexMap, 2542 Object obj1, Object obj2, 2543 Long fallbackIndex) { 2544 Long index1 = (Long)indexMap.get(obj1); 2545 Long index2 = (Long)indexMap.get(obj2); 2546 2547 if (index1 == null) { 2548 index1 = fallbackIndex; 2549 } 2550 if (index2 == null) { 2551 index2 = fallbackIndex; 2552 } 2553 2554 return index1.compareTo(index2); 2555 } 2556 } 2557 2558 /** 2559 * An IndexedComparator which compares two String charsets. The comparison 2560 * follows the rules outlined in DataFlavor.selectBestTextFlavor. In order 2561 * to ensure that non-Unicode, non-ASCII, non-default charsets are sorted 2562 * in alphabetical order, charsets are not automatically converted to their 2563 * canonical forms. 2564 */ 2565 public static class CharsetComparator extends IndexedComparator { 2566 private static final Map charsets; 2567 private static String defaultEncoding; 2568 2569 private static final Integer DEFAULT_CHARSET_INDEX = Integer.valueOf(2); 2570 private static final Integer OTHER_CHARSET_INDEX = Integer.valueOf(1); 2571 private static final Integer WORST_CHARSET_INDEX = Integer.valueOf(0); 2572 private static final Integer UNSUPPORTED_CHARSET_INDEX = 2573 Integer.valueOf(Integer.MIN_VALUE); 2574 2575 private static final String UNSUPPORTED_CHARSET = "UNSUPPORTED"; 2576 2577 static { 2578 HashMap charsetsMap = new HashMap(8, 1.0f); 2579 2580 // we prefer Unicode charsets 2581 charsetsMap.put(canonicalName("UTF-16LE"), Integer.valueOf(4)); 2582 charsetsMap.put(canonicalName("UTF-16BE"), Integer.valueOf(5)); 2583 charsetsMap.put(canonicalName("UTF-8"), Integer.valueOf(6)); 2584 charsetsMap.put(canonicalName("UTF-16"), Integer.valueOf(7)); 2585 2586 // US-ASCII is the worst charset supported 2587 charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX); 2588 2589 String defEncoding = DataTransferer.canonicalName 2590 (DataTransferer.getDefaultTextCharset()); 2591 2592 if (charsetsMap.get(defaultEncoding) == null) { 2593 charsetsMap.put(defaultEncoding, DEFAULT_CHARSET_INDEX); 2594 } 2595 charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); 2596 2597 charsets = Collections.unmodifiableMap(charsetsMap); 2598 } 2599 2600 public CharsetComparator() { 2601 this(SELECT_BEST); 2602 } 2603 2604 public CharsetComparator(boolean order) { 2605 super(order); 2606 } 2607 2608 /** 2609 * Compares two String objects. Returns a negative integer, zero, 2610 * or a positive integer as the first charset is worse than, equal to, 2611 * or better than the second. 2612 * 2613 * @param obj1 the first charset to be compared 2614 * @param obj2 the second charset to be compared 2615 * @return a negative integer, zero, or a positive integer as the 2616 * first argument is worse, equal to, or better than the 2617 * second. 2618 * @throws ClassCastException if either of the arguments is not 2619 * instance of String 2620 * @throws NullPointerException if either of the arguments is 2621 * <code>null</code>. 2622 */ 2623 public int compare(Object obj1, Object obj2) { 2624 String charset1 = null; 2625 String charset2 = null; 2626 if (order == SELECT_BEST) { 2627 charset1 = (String)obj1; 2628 charset2 = (String)obj2; 2629 } else { 2630 charset1 = (String)obj2; 2631 charset2 = (String)obj1; 2632 } 2633 2634 return compareCharsets(charset1, charset2); 2635 } 2636 2637 /** 2638 * Compares charsets. Returns a negative integer, zero, or a positive 2639 * integer as the first charset is worse than, equal to, or better than 2640 * the second. 2641 * <p> 2642 * Charsets are ordered according to the following rules: 2643 * <ul> 2644 * <li>All unsupported charsets are equal. 2645 * <li>Any unsupported charset is worse than any supported charset. 2646 * <li>Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE" and 2647 * "UTF-16LE", are considered best. 2648 * <li>After them, platform default charset is selected. 2649 * <li>"US-ASCII" is the worst of supported charsets. 2650 * <li>For all other supported charsets, the lexicographically less 2651 * one is considered the better. 2652 * </ul> 2653 * 2654 * @param charset1 the first charset to be compared 2655 * @param charset2 the second charset to be compared. 2656 * @return a negative integer, zero, or a positive integer as the 2657 * first argument is worse, equal to, or better than the 2658 * second. 2659 */ 2660 protected int compareCharsets(String charset1, String charset2) { 2661 charset1 = getEncoding(charset1); 2662 charset2 = getEncoding(charset2); 2663 2664 int comp = compareIndices(charsets, charset1, charset2, 2665 OTHER_CHARSET_INDEX); 2666 2667 if (comp == 0) { 2668 return charset2.compareTo(charset1); 2669 } 2670 2671 return comp; 2672 } 2673 2674 /** 2675 * Returns encoding for the specified charset according to the 2676 * following rules: 2677 * <ul> 2678 * <li>If the charset is <code>null</code>, then <code>null</code> will 2679 * be returned. 2680 * <li>Iff the charset specifies an encoding unsupported by this JRE, 2681 * <code>UNSUPPORTED_CHARSET</code> will be returned. 2682 * <li>If the charset specifies an alias name, the corresponding 2683 * canonical name will be returned iff the charset is a known 2684 * Unicode, ASCII, or default charset. 2685 * </ul> 2686 * 2687 * @param charset the charset. 2688 * @return an encoding for this charset. 2689 */ 2690 protected static String getEncoding(String charset) { 2691 if (charset == null) { 2692 return null; 2693 } else if (!DataTransferer.isEncodingSupported(charset)) { 2694 return UNSUPPORTED_CHARSET; 2695 } else { 2696 // Only convert to canonical form if the charset is one 2697 // of the charsets explicitly listed in the known charsets 2698 // map. This will happen only for Unicode, ASCII, or default 2699 // charsets. 2700 String canonicalName = DataTransferer.canonicalName(charset); 2701 return (charsets.containsKey(canonicalName)) 2702 ? canonicalName 2703 : charset; 2704 } 2705 } 2706 } 2707 2708 /** 2709 * An IndexedComparator which compares two DataFlavors. For text flavors, 2710 * the comparison follows the rules outlined in 2711 * DataFlavor.selectBestTextFlavor. For non-text flavors, unknown 2712 * application MIME types are preferred, followed by known 2713 * application/x-java-* MIME types. Unknown application types are preferred 2714 * because if the user provides his own data flavor, it will likely be the 2715 * most descriptive one. For flavors which are otherwise equal, the 2716 * flavors' string representation are compared in the alphabetical order. 2717 */ 2718 public static class DataFlavorComparator extends IndexedComparator { 2719 2720 private final CharsetComparator charsetComparator; 2721 2722 private static final Map exactTypes; 2723 private static final Map primaryTypes; 2724 private static final Map nonTextRepresentations; 2725 private static final Map textTypes; 2726 private static final Map decodedTextRepresentations; 2727 private static final Map encodedTextRepresentations; 2728 2729 private static final Integer UNKNOWN_OBJECT_LOSES = 2730 Integer.valueOf(Integer.MIN_VALUE); 2731 private static final Integer UNKNOWN_OBJECT_WINS = 2732 Integer.valueOf(Integer.MAX_VALUE); 2733 2734 private static final Long UNKNOWN_OBJECT_LOSES_L = 2735 Long.valueOf(Long.MIN_VALUE); 2736 private static final Long UNKNOWN_OBJECT_WINS_L = 2737 Long.valueOf(Long.MAX_VALUE); 2738 2739 static { 2740 { 2741 HashMap exactTypesMap = new HashMap(4, 1.0f); 2742 2743 // application/x-java-* MIME types 2744 exactTypesMap.put("application/x-java-file-list", 2745 Integer.valueOf(0)); 2746 exactTypesMap.put("application/x-java-serialized-object", 2747 Integer.valueOf(1)); 2748 exactTypesMap.put("application/x-java-jvm-local-objectref", 2749 Integer.valueOf(2)); 2750 exactTypesMap.put("application/x-java-remote-object", 2751 Integer.valueOf(3)); 2752 2753 exactTypes = Collections.unmodifiableMap(exactTypesMap); 2754 } 2755 2756 { 2757 HashMap primaryTypesMap = new HashMap(1, 1.0f); 2758 2759 primaryTypesMap.put("application", Integer.valueOf(0)); 2760 2761 primaryTypes = Collections.unmodifiableMap(primaryTypesMap); 2762 } 2763 2764 { 2765 HashMap nonTextRepresentationsMap = new HashMap(3, 1.0f); 2766 2767 nonTextRepresentationsMap.put(java.io.InputStream.class, 2768 Integer.valueOf(0)); 2769 nonTextRepresentationsMap.put(java.io.Serializable.class, 2770 Integer.valueOf(1)); 2771 2772 Class<?> remoteClass = RMI.remoteClass(); 2773 if (remoteClass != null) { 2774 nonTextRepresentationsMap.put(remoteClass, 2775 Integer.valueOf(2)); 2776 } 2777 2778 nonTextRepresentations = 2779 Collections.unmodifiableMap(nonTextRepresentationsMap); 2780 } 2781 2782 { 2783 HashMap textTypesMap = new HashMap(16, 1.0f); 2784 2785 // plain text 2786 textTypesMap.put("text/plain", Integer.valueOf(0)); 2787 2788 // stringFlavor 2789 textTypesMap.put("application/x-java-serialized-object", 2790 Integer.valueOf(1)); 2791 2792 // misc 2793 textTypesMap.put("text/calendar", Integer.valueOf(2)); 2794 textTypesMap.put("text/css", Integer.valueOf(3)); 2795 textTypesMap.put("text/directory", Integer.valueOf(4)); 2796 textTypesMap.put("text/parityfec", Integer.valueOf(5)); 2797 textTypesMap.put("text/rfc822-headers", Integer.valueOf(6)); 2798 textTypesMap.put("text/t140", Integer.valueOf(7)); 2799 textTypesMap.put("text/tab-separated-values", Integer.valueOf(8)); 2800 textTypesMap.put("text/uri-list", Integer.valueOf(9)); 2801 2802 // enriched 2803 textTypesMap.put("text/richtext", Integer.valueOf(10)); 2804 textTypesMap.put("text/enriched", Integer.valueOf(11)); 2805 textTypesMap.put("text/rtf", Integer.valueOf(12)); 2806 2807 // markup 2808 textTypesMap.put("text/html", Integer.valueOf(13)); 2809 textTypesMap.put("text/xml", Integer.valueOf(14)); 2810 textTypesMap.put("text/sgml", Integer.valueOf(15)); 2811 2812 textTypes = Collections.unmodifiableMap(textTypesMap); 2813 } 2814 2815 { 2816 HashMap decodedTextRepresentationsMap = new HashMap(4, 1.0f); 2817 2818 decodedTextRepresentationsMap.put 2819 (DataTransferer.charArrayClass, Integer.valueOf(0)); 2820 decodedTextRepresentationsMap.put 2821 (java.nio.CharBuffer.class, Integer.valueOf(1)); 2822 decodedTextRepresentationsMap.put 2823 (java.lang.String.class, Integer.valueOf(2)); 2824 decodedTextRepresentationsMap.put 2825 (java.io.Reader.class, Integer.valueOf(3)); 2826 2827 decodedTextRepresentations = 2828 Collections.unmodifiableMap(decodedTextRepresentationsMap); 2829 } 2830 2831 { 2832 HashMap encodedTextRepresentationsMap = new HashMap(3, 1.0f); 2833 2834 encodedTextRepresentationsMap.put 2835 (DataTransferer.byteArrayClass, Integer.valueOf(0)); 2836 encodedTextRepresentationsMap.put 2837 (java.nio.ByteBuffer.class, Integer.valueOf(1)); 2838 encodedTextRepresentationsMap.put 2839 (java.io.InputStream.class, Integer.valueOf(2)); 2840 2841 encodedTextRepresentations = 2842 Collections.unmodifiableMap(encodedTextRepresentationsMap); 2843 } 2844 } 2845 2846 public DataFlavorComparator() { 2847 this(SELECT_BEST); 2848 } 2849 2850 public DataFlavorComparator(boolean order) { 2851 super(order); 2852 2853 charsetComparator = new CharsetComparator(order); 2854 } 2855 2856 public int compare(Object obj1, Object obj2) { 2857 DataFlavor flavor1 = null; 2858 DataFlavor flavor2 = null; 2859 if (order == SELECT_BEST) { 2860 flavor1 = (DataFlavor)obj1; 2861 flavor2 = (DataFlavor)obj2; 2862 } else { 2863 flavor1 = (DataFlavor)obj2; 2864 flavor2 = (DataFlavor)obj1; 2865 } 2866 2867 if (flavor1.equals(flavor2)) { 2868 return 0; 2869 } 2870 2871 int comp = 0; 2872 2873 String primaryType1 = flavor1.getPrimaryType(); 2874 String subType1 = flavor1.getSubType(); 2875 String mimeType1 = primaryType1 + "/" + subType1; 2876 Class class1 = flavor1.getRepresentationClass(); 2877 2878 String primaryType2 = flavor2.getPrimaryType(); 2879 String subType2 = flavor2.getSubType(); 2880 String mimeType2 = primaryType2 + "/" + subType2; 2881 Class class2 = flavor2.getRepresentationClass(); 2882 2883 if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) { 2884 // First, compare MIME types 2885 comp = compareIndices(textTypes, mimeType1, mimeType2, 2886 UNKNOWN_OBJECT_LOSES); 2887 if (comp != 0) { 2888 return comp; 2889 } 2890 2891 // Only need to test one flavor because they both have the 2892 // same MIME type. Also don't need to worry about accidentally 2893 // passing stringFlavor because either 2894 // 1. Both flavors are stringFlavor, in which case the 2895 // equality test at the top of the function succeeded. 2896 // 2. Only one flavor is stringFlavor, in which case the MIME 2897 // type comparison returned a non-zero value. 2898 if (doesSubtypeSupportCharset(flavor1)) { 2899 // Next, prefer the decoded text representations of Reader, 2900 // String, CharBuffer, and [C, in that order. 2901 comp = compareIndices(decodedTextRepresentations, class1, 2902 class2, UNKNOWN_OBJECT_LOSES); 2903 if (comp != 0) { 2904 return comp; 2905 } 2906 2907 // Next, compare charsets 2908 comp = charsetComparator.compareCharsets 2909 (DataTransferer.getTextCharset(flavor1), 2910 DataTransferer.getTextCharset(flavor2)); 2911 if (comp != 0) { 2912 return comp; 2913 } 2914 } 2915 2916 // Finally, prefer the encoded text representations of 2917 // InputStream, ByteBuffer, and [B, in that order. 2918 comp = compareIndices(encodedTextRepresentations, class1, 2919 class2, UNKNOWN_OBJECT_LOSES); 2920 if (comp != 0) { 2921 return comp; 2922 } 2923 } else { 2924 // First, prefer application types. 2925 comp = compareIndices(primaryTypes, primaryType1, primaryType2, 2926 UNKNOWN_OBJECT_LOSES); 2927 if (comp != 0) { 2928 return comp; 2929 } 2930 2931 // Next, look for application/x-java-* types. Prefer unknown 2932 // MIME types because if the user provides his own data flavor, 2933 // it will likely be the most descriptive one. 2934 comp = compareIndices(exactTypes, mimeType1, mimeType2, 2935 UNKNOWN_OBJECT_WINS); 2936 if (comp != 0) { 2937 return comp; 2938 } 2939 2940 // Finally, prefer the representation classes of Remote, 2941 // Serializable, and InputStream, in that order. 2942 comp = compareIndices(nonTextRepresentations, class1, class2, 2943 UNKNOWN_OBJECT_LOSES); 2944 if (comp != 0) { 2945 return comp; 2946 } 2947 } 2948 2949 // The flavours are not equal but still not distinguishable. 2950 // Compare String representations in alphabetical order 2951 return flavor1.getMimeType().compareTo(flavor2.getMimeType()); 2952 } 2953 } 2954 2955 /* 2956 * Given the Map that maps objects to Integer indices and a boolean value, 2957 * this Comparator imposes a direct or reverse order on set of objects. 2958 * <p> 2959 * If the specified boolean value is SELECT_BEST, the Comparator imposes the 2960 * direct index-based order: an object A is greater than an object B if and 2961 * only if the index of A is greater than the index of B. An object that 2962 * doesn't have an associated index is less or equal than any other object. 2963 * <p> 2964 * If the specified boolean value is SELECT_WORST, the Comparator imposes the 2965 * reverse index-based order: an object A is greater than an object B if and 2966 * only if A is less than B with the direct index-based order. 2967 */ 2968 public static class IndexOrderComparator extends IndexedComparator { 2969 private final Map indexMap; 2970 private static final Integer FALLBACK_INDEX = 2971 Integer.valueOf(Integer.MIN_VALUE); 2972 2973 public IndexOrderComparator(Map indexMap) { 2974 super(SELECT_BEST); 2975 this.indexMap = indexMap; 2976 } 2977 2978 public IndexOrderComparator(Map indexMap, boolean order) { 2979 super(order); 2980 this.indexMap = indexMap; 2981 } 2982 2983 public int compare(Object obj1, Object obj2) { 2984 if (order == SELECT_WORST) { 2985 return -compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX); 2986 } else { 2987 return compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX); 2988 } 2989 } 2990 } 2991 2992 /** 2993 * A class that provides access to java.rmi.Remote and java.rmi.MarshalledObject 2994 * without creating a static dependency. 2995 */ 2996 private static class RMI { 2997 private static final Class<?> remoteClass = getClass("java.rmi.Remote"); 2998 private static final Class<?> marshallObjectClass = 2999 getClass("java.rmi.MarshalledObject"); 3000 private static final Constructor<?> marshallCtor = 3001 getConstructor(marshallObjectClass, Object.class); 3002 private static final Method marshallGet = 3003 getMethod(marshallObjectClass, "get"); 3004 3005 private static Class<?> getClass(String name) { 3006 try { 3007 return Class.forName(name, true, null); 3008 } catch (ClassNotFoundException e) { 3009 return null; 3010 } 3011 } 3012 3013 private static Constructor<?> getConstructor(Class<?> c, Class<?>... types) { 3014 try { 3015 return (c == null) ? null : c.getDeclaredConstructor(types); 3016 } catch (NoSuchMethodException x) { 3017 throw new AssertionError(x); 3018 } 3019 } 3020 3021 private static Method getMethod(Class<?> c, String name, Class<?>... types) { 3022 try { 3023 return (c == null) ? null : c.getMethod(name, types); 3024 } catch (NoSuchMethodException e) { 3025 throw new AssertionError(e); 3026 } 3027 } 3028 3029 /** 3030 * Returns {@code true} if the given class is java.rmi.Remote. 3031 */ 3032 static boolean isRemote(Class<?> c) { 3033 return (remoteClass == null) ? null : remoteClass.isAssignableFrom(c); 3034 } 3035 3036 /** 3037 * Returns java.rmi.Remote.class if RMI is present; otherwise {@code null}. 3038 */ 3039 static Class<?> remoteClass() { 3040 return remoteClass; 3041 } 3042 3043 /** 3044 * Returns a new MarshalledObject containing the serialized representation 3045 * of the given object. 3046 */ 3047 static Object newMarshalledObject(Object obj) throws IOException { 3048 try { 3049 return marshallCtor.newInstance(obj); 3050 } catch (InstantiationException x) { 3051 throw new AssertionError(x); 3052 } catch (IllegalAccessException x) { 3053 throw new AssertionError(x); 3054 } catch (InvocationTargetException x) { 3055 Throwable cause = x.getCause(); 3056 if (cause instanceof IOException) 3057 throw (IOException)cause; 3058 throw new AssertionError(x); 3059 } 3060 } 3061 3062 /** 3063 * Returns a new copy of the contained marshalled object. 3064 */ 3065 static Object getMarshalledObject(Object obj) 3066 throws IOException, ClassNotFoundException 3067 { 3068 try { 3069 return marshallGet.invoke(obj); 3070 } catch (IllegalAccessException x) { 3071 throw new AssertionError(x); 3072 } catch (InvocationTargetException x) { 3073 Throwable cause = x.getCause(); 3074 if (cause instanceof IOException) 3075 throw (IOException)cause; 3076 if (cause instanceof ClassNotFoundException) 3077 throw (ClassNotFoundException)cause; 3078 throw new AssertionError(x); 3079 } 3080 } 3081 } 3082 }