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

📄 parameterlist.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				    break;				}				value = vv.value = decodeBytes(evalue, charset);			    }			} catch (NumberFormatException nex) {			    if (decodeParametersStrict)				throw new ParseException(nex.toString());			} catch (UnsupportedEncodingException uex) {			    if (decodeParametersStrict)				throw new ParseException(uex.toString());			} catch (StringIndexOutOfBoundsException ex) {			    if (decodeParametersStrict)				throw new ParseException(ex.toString());			}			// if anything went wrong decoding the value,			// we just use the original value (set above)		    } else {			value = (String)v;		    }		    sb.append(value);		    slist.remove(sname);		}		if (segment == 0) {		    // didn't find any segments at all		    list.remove(name);		} else {		    mv.value = sb.toString();		    list.put(name, mv);		}	    }	    success = true;	} finally {	    /*	     * If we get here because of an exception that's going to	     * be thrown (success == false) from the constructor	     * (keepConsistentOnFailure == false), this is all wasted effort.	     */	    if (keepConsistentOnFailure || success)  {		// we should never end up with anything in slist,		// but if we do, add it all to list		if (slist.size() > 0) {		    // first, decode any values that we'll add to the list		    Iterator sit = slist.values().iterator();		    while (sit.hasNext()) {			Object v = sit.next();			if (v instanceof Value) {			    Value vv = (Value)v;			    Value vnew = decodeValue(vv.encodedValue);			    vv.charset = vnew.charset;			    vv.value = vnew.value;			}		    }		    list.putAll(slist);		}		// clear out the set of names and segments		multisegmentNames.clear();		slist.clear();	    }	}    }    /**     * Return the number of parameters in this list.     *      * @return  number of parameters.     */    public int size() {	return list.size();    }    /**     * Returns the value of the specified parameter. Note that      * parameter names are case-insensitive.     *     * @param name	parameter name.     * @return		Value of the parameter. Returns      *			<code>null</code> if the parameter is not      *			present.     */    public String get(String name) {	String value;	Object v = list.get(name.trim().toLowerCase(Locale.ENGLISH));	if (v instanceof MultiValue)	    value = ((MultiValue)v).value;	else if (v instanceof Value)	    value = ((Value)v).value;	else	    value = (String)v;	return value;    }    /**     * Set a parameter. If this parameter already exists, it is     * replaced by this new value.     *     * @param	name 	name of the parameter.     * @param	value	value of the parameter.     */    public void set(String name, String value) {	// XXX - an incredible kludge used by the IMAP provider	// to indicate that it's done setting parameters	if (name == null && value != null && value.equals("DONE")) {	    /*	     * If we've accumulated any multi-segment names from calls to	     * the set method from the IMAP provider, combine the pieces.	     * Ignore any parse errors (e.g., from decoding the values)	     * because it's too late to report them.	     */	    if (decodeParameters && multisegmentNames.size() > 0) {		try {		    combineMultisegmentNames(true);		} catch (ParseException pex) {		    // too late to do anything about it		}	    }	    return;	}	name = name.trim().toLowerCase(Locale.ENGLISH);	if (decodeParameters) {	    try {		putEncodedName(name, value);	    } catch (ParseException pex) {		// ignore it		list.put(name, value);	    }	} else	    list.put(name, value);    }    /**     * Set a parameter. If this parameter already exists, it is     * replaced by this new value.  If the     * <code>mail.mime.encodeparameters</code> System property     * is true, and the parameter value is non-ASCII, it will be     * encoded with the specified charset, as specified by RFC 2231.     *     * @param	name 	name of the parameter.     * @param	value	value of the parameter.     * @param	charset	charset of the parameter value.     * @since	JavaMail 1.4     */    public void set(String name, String value, String charset) {	if (encodeParameters) {	    Value ev = encodeValue(value, charset);	    // was it actually encoded?	    if (ev != null)		list.put(name.trim().toLowerCase(Locale.ENGLISH), ev);	    else		set(name, value);	} else	    set(name, value);    }    /**     * Removes the specified parameter from this ParameterList.     * This method does nothing if the parameter is not present.     *     * @param	name	name of the parameter.     */    public void remove(String name) {	list.remove(name.trim().toLowerCase(Locale.ENGLISH));    }    /**     * Return an enumeration of the names of all parameters in this     * list.     *     * @return Enumeration of all parameter names in this list.     */    public Enumeration getNames() {	return new ParamEnum(list.keySet().iterator());    }    /**     * Convert this ParameterList into a MIME String. If this is     * an empty list, an empty string is returned.     *     * @return		String     */    public String toString() {	return toString(0);    }    /**     * Convert this ParameterList into a MIME String. If this is     * an empty list, an empty string is returned.     *        * The 'used' parameter specifies the number of character positions     * already taken up in the field into which the resulting parameter     * list is to be inserted. It's used to determine where to fold the     * resulting parameter list.     *     * @param used      number of character positions already used, in     *                  the field into which the parameter list is to     *                  be inserted.     * @return          String     */      public String toString(int used) {        ToStringBuffer sb = new ToStringBuffer(used);        Iterator e = list.keySet().iterator();         while (e.hasNext()) {            String name = (String)e.next();	    Object v = list.get(name);	    if (v instanceof MultiValue) {		MultiValue vv = (MultiValue)v;		String ns = name + "*";		for (int i = 0; i < vv.size(); i++) {		    Object va = vv.get(i);		    if (va instanceof Value)			sb.addNV(ns + i + "*", ((Value)va).encodedValue);		    else			sb.addNV(ns + i, (String)va);		}	    } else if (v instanceof Value)		sb.addNV(name + "*", ((Value)v).encodedValue);	    else		sb.addNV(name, (String)v);        }        return sb.toString();    }    /**     * A special wrapper for a StringBuffer that keeps track of the     * number of characters used in a line, wrapping to a new line     * as necessary; for use by the toString method.     */    private static class ToStringBuffer {	private int used;	// keep track of how much used on current line	private StringBuffer sb = new StringBuffer();	public ToStringBuffer(int used) {	    this.used = used;	}	public void addNV(String name, String value) {	    value = quote(value);	    sb.append("; ");	    used += 2;	    int len = name.length() + value.length() + 1;	    if (used + len > 76) { // overflows ...		sb.append("\r\n\t"); // .. start new continuation line		used = 8; // account for the starting <tab> char	    }	    sb.append(name).append('=');	    used += name.length() + 1;	    if (used + value.length() > 76) { // still overflows ...		// have to fold value		String s = MimeUtility.fold(used, value);		sb.append(s);		int lastlf = s.lastIndexOf('\n');		if (lastlf >= 0)	// always true		    used += s.length() - lastlf - 1;		else		    used += s.length();	    } else {		sb.append(value);		used += value.length();	    }	}	public String toString() {	    return sb.toString();	}    }     // Quote a parameter value token if required.    private static String quote(String value) {	return MimeUtility.quote(value, HeaderTokenizer.MIME);    }    private static final char hex[] = {	'0','1', '2', '3', '4', '5', '6', '7',	'8','9', 'A', 'B', 'C', 'D', 'E', 'F'    };    /**     * Encode a parameter value, if necessary.     * If the value is encoded, a Value object is returned.     * Otherwise, null is returned.     * XXX - Could return a MultiValue object if parameter value is too long.     */    private static Value encodeValue(String value, String charset) {	if (MimeUtility.checkAscii(value) == MimeUtility.ALL_ASCII)	    return null;	// no need to encode it	byte[] b;	// charset encoded bytes from the string	try {	    b = value.getBytes(MimeUtility.javaCharset(charset));	} catch (UnsupportedEncodingException ex) {	    return null;	}	StringBuffer sb = new StringBuffer(b.length + charset.length() + 2);	sb.append(charset).append("''");	for (int i = 0; i < b.length; i++) {	    char c = (char)(b[i] & 0xff);	    // do we need to encode this character?	    if (c <= ' ' || c >= 0x7f || c == '*' || c == '\'' || c == '%' ||		    HeaderTokenizer.MIME.indexOf(c) >= 0) {		sb.append('%').append(hex[c>>4]).append(hex[c&0xf]);	    } else		sb.append(c);	}	Value v = new Value();	v.charset = charset;	v.value = value;	v.encodedValue = sb.toString();	return v;    }    /**     * Decode a parameter value.     */    private static Value decodeValue(String value) throws ParseException {	Value v = new Value();	v.encodedValue = value;	v.value = value;	// in case we fail to decode it	try {	    int i = value.indexOf('\'');	    if (i <= 0) {		if (decodeParametersStrict)		    throw new ParseException(			"Missing charset in encoded value: " + value);		return v;	// not encoded correctly?  return as is.	    }	    String charset = value.substring(0, i);	    int li = value.indexOf('\'', i + 1);	    if (li < 0) {		if (decodeParametersStrict)		    throw new ParseException(			"Missing language in encoded value: " + value);		return v;	// not encoded correctly?  return as is.	    }	    String lang = value.substring(i + 1, li);	    value = value.substring(li + 1);	    v.charset = charset;	    v.value = decodeBytes(value, charset);	} catch (NumberFormatException nex) {	    if (decodeParametersStrict)		throw new ParseException(nex.toString());	} catch (UnsupportedEncodingException uex) {	    if (decodeParametersStrict)		throw new ParseException(uex.toString());	} catch (StringIndexOutOfBoundsException ex) {	    if (decodeParametersStrict)		throw new ParseException(ex.toString());	}	return v;    }    /**     * Decode the encoded bytes in value using the specified charset.     */    private static String decodeBytes(String value, String charset)				throws UnsupportedEncodingException {	/*	 * Decode the ASCII characters in value	 * into an array of bytes, and then convert	 * the bytes to a String using the specified	 * charset.  We'll never need more bytes than	 * encoded characters, so use that to size the	 * array.	 */	byte[] b = new byte[value.length()];	int i, bi;	for (i = 0, bi = 0; i < value.length(); i++) {	    char c = value.charAt(i);	    if (c == '%') {		String hex = value.substring(i + 1, i + 3);		c = (char)Integer.parseInt(hex, 16);		i += 2;	    }	    b[bi++] = (byte)c;	}	return new String(b, 0, bi, MimeUtility.javaCharset(charset));    }}

⌨️ 快捷键说明

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