src/share/classes/sun/awt/datatransfer/DataTransferer.java

Print this page




2389     public abstract ToolkitThreadBlockedHandler
2390         getToolkitThreadBlockedHandler();
2391 
2392     /**
2393      * Helper function to reduce a Map with Long keys to a long array.
2394      * <p>
2395      * The map keys are sorted according to the native formats preference
2396      * order.
2397      */
2398     public static long[] keysToLongArray(SortedMap map) {
2399         Set keySet = map.keySet();
2400         long[] retval = new long[keySet.size()];
2401         int i = 0;
2402         for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) {
2403             retval[i] = ((Long)iter.next()).longValue();
2404         }
2405         return retval;
2406     }
2407 
2408     /**
2409      * Helper function to reduce a Map with DataFlavor keys to a DataFlavor
2410      * array. The array will be sorted according to
2411      * <code>DataFlavorComparator</code>.
2412      */
2413     public static DataFlavor[] keysToDataFlavorArray(Map map) {
2414         return setToSortedDataFlavorArray(map.keySet(), map);
2415     }
2416 
2417     /**
2418      * Helper function to convert a Set of DataFlavors to a sorted array.
2419      * The array will be sorted according to <code>DataFlavorComparator</code>.
2420      */
2421     public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) {
2422         DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
2423         flavorsSet.toArray(flavors);
2424         final Comparator comparator =
2425                 new DataFlavorComparator(IndexedComparator.SELECT_WORST);
2426         Arrays.sort(flavors, comparator);
2427         return flavors;
2428     }
2429 
2430     /**
2431      * Helper function to convert a Set of DataFlavors to a sorted array.
2432      * The array will be sorted according to a
2433      * <code>DataFlavorComparator</code> created with the specified
2434      * flavor-to-native map as an argument.
2435      */
2436     public static DataFlavor[] setToSortedDataFlavorArray
2437         (Set flavorsSet, Map flavorToNativeMap)
2438     {
2439         DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
2440         flavorsSet.toArray(flavors);
2441         Comparator comparator =
2442             new DataFlavorComparator(flavorToNativeMap,
2443                                      IndexedComparator.SELECT_WORST);
2444         Arrays.sort(flavors, comparator);
2445         return flavors;
2446     }
2447 
2448     /**
2449      * Helper function to convert an InputStream to a byte[] array.
2450      */
2451     protected static byte[] inputStreamToByteArray(InputStream str)
2452         throws IOException
2453     {
2454         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
2455             int len = 0;
2456             byte[] buf = new byte[8192];
2457 
2458             while ((len = str.read(buf)) != -1) {
2459                 baos.write(buf, 0, len);
2460             }
2461 
2462             return baos.toByteArray();
2463         }
2464     }
2465 
2466     /**
2467      * Returns platform-specific mappings for the specified native.
2468      * If there are no platform-specific mappings for this native, the method


2707                 // Only convert to canonical form if the charset is one
2708                 // of the charsets explicitly listed in the known charsets
2709                 // map. This will happen only for Unicode, ASCII, or default
2710                 // charsets.
2711                 String canonicalName = DataTransferer.canonicalName(charset);
2712                 return (charsets.containsKey(canonicalName))
2713                     ? canonicalName
2714                     : charset;
2715             }
2716         }
2717     }
2718 
2719     /**
2720      * An IndexedComparator which compares two DataFlavors. For text flavors,
2721      * the comparison follows the rules outlined in
2722      * DataFlavor.selectBestTextFlavor. For non-text flavors, unknown
2723      * application MIME types are preferred, followed by known
2724      * application/x-java-* MIME types. Unknown application types are preferred
2725      * because if the user provides his own data flavor, it will likely be the
2726      * most descriptive one. For flavors which are otherwise equal, the
2727      * flavors' native formats are compared, with greater long values
2728      * taking precedence.
2729      */
2730     public static class DataFlavorComparator extends IndexedComparator {
2731         protected final Map flavorToFormatMap;
2732 
2733         private final CharsetComparator charsetComparator;
2734 
2735         private static final Map exactTypes;
2736         private static final Map primaryTypes;
2737         private static final Map nonTextRepresentations;
2738         private static final Map textTypes;
2739         private static final Map decodedTextRepresentations;
2740         private static final Map encodedTextRepresentations;
2741 
2742         private static final Integer UNKNOWN_OBJECT_LOSES =
2743             Integer.valueOf(Integer.MIN_VALUE);
2744         private static final Integer UNKNOWN_OBJECT_WINS =
2745             Integer.valueOf(Integer.MAX_VALUE);
2746 
2747         private static final Long UNKNOWN_OBJECT_LOSES_L =
2748             Long.valueOf(Long.MIN_VALUE);
2749         private static final Long UNKNOWN_OBJECT_WINS_L =
2750             Long.valueOf(Long.MAX_VALUE);
2751 


2847                 encodedTextRepresentationsMap.put
2848                     (DataTransferer.byteArrayClass, Integer.valueOf(0));
2849                 encodedTextRepresentationsMap.put
2850                     (java.nio.ByteBuffer.class, Integer.valueOf(1));
2851                 encodedTextRepresentationsMap.put
2852                     (java.io.InputStream.class, Integer.valueOf(2));
2853 
2854                 encodedTextRepresentations =
2855                     Collections.unmodifiableMap(encodedTextRepresentationsMap);
2856             }
2857         }
2858 
2859         public DataFlavorComparator() {
2860             this(SELECT_BEST);
2861         }
2862 
2863         public DataFlavorComparator(boolean order) {
2864             super(order);
2865 
2866             charsetComparator = new CharsetComparator(order);
2867             flavorToFormatMap = Collections.EMPTY_MAP;
2868         }
2869 
2870         public DataFlavorComparator(Map map) {
2871             this(map, SELECT_BEST);
2872         }
2873 
2874         public DataFlavorComparator(Map map, boolean order) {
2875             super(order);
2876 
2877             charsetComparator = new CharsetComparator(order);
2878             HashMap hashMap = new HashMap(map.size());
2879             hashMap.putAll(map);
2880             flavorToFormatMap = Collections.unmodifiableMap(hashMap);
2881         }
2882 
2883         public int compare(Object obj1, Object obj2) {
2884             DataFlavor flavor1 = null;
2885             DataFlavor flavor2 = null;
2886             if (order == SELECT_BEST) {
2887                 flavor1 = (DataFlavor)obj1;
2888                 flavor2 = (DataFlavor)obj2;
2889             } else {
2890                 flavor1 = (DataFlavor)obj2;
2891                 flavor2 = (DataFlavor)obj1;
2892             }
2893 
2894             if (flavor1.equals(flavor2)) {
2895                 return 0;
2896             }
2897 
2898             int comp = 0;
2899 
2900             String primaryType1 = flavor1.getPrimaryType();


2956                 }
2957 
2958                 // Next, look for application/x-java-* types. Prefer unknown
2959                 // MIME types because if the user provides his own data flavor,
2960                 // it will likely be the most descriptive one.
2961                 comp = compareIndices(exactTypes, mimeType1, mimeType2,
2962                                       UNKNOWN_OBJECT_WINS);
2963                 if (comp != 0) {
2964                     return comp;
2965                 }
2966 
2967                 // Finally, prefer the representation classes of Remote,
2968                 // Serializable, and InputStream, in that order.
2969                 comp = compareIndices(nonTextRepresentations, class1, class2,
2970                                       UNKNOWN_OBJECT_LOSES);
2971                 if (comp != 0) {
2972                     return comp;
2973                 }
2974             }
2975 
2976             // As a last resort, take the DataFlavor with the greater integer
2977             // format.
2978             return compareLongs(flavorToFormatMap, flavor1, flavor2,
2979                                 UNKNOWN_OBJECT_LOSES_L);
2980         }
2981     }
2982 
2983     /*
2984      * Given the Map that maps objects to Integer indices and a boolean value,
2985      * this Comparator imposes a direct or reverse order on set of objects.
2986      * <p>
2987      * If the specified boolean value is SELECT_BEST, the Comparator imposes the
2988      * direct index-based order: an object A is greater than an object B if and
2989      * only if the index of A is greater than the index of B. An object that
2990      * doesn't have an associated index is less or equal than any other object.
2991      * <p>
2992      * If the specified boolean value is SELECT_WORST, the Comparator imposes the
2993      * reverse index-based order: an object A is greater than an object B if and
2994      * only if A is less than B with the direct index-based order.
2995      */
2996     public static class IndexOrderComparator extends IndexedComparator {
2997         private final Map indexMap;
2998         private static final Integer FALLBACK_INDEX =
2999             Integer.valueOf(Integer.MIN_VALUE);




2389     public abstract ToolkitThreadBlockedHandler
2390         getToolkitThreadBlockedHandler();
2391 
2392     /**
2393      * Helper function to reduce a Map with Long keys to a long array.
2394      * <p>
2395      * The map keys are sorted according to the native formats preference
2396      * order.
2397      */
2398     public static long[] keysToLongArray(SortedMap map) {
2399         Set keySet = map.keySet();
2400         long[] retval = new long[keySet.size()];
2401         int i = 0;
2402         for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) {
2403             retval[i] = ((Long)iter.next()).longValue();
2404         }
2405         return retval;
2406     }
2407 
2408     /**









2409      * Helper function to convert a Set of DataFlavors to a sorted array.
2410      * The array will be sorted according to <code>DataFlavorComparator</code>.
2411      */
2412     public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) {
2413         DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
2414         flavorsSet.toArray(flavors);
2415         final Comparator comparator =
2416                 new DataFlavorComparator(IndexedComparator.SELECT_WORST);
2417         Arrays.sort(flavors, comparator);
2418         return flavors;
2419     }
2420 
2421     /**


















2422      * Helper function to convert an InputStream to a byte[] array.
2423      */
2424     protected static byte[] inputStreamToByteArray(InputStream str)
2425         throws IOException
2426     {
2427         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
2428             int len = 0;
2429             byte[] buf = new byte[8192];
2430 
2431             while ((len = str.read(buf)) != -1) {
2432                 baos.write(buf, 0, len);
2433             }
2434 
2435             return baos.toByteArray();
2436         }
2437     }
2438 
2439     /**
2440      * Returns platform-specific mappings for the specified native.
2441      * If there are no platform-specific mappings for this native, the method


2680                 // Only convert to canonical form if the charset is one
2681                 // of the charsets explicitly listed in the known charsets
2682                 // map. This will happen only for Unicode, ASCII, or default
2683                 // charsets.
2684                 String canonicalName = DataTransferer.canonicalName(charset);
2685                 return (charsets.containsKey(canonicalName))
2686                     ? canonicalName
2687                     : charset;
2688             }
2689         }
2690     }
2691 
2692     /**
2693      * An IndexedComparator which compares two DataFlavors. For text flavors,
2694      * the comparison follows the rules outlined in
2695      * DataFlavor.selectBestTextFlavor. For non-text flavors, unknown
2696      * application MIME types are preferred, followed by known
2697      * application/x-java-* MIME types. Unknown application types are preferred
2698      * because if the user provides his own data flavor, it will likely be the
2699      * most descriptive one. For flavors which are otherwise equal, the
2700      * flavors' string representation are compared in the alphabetical order.

2701      */
2702     public static class DataFlavorComparator extends IndexedComparator {

2703 
2704         private final CharsetComparator charsetComparator;
2705 
2706         private static final Map exactTypes;
2707         private static final Map primaryTypes;
2708         private static final Map nonTextRepresentations;
2709         private static final Map textTypes;
2710         private static final Map decodedTextRepresentations;
2711         private static final Map encodedTextRepresentations;
2712 
2713         private static final Integer UNKNOWN_OBJECT_LOSES =
2714             Integer.valueOf(Integer.MIN_VALUE);
2715         private static final Integer UNKNOWN_OBJECT_WINS =
2716             Integer.valueOf(Integer.MAX_VALUE);
2717 
2718         private static final Long UNKNOWN_OBJECT_LOSES_L =
2719             Long.valueOf(Long.MIN_VALUE);
2720         private static final Long UNKNOWN_OBJECT_WINS_L =
2721             Long.valueOf(Long.MAX_VALUE);
2722 


2818                 encodedTextRepresentationsMap.put
2819                     (DataTransferer.byteArrayClass, Integer.valueOf(0));
2820                 encodedTextRepresentationsMap.put
2821                     (java.nio.ByteBuffer.class, Integer.valueOf(1));
2822                 encodedTextRepresentationsMap.put
2823                     (java.io.InputStream.class, Integer.valueOf(2));
2824 
2825                 encodedTextRepresentations =
2826                     Collections.unmodifiableMap(encodedTextRepresentationsMap);
2827             }
2828         }
2829 
2830         public DataFlavorComparator() {
2831             this(SELECT_BEST);
2832         }
2833 
2834         public DataFlavorComparator(boolean order) {
2835             super(order);
2836 
2837             charsetComparator = new CharsetComparator(order);














2838         }
2839 
2840         public int compare(Object obj1, Object obj2) {
2841             DataFlavor flavor1 = null;
2842             DataFlavor flavor2 = null;
2843             if (order == SELECT_BEST) {
2844                 flavor1 = (DataFlavor)obj1;
2845                 flavor2 = (DataFlavor)obj2;
2846             } else {
2847                 flavor1 = (DataFlavor)obj2;
2848                 flavor2 = (DataFlavor)obj1;
2849             }
2850 
2851             if (flavor1.equals(flavor2)) {
2852                 return 0;
2853             }
2854 
2855             int comp = 0;
2856 
2857             String primaryType1 = flavor1.getPrimaryType();


2913                 }
2914 
2915                 // Next, look for application/x-java-* types. Prefer unknown
2916                 // MIME types because if the user provides his own data flavor,
2917                 // it will likely be the most descriptive one.
2918                 comp = compareIndices(exactTypes, mimeType1, mimeType2,
2919                                       UNKNOWN_OBJECT_WINS);
2920                 if (comp != 0) {
2921                     return comp;
2922                 }
2923 
2924                 // Finally, prefer the representation classes of Remote,
2925                 // Serializable, and InputStream, in that order.
2926                 comp = compareIndices(nonTextRepresentations, class1, class2,
2927                                       UNKNOWN_OBJECT_LOSES);
2928                 if (comp != 0) {
2929                     return comp;
2930                 }
2931             }
2932 
2933             // The flavours are not equal but still not distinguishable.
2934             // Compare String representations in alphabetical order
2935             return flavor1.getMimeType().compareTo(flavor2.getMimeType());

2936         }
2937     }
2938 
2939     /*
2940      * Given the Map that maps objects to Integer indices and a boolean value,
2941      * this Comparator imposes a direct or reverse order on set of objects.
2942      * <p>
2943      * If the specified boolean value is SELECT_BEST, the Comparator imposes the
2944      * direct index-based order: an object A is greater than an object B if and
2945      * only if the index of A is greater than the index of B. An object that
2946      * doesn't have an associated index is less or equal than any other object.
2947      * <p>
2948      * If the specified boolean value is SELECT_WORST, the Comparator imposes the
2949      * reverse index-based order: an object A is greater than an object B if and
2950      * only if A is less than B with the direct index-based order.
2951      */
2952     public static class IndexOrderComparator extends IndexedComparator {
2953         private final Map indexMap;
2954         private static final Integer FALLBACK_INDEX =
2955             Integer.valueOf(Integer.MIN_VALUE);