# HG changeset patch # User pchelko # Date 1391163640 -14400 # Node ID 16c1ddb7b66af933bb921d57e98e30c4c5062fc3 # Parent 6369264caa4ae173f80214eed79b576fe469cd25 8030093: Generify DataTrasfer classes Reviewed-by: anthony, serb diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java --- a/jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java Fri Jan 31 14:20:40 2014 +0400 @@ -743,7 +743,7 @@ private static Comparator textFlavorComparator; static class TextFlavorComparator - extends DataTransferer.DataFlavorComparator { + extends DataTransferer.DataFlavorComparator { /** * Compares two DataFlavor objects. Returns a negative @@ -754,8 +754,8 @@ * DataFlavors are ordered according to the rules outlined * for selectBestTextFlavor. * - * @param obj1 the first DataFlavor to be compared - * @param obj2 the second DataFlavor to be compared + * @param flavor1 the first DataFlavor to be compared + * @param flavor2 the second DataFlavor to be compared * @return a negative integer, zero, or a positive integer as the first * argument is worse, equal to, or better than the second * @throws ClassCastException if either of the arguments is not an @@ -765,13 +765,10 @@ * * @see #selectBestTextFlavor */ - public int compare(Object obj1, Object obj2) { - DataFlavor flavor1 = (DataFlavor)obj1; - DataFlavor flavor2 = (DataFlavor)obj2; - + public int compare(DataFlavor flavor1, DataFlavor flavor2) { if (flavor1.isFlavorTextType()) { if (flavor2.isFlavorTextType()) { - return super.compare(obj1, obj2); + return super.compare(flavor1, flavor2); } else { return 1; } diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java --- a/jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java Fri Jan 31 14:20:40 2014 +0400 @@ -544,7 +544,7 @@ DataTransferer transferer = DataTransferer.getInstance(); if (transferer != null) { List platformFlavors = - transferer.getPlatformMappingsForNative(nat); + transferer.getPlatformMappingsForNative(nat); if (!platformFlavors.isEmpty()) { if (flavors != null) { platformFlavors.removeAll(new HashSet<>(flavors)); diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java --- a/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java Fri Jan 31 14:20:40 2014 +0400 @@ -32,7 +32,6 @@ import java.io.IOException; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; @@ -55,7 +54,7 @@ * @since 1.4 (appeared in modified form as FullyRenderedTransferable in 1.3.1) */ public class ClipboardTransferable implements Transferable { - private final HashMap flavorsToData = new HashMap(); + private final Map flavorsToData = new HashMap<>(); private DataFlavor[] flavors = new DataFlavor[0]; private final class DataFactory { @@ -84,20 +83,13 @@ // Since the SystemFlavorMap will specify many DataFlavors // which map to the same format, we should cache data as we // read it. - HashMap cached_data = new HashMap(formats.length, 1.0f); + Map cached_data = new HashMap<>(formats.length, 1.0f); + DataTransferer.getInstance() + .getFlavorsForFormats(formats, SunClipboard.flavorMap) + .entrySet() + .forEach(entry -> fetchOneFlavor(clipboard, entry.getKey(), entry.getValue(), cached_data)); + flavors = DataTransferer.setToSortedDataFlavorArray(flavorsToData.keySet()); - Map flavorsForFormats = DataTransferer.getInstance(). - getFlavorsForFormats(formats, SunClipboard.flavorMap); - for (Iterator iter = flavorsForFormats.keySet().iterator(); - iter.hasNext(); ) - { - DataFlavor flavor = (DataFlavor)iter.next(); - Long lFormat = (Long)flavorsForFormats.get(flavor); - - fetchOneFlavor(clipboard, flavor, lFormat, cached_data); - } - - flavors = DataTransferer.setToSortedDataFlavorArray(flavorsToData.keySet()); } } finally { clipboard.closeClipboard(); @@ -105,13 +97,12 @@ } private boolean fetchOneFlavor(SunClipboard clipboard, DataFlavor flavor, - Long lFormat, HashMap cached_data) + long format, Map cached_data) { if (!flavorsToData.containsKey(flavor)) { - long format = lFormat.longValue(); Object data = null; - if (!cached_data.containsKey(lFormat)) { + if (!cached_data.containsKey(format)) { try { data = clipboard.getClipboardData(format); } catch (IOException e) { @@ -122,9 +113,9 @@ // Cache this data, even if it's null, so we don't have to go // to native code again for this format. - cached_data.put(lFormat, data); + cached_data.put(format, data); } else { - data = cached_data.get(lFormat); + data = cached_data.get(format); } // Casting IOException to byte array causes ClassCastException. @@ -134,8 +125,7 @@ flavorsToData.put(flavor, data); return false; } else if (data != null) { - flavorsToData.put(flavor, new DataFactory(format, - (byte[])data)); + flavorsToData.put(flavor, new DataFactory(format, (byte[])data)); return true; } } @@ -143,14 +133,17 @@ return false; } + @Override public DataFlavor[] getTransferDataFlavors() { return flavors.clone(); } + @Override public boolean isDataFlavorSupported(DataFlavor flavor) { return flavorsToData.containsKey(flavor); } + @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java --- a/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java Fri Jan 31 14:20:40 2014 +0400 @@ -70,6 +70,7 @@ import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -112,6 +113,7 @@ import sun.awt.image.ToolkitImage; import java.io.FilePermission; +import java.util.stream.Stream; /** @@ -136,17 +138,6 @@ * @since 1.3.1 */ public abstract class DataTransferer { - - /** - * The DataFlavor representing plain text with Unicode - * encoding, where: - *
-     *     representationClass = java.lang.String
-     *     mimeType            = "text/plain; charset=Unicode"
-     * 
- */ - public static final DataFlavor plainTextStringFlavor; - /** * The DataFlavor representing a Java text encoding String * encoded in UTF-8, where @@ -164,9 +155,9 @@ private static final SortedSet standardEncodings = load(); private static SortedSet load() { - final Comparator comparator = + final Comparator comparator = new CharsetComparator(IndexedComparator.SELECT_WORST); - final SortedSet tempSet = new TreeSet(comparator); + final SortedSet tempSet = new TreeSet<>(comparator); tempSet.add("US-ASCII"); tempSet.add("ISO-8859-1"); tempSet.add("UTF-8"); @@ -184,7 +175,7 @@ * listed in the DataFlavor.selectBestTextFlavor method comment. Additional * entries may be added during the life of the JRE for text/ types. */ - private static final Map textMIMESubtypeCharsetSupport; + private static final Map textMIMESubtypeCharsetSupport; /** * Cache of the platform default encoding as specified in the @@ -196,58 +187,43 @@ * A collection of all natives listed in flavormap.properties with * a primary MIME type of "text". */ - private static final Set textNatives = - Collections.synchronizedSet(new HashSet()); + private static final Set textNatives = + Collections.synchronizedSet(new HashSet<>()); /** * The native encodings/charsets for the Set of textNatives. */ - private static final Map nativeCharsets = - Collections.synchronizedMap(new HashMap()); + private static final Map nativeCharsets = + Collections.synchronizedMap(new HashMap<>()); /** * The end-of-line markers for the Set of textNatives. */ - private static final Map nativeEOLNs = - Collections.synchronizedMap(new HashMap()); + private static final Map nativeEOLNs = + Collections.synchronizedMap(new HashMap<>()); /** * The number of terminating NUL bytes for the Set of textNatives. */ - private static final Map nativeTerminators = - Collections.synchronizedMap(new HashMap()); + private static final Map nativeTerminators = + Collections.synchronizedMap(new HashMap<>()); /** * The key used to store pending data conversion requests for an AppContext. */ private static final String DATA_CONVERTER_KEY = "DATA_CONVERTER_KEY"; - /** - * The singleton DataTransferer instance. It is created during MToolkit - * or WToolkit initialization. - */ - private static DataTransferer transferer; - private static final PlatformLogger dtLog = PlatformLogger.getLogger("sun.awt.datatransfer.DataTransfer"); static { - DataFlavor tPlainTextStringFlavor = null; - try { - tPlainTextStringFlavor = new DataFlavor - ("text/plain;charset=Unicode;class=java.lang.String"); - } catch (ClassNotFoundException cannotHappen) { - } - plainTextStringFlavor = tPlainTextStringFlavor; - DataFlavor tJavaTextEncodingFlavor = null; try { - tJavaTextEncodingFlavor = new DataFlavor - ("application/x-java-text-encoding;class=\"[B\""); + tJavaTextEncodingFlavor = new DataFlavor("application/x-java-text-encoding;class=\"[B\""); } catch (ClassNotFoundException cannotHappen) { } javaTextEncodingFlavor = tJavaTextEncodingFlavor; - Map tempMap = new HashMap(17); + Map tempMap = new HashMap<>(17); tempMap.put("sgml", Boolean.TRUE); tempMap.put("xml", Boolean.TRUE); tempMap.put("html", Boolean.TRUE); @@ -335,29 +311,27 @@ return false; } - Object support = textMIMESubtypeCharsetSupport.get(subType); + Boolean support = textMIMESubtypeCharsetSupport.get(subType); if (support != null) { - return (support == Boolean.TRUE); + return support; } boolean ret_val = (flavor.getParameter("charset") != null); - textMIMESubtypeCharsetSupport.put - (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE); + textMIMESubtypeCharsetSupport.put(subType, ret_val); return ret_val; } public static boolean doesSubtypeSupportCharset(String subType, String charset) { - Object support = textMIMESubtypeCharsetSupport.get(subType); + Boolean support = textMIMESubtypeCharsetSupport.get(subType); if (support != null) { - return (support == Boolean.TRUE); + return support; } boolean ret_val = (charset != null); - textMIMESubtypeCharsetSupport.put - (subType, (ret_val) ? Boolean.TRUE : Boolean.FALSE); + textMIMESubtypeCharsetSupport.put(subType, ret_val); return ret_val; } @@ -423,7 +397,7 @@ * Determines whether this JRE can both encode and decode text in the * specified encoding. */ - public static boolean isEncodingSupported(String encoding) { + private static boolean isEncodingSupported(String encoding) { if (encoding == null) { return false; } @@ -461,37 +435,35 @@ } return new FlavorTable() { - public Map getNativesForFlavors(DataFlavor[] flavors) { - return map.getNativesForFlavors(flavors); - } - public Map getFlavorsForNatives(String[] natives) { - return map.getFlavorsForNatives(natives); + @Override + public Map getNativesForFlavors(DataFlavor[] flavors) { + return map.getNativesForFlavors(flavors); + } + @Override + public Map getFlavorsForNatives(String[] natives) { + return map.getFlavorsForNatives(natives); + } + @Override + public List getNativesForFlavor(DataFlavor flav) { + Map natives = getNativesForFlavors(new DataFlavor[]{flav}); + String nat = natives.get(flav); + if (nat != null) { + return Collections.singletonList(nat); + } else { + return Collections.emptyList(); } - public List getNativesForFlavor(DataFlavor flav) { - Map natives = - getNativesForFlavors(new DataFlavor[] { flav } ); - String nat = (String)natives.get(flav); - if (nat != null) { - List list = new ArrayList(1); - list.add(nat); - return list; - } else { - return Collections.EMPTY_LIST; - } + } + @Override + public List getFlavorsForNative(String nat) { + Map flavors = getFlavorsForNatives(new String[]{nat}); + DataFlavor flavor = flavors.get(nat); + if (flavor != null) { + return Collections.singletonList(flavor); + } else { + return Collections.emptyList(); } - public List getFlavorsForNative(String nat) { - Map flavors = - getFlavorsForNatives(new String[] { nat } ); - DataFlavor flavor = (DataFlavor)flavors.get(nat); - if (flavor != null) { - List list = new ArrayList(1); - list.add(flavor); - return list; - } else { - return Collections.EMPTY_LIST; - } - } - }; + } + }; } /** @@ -513,13 +485,13 @@ textNatives.add(format); nativeCharsets.put(format, (charset != null && charset.length() != 0) - ? charset : getDefaultTextCharset()); + ? charset : getDefaultTextCharset()); if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) { nativeEOLNs.put(format, eoln); } if (terminators != null && terminators.length() != 0) { Integer iTerminators = Integer.valueOf(terminators); - if (iTerminators.intValue() > 0) { + if (iTerminators > 0) { nativeTerminators.put(format, iTerminators); } } @@ -534,7 +506,7 @@ } protected String getCharsetForTextFormat(Long lFormat) { - return (String)nativeCharsets.get(lFormat); + return nativeCharsets.get(lFormat); } /** @@ -574,32 +546,18 @@ * The map keys are sorted according to the native formats preference * order. */ - public SortedMap getFormatsForTransferable( - Transferable contents, FlavorTable map) + public SortedMap getFormatsForTransferable(Transferable contents, + FlavorTable map) { DataFlavor[] flavors = contents.getTransferDataFlavors(); if (flavors == null) { - return new TreeMap(); + return Collections.emptySortedMap(); } return getFormatsForFlavors(flavors, map); } /** * Returns a Map whose keys are all of the possible formats into which data - * in the specified DataFlavor can be translated. The value of each key - * is the DataFlavor in which a Transferable's data should be requested - * when converting to the format. - *

- * The map keys are sorted according to the native formats preference - * order. - */ - public SortedMap getFormatsForFlavor(DataFlavor flavor, FlavorTable map) { - return getFormatsForFlavors(new DataFlavor[] { flavor }, - map); - } - - /** - * Returns a Map whose keys are all of the possible formats into which data * in the specified DataFlavors can be translated. The value of each key * is the DataFlavor in which the Transferable's data should be requested * when converting to the format. @@ -612,18 +570,16 @@ * DataFlavors and data formats * @throws NullPointerException if flavors or map is null */ - public SortedMap getFormatsForFlavors( - DataFlavor[] flavors, FlavorTable map) + public SortedMap getFormatsForFlavors(DataFlavor[] flavors, + FlavorTable map) { - Map formatMap = - new HashMap <> (flavors.length); - Map textPlainMap = - new HashMap <> (flavors.length); + Map formatMap = new HashMap<>(flavors.length); + Map textPlainMap = new HashMap<>(flavors.length); // Maps formats to indices that will be used to sort the formats // according to the preference order. // Larger index value corresponds to the more preferable format. - Map indexMap = new HashMap(flavors.length); - Map textPlainIndexMap = new HashMap(flavors.length); + Map indexMap = new HashMap<>(flavors.length); + Map textPlainIndexMap = new HashMap<>(flavors.length); int currentIndex = 0; @@ -643,14 +599,13 @@ flavor.isRepresentationClassInputStream() || flavor.isRepresentationClassRemote()) { - List natives = map.getNativesForFlavor(flavor); + List natives = map.getNativesForFlavor(flavor); currentIndex += natives.size(); - for (Iterator iter = natives.iterator(); iter.hasNext(); ) { - Long lFormat = - getFormatForNativeAsLong((String)iter.next()); - Integer index = Integer.valueOf(currentIndex--); + for (String aNative : natives) { + Long lFormat = getFormatForNativeAsLong(aNative); + Integer index = currentIndex--; formatMap.put(lFormat, flavor); indexMap.put(lFormat, index); @@ -660,9 +615,8 @@ // for a single text/* flavor, we would prefer that // text/plain native data come from a text/plain flavor. if (("text".equals(flavor.getPrimaryType()) && - "plain".equals(flavor.getSubType())) || - flavor.equals(DataFlavor.stringFlavor)) - { + "plain".equals(flavor.getSubType())) || + flavor.equals(DataFlavor.stringFlavor)) { textPlainMap.put(lFormat, flavor); textPlainIndexMap.put(lFormat, index); } @@ -676,9 +630,9 @@ indexMap.putAll(textPlainIndexMap); // Sort the map keys according to the formats preference order. - Comparator comparator = - new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST); - SortedMap sortedMap = new TreeMap(comparator); + Comparator comparator = + new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST); + SortedMap sortedMap = new TreeMap<>(comparator); sortedMap.putAll(formatMap); return sortedMap; @@ -692,24 +646,6 @@ FlavorTable map) { return keysToLongArray(getFormatsForTransferable(contents, map)); } - public long[] getFormatsForFlavorAsArray(DataFlavor flavor, - FlavorTable map) { - return keysToLongArray(getFormatsForFlavor(flavor, map)); - } - public long[] getFormatsForFlavorsAsArray(DataFlavor[] flavors, - FlavorTable map) { - return keysToLongArray(getFormatsForFlavors(flavors, map)); - } - - /** - * Returns a Map whose keys are all of the possible DataFlavors into which - * data in the specified format can be translated. The value of each key - * is the format in which the Clipboard or dropped data should be requested - * when converting to the DataFlavor. - */ - public Map getFlavorsForFormat(long format, FlavorTable map) { - return getFlavorsForFormats(new long[] { format }, map); - } /** * Returns a Map whose keys are all of the possible DataFlavors into which @@ -717,10 +653,10 @@ * is the format in which the Clipboard or dropped data should be requested * when converting to the DataFlavor. */ - public Map getFlavorsForFormats(long[] formats, FlavorTable map) { - Map flavorMap = new HashMap(formats.length); - Set mappingSet = new HashSet(formats.length); - Set flavorSet = new HashSet(formats.length); + public Map getFlavorsForFormats(long[] formats, FlavorTable map) { + Map flavorMap = new HashMap<>(formats.length); + Set> mappingSet = new HashSet<>(formats.length); + Set flavorSet = new HashSet<>(formats.length); // First step: build flavorSet, mappingSet and initial flavorMap // flavorSet - the set of all the DataFlavors into which @@ -729,27 +665,22 @@ // into any DataFlavor; // flavorMap - after this step, this map maps each of the DataFlavors // from flavorSet to any of the specified formats. - for (int i = 0; i < formats.length; i++) { - long format = formats[i]; + for (long format : formats) { String nat = getNativeForFormat(format); - List flavors = map.getFlavorsForNative(nat); - - for (Iterator iter = flavors.iterator(); iter.hasNext(); ) { - DataFlavor flavor = (DataFlavor)iter.next(); - + List flavors = map.getFlavorsForNative(nat); + for (DataFlavor flavor : flavors) { // Don't explicitly test for String, since it is just a special // case of Serializable if (flavor.isFlavorTextType() || - flavor.isFlavorJavaFileListType() || - DataFlavor.imageFlavor.equals(flavor) || - flavor.isRepresentationClassSerializable() || - flavor.isRepresentationClassInputStream() || - flavor.isRepresentationClassRemote()) - { - Long lFormat = Long.valueOf(format); - Object mapping = - DataTransferer.createMapping(lFormat, flavor); - flavorMap.put(flavor, lFormat); + flavor.isFlavorJavaFileListType() || + DataFlavor.imageFlavor.equals(flavor) || + flavor.isRepresentationClassSerializable() || + flavor.isRepresentationClassInputStream() || + flavor.isRepresentationClassRemote()) { + + AbstractMap.SimpleEntry mapping = + new AbstractMap.SimpleEntry<>(format, flavor); + flavorMap.put(flavor, format); mappingSet.add(mapping); flavorSet.add(flavor); } @@ -769,19 +700,11 @@ // a mappingSet of all format-to-flavor mappings for the specified formats // and check if the format-to-flavor mapping exists for the // (flavor,format) pair being added. - for (Iterator flavorIter = flavorSet.iterator(); - flavorIter.hasNext(); ) { - DataFlavor flavor = (DataFlavor)flavorIter.next(); - - List natives = map.getNativesForFlavor(flavor); - - for (Iterator nativeIter = natives.iterator(); - nativeIter.hasNext(); ) { - Long lFormat = - getFormatForNativeAsLong((String)nativeIter.next()); - Object mapping = DataTransferer.createMapping(lFormat, flavor); - - if (mappingSet.contains(mapping)) { + for (DataFlavor flavor : flavorSet) { + List natives = map.getNativesForFlavor(flavor); + for (String aNative : natives) { + Long lFormat = getFormatForNativeAsLong(aNative); + if (mappingSet.contains(new AbstractMap.SimpleEntry<>(lFormat, flavor))) { flavorMap.put(flavor, lFormat); break; } @@ -804,24 +727,19 @@ * @throws NullPointerException if formats or map is null */ public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) { - Set flavorSet = new HashSet(formats.length); - - for (int i = 0; i < formats.length; i++) { - String nat = getNativeForFormat(formats[i]); - List flavors = map.getFlavorsForNative(nat); - - for (Iterator iter = flavors.iterator(); iter.hasNext(); ) { - DataFlavor flavor = (DataFlavor)iter.next(); - + Set flavorSet = new HashSet<>(formats.length); + + for (long format : formats) { + List flavors = map.getFlavorsForNative(getNativeForFormat(format)); + for (DataFlavor flavor : flavors) { // Don't explicitly test for String, since it is just a special // case of Serializable if (flavor.isFlavorTextType() || - flavor.isFlavorJavaFileListType() || - DataFlavor.imageFlavor.equals(flavor) || - flavor.isRepresentationClassSerializable() || - flavor.isRepresentationClassInputStream() || - flavor.isRepresentationClassRemote()) - { + flavor.isFlavorJavaFileListType() || + DataFlavor.imageFlavor.equals(flavor) || + flavor.isRepresentationClassSerializable() || + flavor.isRepresentationClassInputStream() || + flavor.isRepresentationClassRemote()) { flavorSet.add(flavor); } } @@ -832,25 +750,6 @@ /** * Returns an array of all DataFlavors for which - * 1) a mapping from the specified format exists in the specified map and - * 2) the data translation for this mapping can be performed by the data - * transfer subsystem. - * The array will be sorted according to a - * DataFlavorComparator created with the specified - * map as an argument. - * - * @param format the data format - * @param map the FlavorTable which contains mappings between - * DataFlavors and data formats - * @throws NullPointerException if map is null - */ - public DataFlavor[] getFlavorsForFormatAsArray(long format, - FlavorTable map) { - return getFlavorsForFormatsAsArray(new long[] { format }, map); - } - - /** - * Returns an array of all DataFlavors for which * 1) a mapping from at least one of the specified formats exists in the * specified map and * 2) the data translation for this mapping can be performed by the data @@ -872,22 +771,6 @@ } /** - * Returns an object that represents a mapping between the specified - * key and value. null values and the null keys are - * permitted. The internal representation of the mapping object is - * irrelevant. The only requrement is that the two mapping objects are equal - * if and only if their keys are equal and their values are equal. - * More formally, the two mapping objects are equal if and only if - * (value1 == null ? value2 == null : value1.equals(value2)) - * && (key1 == null ? key2 == null : key1.equals(key2)). - */ - private static Object createMapping(Object key, Object value) { - // NOTE: Should be updated to use AbstractMap.SimpleEntry as - // soon as it is made public. - return Arrays.asList(new Object[] { key, value }); - } - - /** * Looks-up or registers the String native with the native data transfer * system and returns a long format corresponding to that native. */ @@ -938,17 +821,16 @@ private byte[] translateTransferableString(String str, long format) throws IOException { - Long lFormat = Long.valueOf(format); + Long lFormat = format; String charset = getBestCharsetForTextFormat(lFormat, null); // Search and replace EOLN. Note that if EOLN is "\n", then we // never added an entry to nativeEOLNs anyway, so we'll skip this // code altogether. // windows: "abc\nde"->"abc\r\nde" - String eoln = (String)nativeEOLNs.get(lFormat); + String eoln = nativeEOLNs.get(lFormat); if (eoln != null) { int length = str.length(); - StringBuffer buffer = - new StringBuffer(length * 2); // 2 is a heuristic + StringBuilder buffer = new StringBuilder(length * 2); // 2 is a heuristic for (int i = 0; i < length; i++) { // Fix for 4914613 - skip native EOLN if (str.startsWith(eoln, i)) { @@ -973,9 +855,9 @@ // the we never added an entry to nativeTerminators anyway, so // we'll skip code altogether. // "abcde" -> "abcde\0" - Integer terminators = (Integer)nativeTerminators.get(lFormat); + Integer terminators = nativeTerminators.get(lFormat); if (terminators != null) { - int numTerminators = terminators.intValue(); + int numTerminators = terminators; byte[] terminatedBytes = new byte[bytes.length + numTerminators]; System.arraycopy(bytes, 0, terminatedBytes, 0, bytes.length); @@ -998,7 +880,7 @@ throws IOException { - Long lFormat = Long.valueOf(format); + Long lFormat = format; String charset = getBestCharsetForTextFormat(lFormat, localeTransferable); // Locate terminating NUL bytes. Note that if terminators is 0, @@ -1011,11 +893,11 @@ // multibyte coding like UTF-8, but helps understand the procedure. // "abcde\0" -> "abcde" - String eoln = (String)nativeEOLNs.get(lFormat); - Integer terminators = (Integer)nativeTerminators.get(lFormat); + String eoln = nativeEOLNs.get(lFormat); + Integer terminators = nativeTerminators.get(lFormat); int count; if (terminators != null) { - int numTerminators = terminators.intValue(); + int numTerminators = terminators; search: for (count = 0; count < (bytes.length - numTerminators + 1); count += numTerminators) { for (int i = count; i < count + numTerminators; i++) { @@ -1048,7 +930,6 @@ char[] buf = converted.toCharArray(); char[] eoln_arr = eoln.toCharArray(); - converted = null; int j = 0; boolean match; @@ -1139,7 +1020,7 @@ ("cannot transfer non-text data as Reader"); } - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); try (Reader r = (Reader)obj) { int c; while ((c = r.read()) != -1) { @@ -1267,7 +1148,7 @@ final List list = (List)obj; final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents); final ArrayList fileList = castToFiles(list, userProtectionDomain); - final ArrayList uriList = new ArrayList(fileList.size()); + final ArrayList uriList = new ArrayList<>(fileList.size()); for (String fileObject : fileList) { final URI uri = new File(fileObject).toURI(); // Some implementations are fussy about the number of slashes (file:///path/to/file is best) @@ -1281,8 +1162,8 @@ byte[] eoln = "\r\n".getBytes(targetCharset); try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - for (int i = 0; i < uriList.size(); i++) { - byte[] bytes = uriList.get(i).getBytes(targetCharset); + for (String uri : uriList) { + byte[] bytes = uri.getBytes(targetCharset); bos.write(bytes, 0, bytes.length); bos.write(eoln, 0, eoln.length); } @@ -1358,42 +1239,35 @@ return str; } - - String ret_val = ""; final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents); try { - ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() { - public Object run() { - - StringBuffer allowedFiles = new StringBuffer(str.length()); - String [] uriArray = str.split("(\\s)+"); - - for (String fileName : uriArray) + return AccessController.doPrivileged((PrivilegedExceptionAction) () -> { + + StringBuilder allowedFiles = new StringBuilder(str.length()); + String [] uriArray = str.split("(\\s)+"); + + for (String fileName : uriArray) + { + File file = new File(fileName); + if (file.exists() && + !(isFileInWebstartedCache(file) || + isForbiddenToRead(file, userProtectionDomain))) + { + if (0 != allowedFiles.length()) { - File file = new File(fileName); - if (file.exists() && - !(isFileInWebstartedCache(file) || - isForbiddenToRead(file, userProtectionDomain))) - { - - if (0 != allowedFiles.length()) - { - allowedFiles.append("\\r\\n"); - } - - allowedFiles.append(fileName); - } + allowedFiles.append("\\r\\n"); } - return allowedFiles.toString(); + allowedFiles.append(fileName); } - }); + } + + return allowedFiles.toString(); + }); } catch (PrivilegedActionException pae) { throw new IOException(pae.getMessage(), pae); } - - return ret_val; } private static ProtectionDomain getUserProtectionDomain(Transferable contents) { @@ -1417,30 +1291,26 @@ } private ArrayList castToFiles(final List files, - final ProtectionDomain userProtectionDomain) throws IOException - { - final ArrayList fileList = new ArrayList(); + final ProtectionDomain userProtectionDomain) throws IOException { try { - AccessController.doPrivileged(new PrivilegedExceptionAction() { - public Object run() throws IOException { - for (Object fileObject : files) + return AccessController.doPrivileged((PrivilegedExceptionAction>) () -> { + ArrayList fileList = new ArrayList<>(); + for (Object fileObject : files) + { + File file = castToFile(fileObject); + if (file != null && + (null == System.getSecurityManager() || + !(isFileInWebstartedCache(file) || + isForbiddenToRead(file, userProtectionDomain)))) { - File file = castToFile(fileObject); - if (file != null && - (null == System.getSecurityManager() || - !(isFileInWebstartedCache(file) || - isForbiddenToRead(file, userProtectionDomain)))) - { - fileList.add(file.getCanonicalPath()); - } + fileList.add(file.getCanonicalPath()); } - return null; } + return fileList; }); } catch (PrivilegedActionException pae) { throw new IOException(pae.getMessage()); } - return fileList; } // It is important do not use user's successors @@ -1464,8 +1334,7 @@ "deployment.javapi.cachedir" }; - private final static ArrayList deploymentCacheDirectoryList = - new ArrayList(); + private final static ArrayList deploymentCacheDirectoryList = new ArrayList<>(); private static boolean isFileInWebstartedCache(File f) { @@ -1677,7 +1546,7 @@ if (uris == null) { return null; } - ArrayList files = new ArrayList(); + List files = new ArrayList<>(); for (URI uri : uris) { try { files.add(new File(uri)); @@ -1802,47 +1671,34 @@ Class clazz) throws IOException { - final Class dfrc = flavor.getRepresentationClass(); + final Class dfrc = flavor.getRepresentationClass(); if (clazz.equals(dfrc)) { return arg; // simple case } else { - Constructor[] constructors = null; + Constructor[] constructors; try { - constructors = (Constructor[]) - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return dfrc.getConstructors(); - } - }); + constructors = AccessController.doPrivileged( + (PrivilegedAction) dfrc::getConstructors); } catch (SecurityException se) { throw new IOException(se.getMessage()); } - Constructor constructor = null; - - for (int j = 0; j < constructors.length; j++) { - if (!Modifier.isPublic(constructors[j].getModifiers())) { - continue; - } - - Class[] ptypes = constructors[j].getParameterTypes(); - - if (ptypes != null && ptypes.length == 1 && - clazz.equals(ptypes[0])) { - constructor = constructors[j]; - break; - } - } - - if (constructor == null) { - throw new IOException("can't find (L"+ clazz + - ";)V for class: " + dfrc.getName()); - } + Constructor constructor = Stream.of(constructors) + .filter(c -> Modifier.isPublic(c.getModifiers())) + .filter(c -> { + Class[] ptypes = c.getParameterTypes(); + return ptypes != null + && ptypes.length == 1 + && clazz.equals(ptypes[0]); + }) + .findFirst() + .orElseThrow(() -> + new IOException("can't find (L"+ clazz + ";)V for class: " + dfrc.getName())); try { - return constructor.newInstance(new Object[] { arg } ); + return constructor.newInstance(arg); } catch (Exception e) { throw new IOException(e.getMessage()); } @@ -1854,26 +1710,26 @@ * can strip NUL terminators and perform EOLN search-and-replace. */ public class ReencodingInputStream extends InputStream { - protected BufferedReader wrapped; - protected final char[] in = new char[2]; - protected byte[] out; - - protected CharsetEncoder encoder; - protected CharBuffer inBuf; - protected ByteBuffer outBuf; - - protected char[] eoln; - protected int numTerminators; - - protected boolean eos; - protected int index, limit; + BufferedReader wrapped; + final char[] in = new char[2]; + byte[] out; + + CharsetEncoder encoder; + CharBuffer inBuf; + ByteBuffer outBuf; + + char[] eoln; + int numTerminators; + + boolean eos; + int index, limit; public ReencodingInputStream(InputStream bytestream, long format, String targetEncoding, Transferable localeTransferable) throws IOException { - Long lFormat = Long.valueOf(format); + Long lFormat = format; String sourceEncoding = null; if (isLocaleDependentTextFormat(format) && @@ -1911,24 +1767,22 @@ out = new byte[(int)(encoder.maxBytesPerChar() * 2 + 0.5)]; inBuf = CharBuffer.wrap(in); outBuf = ByteBuffer.wrap(out); - } catch (IllegalCharsetNameException e) { - throw new IOException(e.toString()); - } catch (UnsupportedCharsetException e) { - throw new IOException(e.toString()); - } catch (UnsupportedOperationException e) { + } catch (IllegalCharsetNameException + | UnsupportedCharsetException + | UnsupportedOperationException e) { throw new IOException(e.toString()); } - String sEoln = (String)nativeEOLNs.get(lFormat); + String sEoln = nativeEOLNs.get(lFormat); if (sEoln != null) { eoln = sEoln.toCharArray(); } // A hope and a prayer that this works generically. This will // definitely work on Win32. - Integer terminators = (Integer)nativeTerminators.get(lFormat); + Integer terminators = nativeTerminators.get(lFormat); if (terminators != null) { - numTerminators = terminators.intValue(); + numTerminators = terminators; } } @@ -2075,19 +1929,14 @@ while (readerIterator.hasNext()) { ImageReader imageReader = (ImageReader)readerIterator.next(); try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) { - ImageInputStream imageInputStream = - ImageIO.createImageInputStream(bais); - - try { + try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(bais)) { ImageReadParam param = imageReader.getDefaultReadParam(); imageReader.setInput(imageInputStream, true, true); - BufferedImage bufferedImage = - imageReader.read(imageReader.getMinIndex(), param); + BufferedImage bufferedImage = imageReader.read(imageReader.getMinIndex(), param); if (bufferedImage != null) { return bufferedImage; } } finally { - imageInputStream.close(); imageReader.dispose(); } } catch (IOException e) { @@ -2176,7 +2025,7 @@ } } - protected byte[] imageToStandardBytesImpl(RenderedImage renderedImage, + byte[] imageToStandardBytesImpl(RenderedImage renderedImage, String mimeType) throws IOException { @@ -2197,14 +2046,10 @@ } try { - ImageOutputStream imageOutputStream = - ImageIO.createImageOutputStream(baos); - try { + try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos)) { imageWriter.setOutput(imageOutputStream); imageWriter.write(renderedImage); imageOutputStream.flush(); - } finally { - imageOutputStream.close(); } } catch (IOException e) { imageWriter.dispose(); @@ -2290,7 +2135,7 @@ * that may contain client code. */ if (isToolkitThread) try { - final Stack stack = new Stack(); + final Stack stack = new Stack<>(); final Runnable dataConverter = new Runnable() { // Guard against multiple executions. private boolean done = false; @@ -2300,7 +2145,7 @@ } byte[] data = null; try { - DataFlavor flavor = (DataFlavor)formatMap.get(Long.valueOf(format)); + DataFlavor flavor = (DataFlavor)formatMap.get(format); if (flavor != null) { data = translateTransferable(contents, flavor, format); } @@ -2337,12 +2182,11 @@ appContext.remove(DATA_CONVERTER_KEY); } - ret = (byte[])stack.pop(); + ret = stack.pop(); } finally { getToolkitThreadBlockedHandler().unlock(); } else { - DataFlavor flavor = (DataFlavor) - formatMap.get(Long.valueOf(format)); + DataFlavor flavor = (DataFlavor)formatMap.get(format); if (flavor != null) { ret = translateTransferable(contents, flavor, format); } @@ -2377,12 +2221,12 @@ * The map keys are sorted according to the native formats preference * order. */ - public static long[] keysToLongArray(SortedMap map) { - Set keySet = map.keySet(); + public static long[] keysToLongArray(SortedMap map) { + Set keySet = map.keySet(); long[] retval = new long[keySet.size()]; int i = 0; - for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) { - retval[i] = ((Long)iter.next()).longValue(); + for (Iterator iter = keySet.iterator(); iter.hasNext(); i++) { + retval[i] = iter.next(); } return retval; } @@ -2394,7 +2238,7 @@ public static DataFlavor[] setToSortedDataFlavorArray(Set flavorsSet) { DataFlavor[] flavors = new DataFlavor[flavorsSet.size()]; flavorsSet.toArray(flavors); - final Comparator comparator = + final Comparator comparator = new DataFlavorComparator(IndexedComparator.SELECT_WORST); Arrays.sort(flavors, comparator); return flavors; @@ -2423,8 +2267,8 @@ * If there are no platform-specific mappings for this native, the method * returns an empty List. */ - public List getPlatformMappingsForNative(String nat) { - return new ArrayList(); + public List getPlatformMappingsForNative(String nat) { + return new ArrayList<>(); } /** @@ -2432,15 +2276,15 @@ * If there are no platform-specific mappings for this flavor, the method * returns an empty List. */ - public List getPlatformMappingsForFlavor(DataFlavor df) { - return new ArrayList(); + public List getPlatformMappingsForFlavor(DataFlavor df) { + return new ArrayList<>(); } /** * A Comparator which includes a helper function for comparing two Objects * which are likely to be keys in the specified Map. */ - public abstract static class IndexedComparator implements Comparator { + public abstract static class IndexedComparator implements Comparator { /** * The best Object (e.g., DataFlavor) will be the last in sequence. @@ -2452,11 +2296,7 @@ */ public static final boolean SELECT_WORST = false; - protected final boolean order; - - public IndexedComparator() { - this(SELECT_BEST); - } + final boolean order; public IndexedComparator(boolean order) { this.order = order; @@ -2475,48 +2315,11 @@ * first object is mapped to a less, equal to, or greater * index than the second. */ - protected static int compareIndices(Map indexMap, - Object obj1, Object obj2, - Integer fallbackIndex) { - Integer index1 = (Integer)indexMap.get(obj1); - Integer index2 = (Integer)indexMap.get(obj2); - - if (index1 == null) { - index1 = fallbackIndex; - } - if (index2 == null) { - index2 = fallbackIndex; - } - - return index1.compareTo(index2); - } - - /** - * Helper method to compare two objects by their Long indices in the - * given map. If the map doesn't contain an entry for either of the - * objects, the fallback index will be used for the object instead. - * - * @param indexMap the map which maps objects into Long indexes. - * @param obj1 the first object to be compared. - * @param obj2 the second object to be compared. - * @param fallbackIndex the Long to be used as a fallback index. - * @return a negative integer, zero, or a positive integer as the - * first object is mapped to a less, equal to, or greater - * index than the second. - */ - protected static int compareLongs(Map indexMap, - Object obj1, Object obj2, - Long fallbackIndex) { - Long index1 = (Long)indexMap.get(obj1); - Long index2 = (Long)indexMap.get(obj2); - - if (index1 == null) { - index1 = fallbackIndex; - } - if (index2 == null) { - index2 = fallbackIndex; - } - + static int compareIndices(Map indexMap, + T obj1, T obj2, + Integer fallbackIndex) { + Integer index1 = indexMap.getOrDefault(obj1, fallbackIndex); + Integer index2 = indexMap.getOrDefault(obj2, fallbackIndex); return index1.compareTo(index2); } } @@ -2528,45 +2331,37 @@ * in alphabetical order, charsets are not automatically converted to their * canonical forms. */ - public static class CharsetComparator extends IndexedComparator { - private static final Map charsets; - private static String defaultEncoding; - - private static final Integer DEFAULT_CHARSET_INDEX = Integer.valueOf(2); - private static final Integer OTHER_CHARSET_INDEX = Integer.valueOf(1); - private static final Integer WORST_CHARSET_INDEX = Integer.valueOf(0); - private static final Integer UNSUPPORTED_CHARSET_INDEX = - Integer.valueOf(Integer.MIN_VALUE); + public static class CharsetComparator extends IndexedComparator { + private static final Map charsets; + private static final String defaultEncoding; + + private static final Integer DEFAULT_CHARSET_INDEX = 2; + private static final Integer OTHER_CHARSET_INDEX = 1; + private static final Integer WORST_CHARSET_INDEX = 0; + private static final Integer UNSUPPORTED_CHARSET_INDEX = Integer.MIN_VALUE; private static final String UNSUPPORTED_CHARSET = "UNSUPPORTED"; static { - HashMap charsetsMap = new HashMap(8, 1.0f); + Map charsetsMap = new HashMap<>(8, 1.0f); // we prefer Unicode charsets - charsetsMap.put(canonicalName("UTF-16LE"), Integer.valueOf(4)); - charsetsMap.put(canonicalName("UTF-16BE"), Integer.valueOf(5)); - charsetsMap.put(canonicalName("UTF-8"), Integer.valueOf(6)); - charsetsMap.put(canonicalName("UTF-16"), Integer.valueOf(7)); + charsetsMap.put(canonicalName("UTF-16LE"), 4); + charsetsMap.put(canonicalName("UTF-16BE"), 5); + charsetsMap.put(canonicalName("UTF-8"), 6); + charsetsMap.put(canonicalName("UTF-16"), 7); // US-ASCII is the worst charset supported charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX); - String defEncoding = DataTransferer.canonicalName - (DataTransferer.getDefaultTextCharset()); - - if (charsetsMap.get(defaultEncoding) == null) { - charsetsMap.put(defaultEncoding, DEFAULT_CHARSET_INDEX); - } + defaultEncoding = DataTransferer.canonicalName(DataTransferer.getDefaultTextCharset()); + charsetsMap.putIfAbsent(defaultEncoding, DEFAULT_CHARSET_INDEX); + charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); charsets = Collections.unmodifiableMap(charsetsMap); } - public CharsetComparator() { - this(SELECT_BEST); - } - public CharsetComparator(boolean order) { super(order); } @@ -2586,18 +2381,12 @@ * @throws NullPointerException if either of the arguments is * null. */ - public int compare(Object obj1, Object obj2) { - String charset1 = null; - String charset2 = null; + public int compare(String obj1, String obj2) { if (order == SELECT_BEST) { - charset1 = (String)obj1; - charset2 = (String)obj2; + return compareCharsets(obj1, obj2); } else { - charset1 = (String)obj2; - charset2 = (String)obj1; + return compareCharsets(obj2, obj1); } - - return compareCharsets(charset1, charset2); } /** @@ -2623,7 +2412,7 @@ * first argument is worse, equal to, or better than the * second. */ - protected int compareCharsets(String charset1, String charset2) { + int compareCharsets(String charset1, String charset2) { charset1 = getEncoding(charset1); charset2 = getEncoding(charset2); @@ -2653,7 +2442,7 @@ * @param charset the charset. * @return an encoding for this charset. */ - protected static String getEncoding(String charset) { + static String getEncoding(String charset) { if (charset == null) { return null; } else if (!DataTransferer.isEncodingSupported(charset)) { @@ -2681,131 +2470,108 @@ * most descriptive one. For flavors which are otherwise equal, the * flavors' string representation are compared in the alphabetical order. */ - public static class DataFlavorComparator extends IndexedComparator { + public static class DataFlavorComparator extends IndexedComparator { private final CharsetComparator charsetComparator; - private static final Map exactTypes; - private static final Map primaryTypes; - private static final Map nonTextRepresentations; - private static final Map textTypes; - private static final Map decodedTextRepresentations; - private static final Map encodedTextRepresentations; - - private static final Integer UNKNOWN_OBJECT_LOSES = - Integer.valueOf(Integer.MIN_VALUE); - private static final Integer UNKNOWN_OBJECT_WINS = - Integer.valueOf(Integer.MAX_VALUE); - - private static final Long UNKNOWN_OBJECT_LOSES_L = - Long.valueOf(Long.MIN_VALUE); - private static final Long UNKNOWN_OBJECT_WINS_L = - Long.valueOf(Long.MAX_VALUE); + private static final Map exactTypes; + private static final Map primaryTypes; + private static final Map, Integer> nonTextRepresentations; + private static final Map textTypes; + private static final Map, Integer> decodedTextRepresentations; + private static final Map, Integer> encodedTextRepresentations; + + private static final Integer UNKNOWN_OBJECT_LOSES = Integer.MIN_VALUE; + private static final Integer UNKNOWN_OBJECT_WINS = Integer.MAX_VALUE; static { { - HashMap exactTypesMap = new HashMap(4, 1.0f); + Map exactTypesMap = new HashMap<>(4, 1.0f); // application/x-java-* MIME types - exactTypesMap.put("application/x-java-file-list", - Integer.valueOf(0)); - exactTypesMap.put("application/x-java-serialized-object", - Integer.valueOf(1)); - exactTypesMap.put("application/x-java-jvm-local-objectref", - Integer.valueOf(2)); - exactTypesMap.put("application/x-java-remote-object", - Integer.valueOf(3)); + exactTypesMap.put("application/x-java-file-list", 0); + exactTypesMap.put("application/x-java-serialized-object", 1); + exactTypesMap.put("application/x-java-jvm-local-objectref", 2); + exactTypesMap.put("application/x-java-remote-object", 3); exactTypes = Collections.unmodifiableMap(exactTypesMap); } { - HashMap primaryTypesMap = new HashMap(1, 1.0f); - - primaryTypesMap.put("application", Integer.valueOf(0)); + Map primaryTypesMap = new HashMap<>(1, 1.0f); + + primaryTypesMap.put("application", 0); primaryTypes = Collections.unmodifiableMap(primaryTypesMap); } { - HashMap nonTextRepresentationsMap = new HashMap(3, 1.0f); - - nonTextRepresentationsMap.put(java.io.InputStream.class, - Integer.valueOf(0)); - nonTextRepresentationsMap.put(java.io.Serializable.class, - Integer.valueOf(1)); + Map, Integer> nonTextRepresentationsMap = new HashMap<>(3, 1.0f); + + nonTextRepresentationsMap.put(java.io.InputStream.class, 0); + nonTextRepresentationsMap.put(java.io.Serializable.class, 1); Class remoteClass = RMI.remoteClass(); if (remoteClass != null) { - nonTextRepresentationsMap.put(remoteClass, - Integer.valueOf(2)); + nonTextRepresentationsMap.put(remoteClass, 2); } - nonTextRepresentations = - Collections.unmodifiableMap(nonTextRepresentationsMap); + nonTextRepresentations = Collections.unmodifiableMap(nonTextRepresentationsMap); } { - HashMap textTypesMap = new HashMap(16, 1.0f); + Map textTypesMap = new HashMap<>(16, 1.0f); // plain text - textTypesMap.put("text/plain", Integer.valueOf(0)); + textTypesMap.put("text/plain", 0); // stringFlavor - textTypesMap.put("application/x-java-serialized-object", - Integer.valueOf(1)); + textTypesMap.put("application/x-java-serialized-object", 1); // misc - textTypesMap.put("text/calendar", Integer.valueOf(2)); - textTypesMap.put("text/css", Integer.valueOf(3)); - textTypesMap.put("text/directory", Integer.valueOf(4)); - textTypesMap.put("text/parityfec", Integer.valueOf(5)); - textTypesMap.put("text/rfc822-headers", Integer.valueOf(6)); - textTypesMap.put("text/t140", Integer.valueOf(7)); - textTypesMap.put("text/tab-separated-values", Integer.valueOf(8)); - textTypesMap.put("text/uri-list", Integer.valueOf(9)); + textTypesMap.put("text/calendar", 2); + textTypesMap.put("text/css", 3); + textTypesMap.put("text/directory", 4); + textTypesMap.put("text/parityfec", 5); + textTypesMap.put("text/rfc822-headers", 6); + textTypesMap.put("text/t140", 7); + textTypesMap.put("text/tab-separated-values", 8); + textTypesMap.put("text/uri-list", 9); // enriched - textTypesMap.put("text/richtext", Integer.valueOf(10)); - textTypesMap.put("text/enriched", Integer.valueOf(11)); - textTypesMap.put("text/rtf", Integer.valueOf(12)); + textTypesMap.put("text/richtext", 10); + textTypesMap.put("text/enriched", 11); + textTypesMap.put("text/rtf", 12); // markup - textTypesMap.put("text/html", Integer.valueOf(13)); - textTypesMap.put("text/xml", Integer.valueOf(14)); - textTypesMap.put("text/sgml", Integer.valueOf(15)); + textTypesMap.put("text/html", 13); + textTypesMap.put("text/xml", 14); + textTypesMap.put("text/sgml", 15); textTypes = Collections.unmodifiableMap(textTypesMap); } { - HashMap decodedTextRepresentationsMap = new HashMap(4, 1.0f); - - decodedTextRepresentationsMap.put - (char[].class, Integer.valueOf(0)); - decodedTextRepresentationsMap.put - (java.nio.CharBuffer.class, Integer.valueOf(1)); - decodedTextRepresentationsMap.put - (java.lang.String.class, Integer.valueOf(2)); - decodedTextRepresentationsMap.put - (java.io.Reader.class, Integer.valueOf(3)); + Map, Integer> decodedTextRepresentationsMap = new HashMap<>(4, 1.0f); + + decodedTextRepresentationsMap.put(char[].class, 0); + decodedTextRepresentationsMap.put(CharBuffer.class, 1); + decodedTextRepresentationsMap.put(String.class, 2); + decodedTextRepresentationsMap.put(Reader.class, 3); decodedTextRepresentations = - Collections.unmodifiableMap(decodedTextRepresentationsMap); + Collections.unmodifiableMap(decodedTextRepresentationsMap); } { - HashMap encodedTextRepresentationsMap = new HashMap(3, 1.0f); - - encodedTextRepresentationsMap.put - (byte[].class, Integer.valueOf(0)); - encodedTextRepresentationsMap.put - (java.nio.ByteBuffer.class, Integer.valueOf(1)); - encodedTextRepresentationsMap.put - (java.io.InputStream.class, Integer.valueOf(2)); + Map, Integer> encodedTextRepresentationsMap = new HashMap<>(3, 1.0f); + + encodedTextRepresentationsMap.put(byte[].class, 0); + encodedTextRepresentationsMap.put(ByteBuffer.class, 1); + encodedTextRepresentationsMap.put(InputStream.class, 2); encodedTextRepresentations = - Collections.unmodifiableMap(encodedTextRepresentationsMap); + Collections.unmodifiableMap(encodedTextRepresentationsMap); } } @@ -2819,16 +2585,9 @@ charsetComparator = new CharsetComparator(order); } - public int compare(Object obj1, Object obj2) { - DataFlavor flavor1 = null; - DataFlavor flavor2 = null; - if (order == SELECT_BEST) { - flavor1 = (DataFlavor)obj1; - flavor2 = (DataFlavor)obj2; - } else { - flavor1 = (DataFlavor)obj2; - flavor2 = (DataFlavor)obj1; - } + public int compare(DataFlavor obj1, DataFlavor obj2) { + DataFlavor flavor1 = order == SELECT_BEST ? obj1 : obj2; + DataFlavor flavor2 = order == SELECT_BEST ? obj2 : obj1; if (flavor1.equals(flavor2)) { return 0; @@ -2931,22 +2690,16 @@ * reverse index-based order: an object A is greater than an object B if and * only if A is less than B with the direct index-based order. */ - public static class IndexOrderComparator extends IndexedComparator { - private final Map indexMap; - private static final Integer FALLBACK_INDEX = - Integer.valueOf(Integer.MIN_VALUE); - - public IndexOrderComparator(Map indexMap) { - super(SELECT_BEST); - this.indexMap = indexMap; - } - - public IndexOrderComparator(Map indexMap, boolean order) { + public static class IndexOrderComparator extends IndexedComparator { + private final Map indexMap; + private static final Integer FALLBACK_INDEX = Integer.MIN_VALUE; + + public IndexOrderComparator(Map indexMap, boolean order) { super(order); this.indexMap = indexMap; } - public int compare(Object obj1, Object obj2) { + public int compare(Long obj1, Long obj2) { if (order == SELECT_WORST) { return -compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX); } else { @@ -2996,7 +2749,7 @@ * Returns {@code true} if the given class is java.rmi.Remote. */ static boolean isRemote(Class c) { - return (remoteClass == null) ? null : remoteClass.isAssignableFrom(c); + return (remoteClass == null) ? false : remoteClass.isAssignableFrom(c); } /** @@ -3013,9 +2766,7 @@ static Object newMarshalledObject(Object obj) throws IOException { try { return marshallCtor.newInstance(obj); - } catch (InstantiationException x) { - throw new AssertionError(x); - } catch (IllegalAccessException x) { + } catch (InstantiationException | IllegalAccessException x) { throw new AssertionError(x); } catch (InvocationTargetException x) { Throwable cause = x.getCause(); diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java --- a/jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java Fri Jan 31 14:20:40 2014 +0400 @@ -217,7 +217,7 @@ openClipboard(null); long[] formats = getClipboardFormats(); - Long lFormat = (Long)DataTransferer.getInstance(). + Long lFormat = DataTransferer.getInstance(). getFlavorsForFormats(formats, flavorMap).get(flavor); if (lFormat == null) { diff -r 6369264caa4a -r 16c1ddb7b66a jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java --- a/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java Fri Jan 31 13:19:50 2014 +0400 +++ b/jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java Fri Jan 31 14:20:40 2014 +0400 @@ -56,6 +56,7 @@ import sun.awt.datatransfer.ToolkitThreadBlockedHandler; import java.io.ByteArrayOutputStream; +import java.util.stream.Stream; /** * Platform-specific support for the data transfer subsystem. @@ -280,7 +281,7 @@ try { reader = new BufferedReader(new InputStreamReader(stream, charset)); String line; - ArrayList uriList = new ArrayList(); + ArrayList uriList = new ArrayList<>(); URI uri; while ((line = reader.readLine()) != null) { try { @@ -328,8 +329,9 @@ * a valid MIME and return a list of flavors to which the data in this MIME * type can be translated by the Data Transfer subsystem. */ - public List getPlatformMappingsForNative(String nat) { - List flavors = new ArrayList(); + @Override + public List getPlatformMappingsForNative(String nat) { + List flavors = new ArrayList<>(); if (nat == null) { return flavors; @@ -389,8 +391,9 @@ * MIME types to which the data in this flavor can be translated by the Data * Transfer subsystem. */ - public List getPlatformMappingsForFlavor(DataFlavor df) { - List natives = new ArrayList(1); + @Override + public List getPlatformMappingsForFlavor(DataFlavor df) { + List natives = new ArrayList<>(1); if (df == null) { return natives; @@ -416,18 +419,15 @@ if (DataFlavor.imageFlavor.equals(df)) { String[] mimeTypes = ImageIO.getWriterMIMETypes(); if (mimeTypes != null) { - for (int i = 0; i < mimeTypes.length; i++) { - Iterator writers = - ImageIO.getImageWritersByMIMEType(mimeTypes[i]); - + for (String mime : mimeTypes) { + Iterator writers = ImageIO.getImageWritersByMIMEType(mime); while (writers.hasNext()) { - ImageWriter imageWriter = (ImageWriter)writers.next(); - ImageWriterSpi writerSpi = - imageWriter.getOriginatingProvider(); + ImageWriter imageWriter = writers.next(); + ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider(); if (writerSpi != null && - writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) { - natives.add(mimeTypes[i]); + writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) { + natives.add(mime); break; } }