📄 internetaddress.java
字号:
StringBuffer sb = new StringBuffer(len + 2); sb.append('"').append(phrase).append('"'); return sb.toString(); } else return phrase; } private static String unquote(String s) { if (s.startsWith("\"") && s.endsWith("\"")) { s = s.substring(1, s.length() - 1); // check for any escaped characters if (s.indexOf('\\') >= 0) { StringBuffer sb = new StringBuffer(s.length()); // approx for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '\\' && i < s.length() - 1) c = s.charAt(++i); sb.append(c); } s = sb.toString(); } } return s; } /** * The equality operator. */ public boolean equals(Object a) { if (!(a instanceof InternetAddress)) return false; String s = ((InternetAddress)a).getAddress(); if (s == address) return true; if (address != null && address.equalsIgnoreCase(s)) return true; return false; } /** * Compute a hash code for the address. */ public int hashCode() { if (address == null) return 0; else return address.toLowerCase(Locale.ENGLISH).hashCode(); } /** * Convert the given array of InternetAddress objects into * a comma separated sequence of address strings. The * resulting string contains only US-ASCII characters, and * hence is mail-safe. <p> * * @param addresses array of InternetAddress objects * @exception ClassCastException, if any address object in the * given array is not an InternetAddress object. Note * that this is a RuntimeException. * @return comma separated string of addresses */ public static String toString(Address[] addresses) { return toString(addresses, 0); } /** * Convert the given array of InternetAddress objects into * a comma separated sequence of address strings. The * resulting string contains only US-ASCII characters, and * hence is mail-safe. <p> * * The 'used' parameter specifies the number of character positions * already taken up in the field into which the resulting address * sequence string is to be inserted. It is used to determine the * line-break positions in the resulting address sequence string. * * @param addresses array of InternetAddress objects * @param used number of character positions already used, in * the field into which the address string is to * be inserted. * @exception ClassCastException, if any address object in the * given array is not an InternetAddress object. Note * that this is a RuntimeException. * @return comma separated string of addresses */ public static String toString(Address[] addresses, int used) { if (addresses == null || addresses.length == 0) return null; StringBuffer sb = new StringBuffer(); for (int i = 0; i < addresses.length; i++) { if (i != 0) { // need to append comma sb.append(", "); used += 2; } String s = addresses[i].toString(); int len = lengthOfFirstSegment(s); // length till CRLF if (used + len > 76) { // overflows ... sb.append("\r\n\t"); // .. start new continuation line used = 8; // account for the starting <tab> char } sb.append(s); used = lengthOfLastSegment(s, used); } return sb.toString(); } /* Return the length of the first segment within this string. * If no segments exist, the length of the whole line is returned. */ private static int lengthOfFirstSegment(String s) { int pos; if ((pos = s.indexOf("\r\n")) != -1) return pos; else return s.length(); } /* * Return the length of the last segment within this string. * If no segments exist, the length of the whole line plus * <code>used</code> is returned. */ private static int lengthOfLastSegment(String s, int used) { int pos; if ((pos = s.lastIndexOf("\r\n")) != -1) return s.length() - pos - 2; else return s.length() + used; } /** * Return an InternetAddress object representing the current user. * The entire email address may be specified in the "mail.from" * property. If not set, the "mail.user" and "mail.host" properties * are tried. If those are not set, the "user.name" property and * <code>InetAddress.getLocalHost</code> method are tried. * Security exceptions that may occur while accessing this information * are ignored. If it is not possible to determine an email address, * null is returned. * * @param session Session object used for property lookup * @return current user's email address */ public static InternetAddress getLocalAddress(Session session) { String user=null, host=null, address=null; try { if (session == null) { user = System.getProperty("user.name"); host = InetAddress.getLocalHost().getHostName(); } else { address = session.getProperty("mail.from"); if (address == null) { user = session.getProperty("mail.user"); if (user == null || user.length() == 0) user = session.getProperty("user.name"); if (user == null || user.length() == 0) user = System.getProperty("user.name"); host = session.getProperty("mail.host"); if (host == null || host.length() == 0) { InetAddress me = InetAddress.getLocalHost(); if (me != null) host = me.getHostName(); } } } if (address == null && user != null && user.length() != 0 && host != null && host.length() != 0) address = user + "@" + host; if (address != null) return new InternetAddress(address); } catch (SecurityException sex) { // ignore it } catch (AddressException ex) { // ignore it } catch (UnknownHostException ex) { } // ignore it return null; } /** * Parse the given comma separated sequence of addresses into * InternetAddress objects. Addresses must follow RFC822 syntax. * * @param addresslist comma separated address strings * @return array of InternetAddress objects * @exception AddressException if the parse failed */ public static InternetAddress[] parse(String addresslist) throws AddressException { return parse(addresslist, true); } /** * Parse the given sequence of addresses into InternetAddress * objects. If <code>strict</code> is false, simple email addresses * separated by spaces are also allowed. If <code>strict</code> is * true, many (but not all) of the RFC822 syntax rules are enforced. * In particular, even if <code>strict</code> is true, addresses * composed of simple names (with no "@domain" part) are allowed. * Such "illegal" addresses are not uncommon in real messages. <p> * * Non-strict parsing is typically used when parsing a list of * mail addresses entered by a human. Strict parsing is typically * used when parsing address headers in mail messages. * * @param addresslist comma separated address strings * @param strict enforce RFC822 syntax * @return array of InternetAddress objects * @exception AddressException if the parse failed */ public static InternetAddress[] parse(String addresslist, boolean strict) throws AddressException { return parse(addresslist, strict, false); } /** * Parse the given sequence of addresses into InternetAddress * objects. If <code>strict</code> is false, the full syntax rules for * individual addresses are not enforced. If <code>strict</code> is * true, many (but not all) of the RFC822 syntax rules are enforced. <p> * * To better support the range of "invalid" addresses seen in real * messages, this method enforces fewer syntax rules than the * <code>parse</code> method when the strict flag is false * and enforces more rules when the strict flag is true. If the * strict flag is false and the parse is successful in separating out an * email address or addresses, the syntax of the addresses themselves * is not checked. * * @param addresslist comma separated address strings * @param strict enforce RFC822 syntax * @return array of InternetAddress objects * @exception AddressException if the parse failed * @since JavaMail 1.3 */ public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException { return parse(addresslist, strict, true); } /* * RFC822 Address parser. * * XXX - This is complex enough that it ought to be a real parser, * not this ad-hoc mess, and because of that, this is not perfect. * * XXX - Deal with encoded Headers too. */ private static InternetAddress[] parse(String s, boolean strict, boolean parseHdr) throws AddressException { int start, end, index, nesting; int start_personal = -1, end_personal = -1; int length = s.length(); boolean in_group = false; // we're processing a group term boolean route_addr = false; // address came from route-addr term boolean rfc822 = false; // looks like an RFC822 address char c; Vector v = new Vector(); InternetAddress ma; for (start = end = -1, index = 0; index < length; index++) { c = s.charAt(index); switch (c) { case '(': // We are parsing a Comment. Ignore everything inside. // XXX - comment fields should be parsed as whitespace, // more than one allowed per address rfc822 = true; if (start >= 0 && end == -1) end = index; if (start_personal == -1) start_personal = index + 1; for (index++, nesting = 1; index < length && nesting > 0; index++) { c = s.charAt(index); switch (c) { case '\\': index++; // skip both '\' and the escaped char break; case '(': nesting++; break; case ')': nesting--; break; default: break; } } if (nesting > 0) throw new AddressException("Missing ')'", s, index); index--; // point to closing paren if (end_personal == -1) end_personal = index; break; case ')': throw new AddressException("Missing '('", s, index); case '<': rfc822 = true; if (route_addr) throw new AddressException("Extra route-addr", s, index); if (!in_group) { start_personal = start; if (start_personal >= 0) end_personal = index; start = index + 1; } boolean inquote = false; outf: for (index++; index < length; index++) { c = s.charAt(index); switch (c) { case '\\': // XXX - is this needed? index++; // skip both '\' and the escaped char break; case '"': inquote = !inquote; break; case '>': if (inquote) continue; break outf; // out of for loop default: break; } } if (index >= length) { if (inquote) throw new AddressException("Missing '\"'", s, index); else throw new AddressException("Missing '>'", s, index); } route_addr = true; end = index; break; case '>': throw new AddressException("Missing '<'", s, index); case '"': // parse quoted string rfc822 = true; if (start == -1) start = index; outq:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -