encodings.java

来自「JAVA 所有包」· Java 代码 · 共 464 行 · 第 1/2 页

JAVA
464
字号
                            || encoding.equalsIgnoreCase("ISO8859_1")                            || encoding.equalsIgnoreCase("8859_1")                            || encoding.equalsIgnoreCase("UTF8"))                            ? DEFAULT_MIME_ENCODING                            : convertJava2MimeEncoding(encoding);                    encoding =                        (null != jencoding) ? jencoding : DEFAULT_MIME_ENCODING;                }                else                {                    encoding = DEFAULT_MIME_ENCODING;                }            }            catch (SecurityException se)            {                encoding = DEFAULT_MIME_ENCODING;            }        }        else        {            encoding = convertJava2MimeEncoding(encoding);        }        return encoding;    }    /**     * Try the best we can to convert a Java encoding to a XML-style encoding.     *     * @param encoding non-null reference to encoding string, java style.     *     * @return ISO-style encoding string.     */    private static String convertJava2MimeEncoding(String encoding)    {        EncodingInfo enc =            (EncodingInfo) _encodingTableKeyJava.get(encoding.toUpperCase());        if (null != enc)            return enc.name;        return encoding;    }    /**     * Try the best we can to convert a Java encoding to a XML-style encoding.     *     * @param encoding non-null reference to encoding string, java style.     *     * @return ISO-style encoding string.     */    public static String convertMime2JavaEncoding(String encoding)    {        for (int i = 0; i < _encodings.length; ++i)        {            if (_encodings[i].name.equalsIgnoreCase(encoding))            {                return _encodings[i].javaName;            }        }        return encoding;    }    /**     * Load a list of all the supported encodings.     *     * System property "encodings" formatted using URL syntax may define an     * external encodings list. Thanks to Sergey Ushakov for the code     * contribution!     */    private static EncodingInfo[] loadEncodingInfo()    {        URL url = null;        try        {            String urlString = null;            InputStream is = null;            try            {                urlString = System.getProperty(ENCODINGS_PROP, "");            }            catch (SecurityException e)            {            }            if (urlString != null && urlString.length() > 0) {                url = new URL(urlString);                is = url.openStream();            }            if (is == null) {                SecuritySupport ss = SecuritySupport.getInstance();                is = ss.getResourceAsStream(ObjectFactory.findClassLoader(),                                            ENCODINGS_FILE);            }            Properties props = new Properties();            if (is != null) {                props.load(is);                is.close();            } else {                // Seems to be no real need to force failure here, let the                // system do its best... The issue is not really very critical,                // and the output will be in any case _correct_ though maybe not                // always human-friendly... :)                // But maybe report/log the resource problem?                // Any standard ways to report/log errors (in static context)?            }            int totalEntries = props.size();            int totalMimeNames = 0;            Enumeration keys = props.keys();            for (int i = 0; i < totalEntries; ++i)            {                String javaName = (String) keys.nextElement();                String val = props.getProperty(javaName);                totalMimeNames++;                int pos = val.indexOf(' ');                for (int j = 0; j < pos; ++j)                    if (val.charAt(j) == ',')                        totalMimeNames++;            }            EncodingInfo[] ret = new EncodingInfo[totalMimeNames];            int j = 0;            keys = props.keys();            for (int i = 0; i < totalEntries; ++i)            {                String javaName = (String) keys.nextElement();                String val = props.getProperty(javaName);                int pos = val.indexOf(' ');                String mimeName;                //int lastPrintable;                if (pos < 0)                {                    // Maybe report/log this problem?                    //  "Last printable character not defined for encoding " +                    //  mimeName + " (" + val + ")" ...                    mimeName = val;                    //lastPrintable = 0x00FF;                }                else                {                    //lastPrintable =                    //    Integer.decode(val.substring(pos).trim()).intValue();                    StringTokenizer st =                        new StringTokenizer(val.substring(0, pos), ",");                    for (boolean first = true;                        st.hasMoreTokens();                        first = false)                    {                        mimeName = st.nextToken();                        ret[j] =                            new EncodingInfo(mimeName, javaName);                        _encodingTableKeyMime.put(                            mimeName.toUpperCase(),                            ret[j]);                        if (first)                            _encodingTableKeyJava.put(                                javaName.toUpperCase(),                                ret[j]);                        j++;                    }                }            }            return ret;        }        catch (java.net.MalformedURLException mue)        {            throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(mue);        }        catch (java.io.IOException ioe)        {            throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(ioe);        }    }    /**     * Return true if the character is the high member of a surrogate pair.     * <p>     * This is not a public API.     * @param ch the character to test     * @xsl.usage internal     */    static boolean isHighUTF16Surrogate(char ch) {        return ('\uD800' <= ch && ch <= '\uDBFF');    }    /**     * Return true if the character is the low member of a surrogate pair.     * <p>     * This is not a public API.     * @param ch the character to test     * @xsl.usage internal     */    static boolean isLowUTF16Surrogate(char ch) {        return ('\uDC00' <= ch && ch <= '\uDFFF');    }    /**     * Return the unicode code point represented by the high/low surrogate pair.     * <p>     * This is not a public API.     * @param highSurrogate the high char of the high/low pair     * @param lowSurrogate the low char of the high/low pair     * @xsl.usage internal     */    static int toCodePoint(char highSurrogate, char lowSurrogate) {        int codePoint =            ((highSurrogate - 0xd800) << 10)                + (lowSurrogate - 0xdc00)                + 0x10000;        return codePoint;    }    /**     * Return the unicode code point represented by the char.     * A bit of a dummy method, since all it does is return the char,     * but as an int value.     * <p>     * This is not a public API.     * @param ch the char.     * @xsl.usage internal     */    static int toCodePoint(char ch) {        int codePoint = ch;        return codePoint;    }    private static final Hashtable _encodingTableKeyJava = new Hashtable();    private static final Hashtable _encodingTableKeyMime = new Hashtable();    private static final EncodingInfo[] _encodings = loadEncodingInfo();}

⌨️ 快捷键说明

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