⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systemflavormap.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * @(#)SystemFlavorMap.java	1.31 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.datatransfer;import java.awt.Toolkit;import java.lang.ref.SoftReference;import java.io.BufferedReader;import java.io.File;import java.io.InputStreamReader;import java.io.IOException;import java.net.URL;import java.net.MalformedURLException;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.WeakHashMap;import sun.awt.datatransfer.DataTransferer;/** * The SystemFlavorMap is a configurable map between "natives" (Strings), which * correspond to platform-specific data formats, and "flavors" (DataFlavors), * which correspond to platform-independent MIME types. This mapping is used * by the data transfer subsystem to transfer data between Java and native * applications, and between Java applications in separate VMs. * <p> * In the Sun reference implementation, the default SystemFlavorMap is * initialized by the file <code>jre/lib/flavormap.properties</code> and the * contents of the URL referenced by the AWT property * <code>AWT.DnD.flavorMapFileURL</code>. See <code>flavormap.properties</code> * for details. * * @version 1.31, 01/23/03 * @since 1.2 */public final class SystemFlavorMap implements FlavorMap, FlavorTable {    /**     * Constant prefix used to tag Java types converted to native platform     * type.     */    private static String JavaMIME = "JAVA_DATAFLAVOR:";    /**     * System singleton which maps a thread's ClassLoader to a SystemFlavorMap.     */    private static final WeakHashMap flavorMaps = new WeakHashMap();    /**     * Copied from java.util.Properties.     */    private static final String keyValueSeparators = "=: \t\r\n\f";    private static final String strictKeyValueSeparators = "=:";    private static final String whiteSpaceChars = " \t\r\n\f";    /**     * The list of valid, decoded text flavor representation classes, in order     * from best to worst.     */    private static final String[] UNICODE_TEXT_CLASSES = {        "java.io.Reader", "java.lang.String", "java.nio.CharBuffer", "\"[C\""    };    /**     * The list of valid, encoded text flavor representation classes, in order     * from best to worst.     */    private static final String[] ENCODED_TEXT_CLASSES = {        "java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""    };    /**     * A String representing text/plain MIME type.     */    private static final String TEXT_PLAIN_BASE_TYPE = "text/plain";    /**     * This constant is passed to flavorToNativeLookup() to indicate that a     * a native should be synthesized, stored, and returned by encoding the     * DataFlavor's MIME type in case if the DataFlavor is not found in     * 'flavorToNative' map.     */    private static final boolean SYNTHESIZE_IF_NOT_FOUND = true;    /**     * Maps native Strings to Lists of DataFlavors (or base type Strings for     * text DataFlavors).     */    private Map nativeToFlavor = new HashMap();    /**     * Maps DataFlavors (or base type Strings for text DataFlavors) to Lists of     * native Strings.     */    private Map flavorToNative = new HashMap();    /**     * Caches the result of getNativesForFlavor(). Maps DataFlavors to     * SoftReferences which reference Lists of String natives.     */    private Map getNativesForFlavorCache = new HashMap();    /**     * Caches the result getFlavorsForNative(). Maps String natives to     * SoftReferences which reference Lists of DataFlavors.     */    private Map getFlavorsForNativeCache = new HashMap();    /**     * Dynamic mapping generation used for text mappings should not be applied     * to the DataFlavors and String natives for which the mappings have been     * explicitly specified with setFlavorsForNative() or     * setNativesForFlavor(). This keeps all such keys.     */    private Set disabledMappingGenerationKeys = new HashSet();    /**     * Returns the default FlavorMap for this thread's ClassLoader.     */    public static FlavorMap getDefaultFlavorMap() {        ClassLoader contextClassLoader =            Thread.currentThread().getContextClassLoader();        if (contextClassLoader == null) {            contextClassLoader = ClassLoader.getSystemClassLoader();        }        FlavorMap fm;        synchronized(flavorMaps) {            fm = (FlavorMap)flavorMaps.get(contextClassLoader);            if (fm == null) {                fm = new SystemFlavorMap();                flavorMaps.put(contextClassLoader, fm);            }        }        return fm;    }    /**     * Constructs a SystemFlavorMap by reading flavormap.properties and     * AWT.DnD.flavorMapFileURL.     */    private SystemFlavorMap() {        BufferedReader flavormapDotProperties = (BufferedReader)            java.security.AccessController.doPrivileged(                new java.security.PrivilegedAction() {                    public Object run() {                        String fileName =                            System.getProperty("java.home") +                            File.separator +                            "lib" +                            File.separator +                            "flavormap.properties";                        try {                            return new BufferedReader                                (new InputStreamReader                                    (new File(fileName).toURI().toURL().openStream(), "ISO-8859-1"));                        } catch (MalformedURLException e) {                            System.err.println("MalformedURLException:" + e + " while loading default flavormap.properties file:" + fileName);                        } catch (IOException e) {                            System.err.println("IOException:" + e + " while loading default flavormap.properties file:" + fileName);                        }                        return null;                    }                });        BufferedReader flavormapURL = (BufferedReader)            java.security.AccessController.doPrivileged(                new java.security.PrivilegedAction() {                    public Object run() {                        String url = Toolkit.getDefaultToolkit().getProperty                            ("AWT.DnD.flavorMapFileURL", null);                        if (url == null) {                            return null;                        }                        try {                            return new BufferedReader                                (new InputStreamReader                                    (new URL(url).openStream(), "ISO-8859-1"));                        } catch (MalformedURLException e) {                            System.err.println("MalformedURLException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);                        } catch (IOException e) {                            System.err.println("IOException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);                        }                        return null;                    }                });        if (flavormapDotProperties != null) {            try {                parseAndStoreReader(flavormapDotProperties);            } catch (IOException e) {                System.err.println("IOException:" + e + " while parsing default flavormap.properties file");            }        }        if (flavormapURL != null) {            try {                parseAndStoreReader(flavormapURL);            } catch (IOException e) {                System.err.println("IOException:" + e + " while parsing AWT.DnD.flavorMapFileURL");            }        }    }    /**     * Copied code from java.util.Properties. Parsing the data ourselves is the     * only way to handle duplicate keys and values.     */    private void parseAndStoreReader(BufferedReader in) throws IOException {        while (true) {            // Get next line            String line = in.readLine();            if (line == null) {                return;            }            if (line.length() > 0) {                // Continue lines that end in slashes if they are not comments                char firstChar = line.charAt(0);                if (firstChar != '#' && firstChar != '!') {                    while (continueLine(line)) {                        String nextLine = in.readLine();                        if (nextLine == null) {                            nextLine = new String("");                        }                        String loppedLine =                            line.substring(0, line.length() - 1);                        // Advance beyond whitespace on new line                        int startIndex = 0;                        for(; startIndex < nextLine.length(); startIndex++) {                            if (whiteSpaceChars.                                    indexOf(nextLine.charAt(startIndex)) == -1)                            {                                break;                            }                        }                        nextLine = nextLine.substring(startIndex,                                                      nextLine.length());                        line = new String(loppedLine+nextLine);                    }                    // Find start of key                    int len = line.length();                    int keyStart = 0;                    for(; keyStart < len; keyStart++) {                        if(whiteSpaceChars.                               indexOf(line.charAt(keyStart)) == -1) {                            break;                        }                    }                    // Blank lines are ignored                    if (keyStart == len) {                        continue;                    }                    // Find separation between key and value                    int separatorIndex = keyStart;                    for(; separatorIndex < len; separatorIndex++) {                        char currentChar = line.charAt(separatorIndex);                        if (currentChar == '\\') {                            separatorIndex++;                        } else if (keyValueSeparators.                                       indexOf(currentChar) != -1) {                            break;                        }                    }                    // Skip over whitespace after key if any                    int valueIndex = separatorIndex;                    for (; valueIndex < len; valueIndex++) {                        if (whiteSpaceChars.                                indexOf(line.charAt(valueIndex)) == -1) {                            break;                        }                    }                    // Skip over one non whitespace key value separators if any                    if (valueIndex < len) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -