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

📄 mimeutility.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	return defaultJavaCharset;    }    /*     * Get the default MIME charset for this locale.     */    static String getDefaultMIMECharset() {	if (defaultMIMECharset == null) {	    try {		defaultMIMECharset = System.getProperty("mail.mime.charset");	    } catch (SecurityException ex) { }	// ignore it	}	if (defaultMIMECharset == null)	    defaultMIMECharset = mimeCharset(getDefaultJavaCharset());	return defaultMIMECharset;    }    // Tables to map MIME charset names to Java names and vice versa.    // XXX - Should eventually use J2SE 1.4 java.nio.charset.Charset    private static Hashtable mime2java;    private static Hashtable java2mime;    static {	java2mime = new Hashtable(40);	mime2java = new Hashtable(10);	try {	    // Use this class's classloader to load the mapping file	    // XXX - we should use SecuritySupport, but it's in another package	    InputStream is = 		    javax.mail.internet.MimeUtility.class.getResourceAsStream(		    "/META-INF/javamail.charset.map");	    if (is != null) {		try {		    is = new LineInputStream(is);		    // Load the JDK-to-MIME charset mapping table		    loadMappings((LineInputStream)is, java2mime);		    // Load the MIME-to-JDK charset mapping table		    loadMappings((LineInputStream)is, mime2java);		} finally {		    try {			is.close();		    } catch (Exception cex) {			// ignore		    }		}	    }	} catch (Exception ex) { }	// If we didn't load the tables, e.g., because we didn't have	// permission, load them manually.  The entries here should be	// the same as the default javamail.charset.map.	if (java2mime.isEmpty()) {	    java2mime.put("8859_1", "ISO-8859-1");	    java2mime.put("iso8859_1", "ISO-8859-1");	    java2mime.put("iso8859-1", "ISO-8859-1");	    java2mime.put("8859_2", "ISO-8859-2");	    java2mime.put("iso8859_2", "ISO-8859-2");	    java2mime.put("iso8859-2", "ISO-8859-2");	    java2mime.put("8859_3", "ISO-8859-3");	    java2mime.put("iso8859_3", "ISO-8859-3");	    java2mime.put("iso8859-3", "ISO-8859-3");	    java2mime.put("8859_4", "ISO-8859-4");	    java2mime.put("iso8859_4", "ISO-8859-4");	    java2mime.put("iso8859-4", "ISO-8859-4");	    java2mime.put("8859_5", "ISO-8859-5");	    java2mime.put("iso8859_5", "ISO-8859-5");	    java2mime.put("iso8859-5", "ISO-8859-5");	    java2mime.put("8859_6", "ISO-8859-6");	    java2mime.put("iso8859_6", "ISO-8859-6");	    java2mime.put("iso8859-6", "ISO-8859-6");	    java2mime.put("8859_7", "ISO-8859-7");	    java2mime.put("iso8859_7", "ISO-8859-7");	    java2mime.put("iso8859-7", "ISO-8859-7");	    java2mime.put("8859_8", "ISO-8859-8");	    java2mime.put("iso8859_8", "ISO-8859-8");	    java2mime.put("iso8859-8", "ISO-8859-8");	    java2mime.put("8859_9", "ISO-8859-9");	    java2mime.put("iso8859_9", "ISO-8859-9");	    java2mime.put("iso8859-9", "ISO-8859-9");	    java2mime.put("sjis", "Shift_JIS");	    java2mime.put("jis", "ISO-2022-JP");	    java2mime.put("iso2022jp", "ISO-2022-JP");	    java2mime.put("euc_jp", "euc-jp");	    java2mime.put("koi8_r", "koi8-r");	    java2mime.put("euc_cn", "euc-cn");	    java2mime.put("euc_tw", "euc-tw");	    java2mime.put("euc_kr", "euc-kr");	}	if (mime2java.isEmpty()) {	    mime2java.put("iso-2022-cn", "ISO2022CN");	    mime2java.put("iso-2022-kr", "ISO2022KR");	    mime2java.put("utf-8", "UTF8");	    mime2java.put("utf8", "UTF8");	    mime2java.put("ja_jp.iso2022-7", "ISO2022JP");	    mime2java.put("ja_jp.eucjp", "EUCJIS");	    mime2java.put("euc-kr", "KSC5601");	    mime2java.put("euckr", "KSC5601");	    mime2java.put("us-ascii", "ISO-8859-1");	    mime2java.put("x-us-ascii", "ISO-8859-1");	}    }    private static void loadMappings(LineInputStream is, Hashtable table) {	String currLine;	while (true) {	    try {		currLine = is.readLine();	    } catch (IOException ioex) {		break; // error in reading, stop	    }	    if (currLine == null) // end of file, stop		break;	    if (currLine.startsWith("--") && currLine.endsWith("--"))		// end of this table		break;		    // ignore empty lines and comments	    if (currLine.trim().length() == 0 || currLine.startsWith("#"))		continue;	    	    // A valid entry is of the form <key><separator><value>	    // where, <separator> := SPACE | HT. Parse this	    StringTokenizer tk = new StringTokenizer(currLine, " \t");	    try {		String key = tk.nextToken();		String value = tk.nextToken();		table.put(key.toLowerCase(Locale.ENGLISH), value);	    } catch (NoSuchElementException nex) { }	}    }    static final int ALL_ASCII 		= 1;    static final int MOSTLY_ASCII 	= 2;    static final int MOSTLY_NONASCII 	= 3;    /**      * Check if the given string contains non US-ASCII characters.     * @param	s	string     * @return		ALL_ASCII if all characters in the string      *			belong to the US-ASCII charset. MOSTLY_ASCII     *			if more than half of the available characters     *			are US-ASCII characters. Else MOSTLY_NONASCII.     */    static int checkAscii(String s) {	int ascii = 0, non_ascii = 0;	int l = s.length();	for (int i = 0; i < l; i++) {	    if (nonascii((int)s.charAt(i))) // non-ascii		non_ascii++;	    else		ascii++;	}	if (non_ascii == 0)	    return ALL_ASCII;	if (ascii > non_ascii)	    return MOSTLY_ASCII;	return MOSTLY_NONASCII;    }    /**      * Check if the given byte array contains non US-ASCII characters.     * @param	b	byte array     * @return		ALL_ASCII if all characters in the string      *			belong to the US-ASCII charset. MOSTLY_ASCII     *			if more than half of the available characters     *			are US-ASCII characters. Else MOSTLY_NONASCII.     *     * XXX - this method is no longer used     */    static int checkAscii(byte[] b) {	int ascii = 0, non_ascii = 0;	for (int i=0; i < b.length; i++) {	    // The '&' operator automatically causes b[i] to be promoted	    // to an int, and we mask out the higher bytes in the int 	    // so that the resulting value is not a negative integer.	    if (nonascii(b[i] & 0xff)) // non-ascii		non_ascii++;	    else		ascii++;	}		if (non_ascii == 0)	    return ALL_ASCII;	if (ascii > non_ascii)	    return MOSTLY_ASCII;		return MOSTLY_NONASCII;    }    /**      * Check if the given input stream contains non US-ASCII characters.     * Upto <code>max</code> bytes are checked. If <code>max</code> is     * set to <code>ALL</code>, then all the bytes available in this     * input stream are checked. If <code>breakOnNonAscii</code> is true     * the check terminates when the first non-US-ASCII character is     * found and MOSTLY_NONASCII is returned. Else, the check continues     * till <code>max</code> bytes or till the end of stream.     *     * @param	is	the input stream     * @param	max	maximum bytes to check for. The special value     *			ALL indicates that all the bytes in this input     *			stream must be checked.     * @param	breakOnNonAscii if <code>true</code>, then terminate the     *			the check when the first non-US-ASCII character     *			is found.     * @return		ALL_ASCII if all characters in the string      *			belong to the US-ASCII charset. MOSTLY_ASCII     *			if more than half of the available characters     *			are US-ASCII characters. Else MOSTLY_NONASCII.     */    static int checkAscii(InputStream is, int max, boolean breakOnNonAscii) {	int ascii = 0, non_ascii = 0;	int len;	int block = 4096;	int linelen = 0;	boolean longLine = false, badEOL = false;	boolean checkEOL = encodeEolStrict && breakOnNonAscii;	byte buf[] = null;	if (max != 0) {	    block = (max == ALL) ? 4096 : Math.min(max, 4096);	    buf = new byte[block]; 	}	while (max != 0) {	    try {		if ((len = is.read(buf, 0, block)) == -1)		    break;		int lastb = 0;		for (int i = 0; i < len; i++) {	    	    // The '&' operator automatically causes b[i] to 		    // be promoted to an int, and we mask out the higher		    // bytes in the int so that the resulting value is 		    // not a negative integer.		    int b = buf[i] & 0xff;		    if (checkEOL &&			    ((lastb == '\r' && b != '\n') ||			    (lastb != '\r' && b == '\n')))			badEOL = true;		    if (b == '\r' || b == '\n')			linelen = 0;		    else {			linelen++;			if (linelen > 998)	// 1000 - CRLF			    longLine = true;		    }		    if (nonascii(b)) {	// non-ascii		        if (breakOnNonAscii) // we are done			    return MOSTLY_NONASCII;		        else			    non_ascii++;		    } else		        ascii++;		    lastb = b;		}	    } catch (IOException ioex) {		break;	    }	    if (max != ALL)		max -= len;	}	if (max == 0 && breakOnNonAscii)	    // We have been told to break on the first non-ascii character.	    // We haven't got any non-ascii character yet, but then we	    // have not checked all of the available bytes either. So we	    // cannot say for sure that this input stream is ALL_ASCII,	    // and hence we must play safe and return MOSTLY_NONASCII	    return MOSTLY_NONASCII;	if (non_ascii == 0) { // no non-us-ascii characters so far	    // If we're looking at non-text data, and we saw CR without LF	    // or vice versa, consider this mostly non-ASCII so that it	    // will be base64 encoded (since the quoted-printable encoder	    // doesn't encode this case properly).	    if (badEOL)		return MOSTLY_NONASCII;	    // if we've seen a long line, we degrade to mostly ascii	    else if (longLine)		return MOSTLY_ASCII;	    else		return ALL_ASCII;	}	if (ascii > non_ascii) // mostly ascii	    return MOSTLY_ASCII;	return MOSTLY_NONASCII;    }    static final boolean nonascii(int b) {	return b >= 0177 || (b < 040 && b != '\r' && b != '\n' && b != '\t');    }}/** * An OutputStream that determines whether the data written to * it is all ASCII, mostly ASCII, or mostly non-ASCII. */class AsciiOutputStream extends OutputStream {    private boolean breakOnNonAscii;    private int ascii = 0, non_ascii = 0;    private int linelen = 0;    private boolean longLine = false;    private boolean badEOL = false;    private boolean checkEOL = false;    private int lastb = 0;    private int ret = 0;    public AsciiOutputStream(boolean breakOnNonAscii, boolean encodeEolStrict) {	this.breakOnNonAscii = breakOnNonAscii;	checkEOL = encodeEolStrict && breakOnNonAscii;    }    public void write(int b) throws IOException {	check(b);    }    public void write(byte b[]) throws IOException {	write(b, 0, b.length);    }    public void write(byte b[], int off, int len) throws IOException {	len += off;	for (int i = off; i < len ; i++)	    check(b[i]);    }    private final void check(int b) throws IOException {	b &= 0xff;	if (checkEOL &&		((lastb == '\r' && b != '\n') || (lastb != '\r' && b == '\n')))	    badEOL = true;	if (b == '\r' || b == '\n')	    linelen = 0;	else {	    linelen++;	    if (linelen > 998)	// 1000 - CRLF		longLine = true;	}	if (MimeUtility.nonascii(b)) { // non-ascii	    non_ascii++;	    if (breakOnNonAscii) {	// we are done		ret = MimeUtility.MOSTLY_NONASCII;		throw new EOFException();	    }	} else	    ascii++;	lastb = b;    }    /**     * Return ASCII-ness of data stream.     */    public int getAscii() {	if (ret != 0)	    return ret;	// If we're looking at non-text data, and we saw CR without LF	// or vice versa, consider this mostly non-ASCII so that it	// will be base64 encoded (since the quoted-printable encoder	// doesn't encode this case properly).	if (badEOL)	    return MimeUtility.MOSTLY_NONASCII;	else if (non_ascii == 0) { // no non-us-ascii characters so far	    // if we've seen a long line, we degrade to mostly ascii	    if (longLine)		return MimeUtility.MOSTLY_ASCII;	    else		return MimeUtility.ALL_ASCII;	}	if (ascii > non_ascii) // mostly ascii	    return MimeUtility.MOSTLY_ASCII;	return MimeUtility.MOSTLY_NONASCII;    }}

⌨️ 快捷键说明

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