📄 message.java
字号:
addHeaderLine(name + ": " + value); } } else { if (value != null) { setHeaderLine(i, name + ": " + value); } else { removeHeaderLine(i); } } } /** * Returns a line of the message's body by its index. * * @see #getBodyLineCount * @see #setBodyLine * @see #addBodyLine * @see #removeBodyLine * @see #insertBodyLine */ public String getBodyLine(int index) { if ((index < 0) || (index >= getBodyLineCount())) { throw new ArrayIndexOutOfBoundsException(index); } return (String)lines.elementAt(headerSize + index + 1); } /** * Replaces the line of the message's body at the given index. * * @see #getBodyLine * @see #getBodyLineCount * @see #addBodyLine * @see #removeBodyLine * @see #insertBodyLine */ public void setBodyLine(int index, String line) { if ((index < 0) || (index >= getBodyLineCount())) { throw new ArrayIndexOutOfBoundsException(index); } lines.setElementAt(line, headerSize + index + 1); } /** * Returns the number of lines in the message's body. * * @see #getBodyLine * @see #setBodyLine * @see #addBodyLine * @see #removeBodyLine * @see #insertBodyLine */ public int getBodyLineCount() { return lines.size() - headerSize - 1; } /** * Adds a line to the message body, returning its index. * * @see #getBodyLine * @see #getBodyLineCount * @see #setBodyLine * @see #removeBodyLine * @see #insertBodyLine */ public int addBodyLine(String line) { lines.addElement(line); return getBodyLineCount() - 1; } /** * Inserts a new line into the message's body, at the given index. * * @see #getBodyLine * @see #getBodyLineCount * @see #setBodyLine * @see #addBodyLine * @see #removeBodyLine */ public void insertBodyLine(int index, String line) { if ((index < 0) || (index > getBodyLineCount())) { throw new ArrayIndexOutOfBoundsException(index); } lines.insertElementAt(line, headerSize + index + 1); } /** * Removes a line from the message's body. * * @see #getBodyLine * @see #getBodyLineCount * @see #setBodyLine * @see #addBodyLine * @see #insertBodyLine */ public void removeBodyLine(int index) { if ((index < 0) || (index >= getBodyLineCount())) { throw new ArrayIndexOutOfBoundsException(index); } lines.removeElementAt(headerSize + index + 1); } /** * Returns the machine-readable part of an address, that is, the part * that is actually used in delivering the message to a recipient. * For an address like, say, * <pre> * "Joerg Pleumann" <joerg@pleumann.de> * </pre> * this would be "joerg@pleumann.de" (without the quotes). */ public static String getMachineAddress(String address) { int p, q; p = address.indexOf('<'); q = address.indexOf('>', p + 1); if ((p != -1) && (q != -1)) return address.substring(p + 1, q); p = address.indexOf('('); q = address.indexOf(')', p + 1); if ((p != -1) && (q != -1)) return address.substring(0, p).trim(); p = address.indexOf('"'); q = address.indexOf('"', p + 1); if ((p != -1) && (q != -1)) return address.substring(0, p).trim(); return address; } /* Some additional requirements from the RFC that have to be addressed by a *real* address parser: - Several addresses in one line, like: addr1, addr2, addr3 - A named list of addresses like: friends: a, b, c; - Everything in (...) is a comment - Everything in "..." is a literal - Everything in <...> is a machine-readable part - Using escape sequences \... is also allowed. */ /** * Returns the human-readable part of an address, that is, the part * that usually holds the real-life name of a user. For an address * like, say, * <pre> * "Joerg Pleumann" <joerg@pleumann.de> * </pre> * this would be "Joerg Pleumann" (without the quotes). */ public static String getDisplayAddress(String address) { int p, q; p = address.indexOf('"'); q = address.indexOf('"', p + 1); if ((p != -1) && (q != -1)) return address.substring(p + 1, q); p = address.indexOf('('); q = address.indexOf(')', p + 1); if ((p != -1) && (q != -1)) return address.substring(p + 1, q); p = address.indexOf('<'); q = address.indexOf('>', p + 1); if ((p != -1) && (q != -1)) return address.substring(0, p).trim(); return ""; } /** * Returns a canonical representation of an e-mail address. It basically * consists of the username and domain parts enclosed in angular brackets. * It does not contain the user's realname. * [May be removed in favour of getMachineAddress or expanded to hold * display *and* machine address.] */ /* public static String getCanonicalAddress(String address) { int p = address.indexOf('<'); int q = address.indexOf('>', p + 1); if ((p != -1) && (q != -1)) return address.substring(p, q + 1); p = address.indexOf('('); q = address.indexOf(')', p + 1); if ((p != -1) && (q != -1)) return "<" + address.substring(0, p) + ">"; p = address.indexOf('"'); q = address.indexOf('"', p + 1); if ((p != -1) && (q != -1)) return "<" + address.substring(0, p) + ">"; return "<" + address + ">"; } */ /** * Converts an integer to a string. If the result has less digits than the * specified length, it is left-padded with zeroes. This is a helper * method required by getCanonicalDate(). The length parameter must not * exceed 4, the number must not exceed 9999. Since the method is used * only internally, this should be easy to guarantee. */ private static String intToStr(int value, int length) { String result = Integer.toString(value); result = "0000".substring(result.length(), length) + result; return result; } /** * Returns a formatted date. This method converts the given date/time/zone * information into the textual format defined by RFC 822, that is, into a * string looking like this: * <pre> * Mon, 01 Jan 1970 23:59:59 GMT+1000 * </pre> * If the timezone parameter is null, no zone information is appended to * the result. */ public static String getCanonicalDate(Calendar calendar, TimeZone timezone) { String monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec"; String dayNames = "SunMonTueWedThuFriSatSun"; /** * Get the various fields from the calendar parameter. */ int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int weekday = calendar.get(Calendar.DAY_OF_WEEK) - 1; int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); /** * Format the first part of the result string. */ String result = dayNames.substring(3 * weekday, 3 * weekday + 3) + ", " + intToStr(day, 2) + " " + monthNames.substring(3 * month, 3 * month + 3) + " " + intToStr(year, 4) + " " + intToStr(hour, 2) + ":" + intToStr(minute, 2) + ":" + intToStr(second, 2); /** * If we have a timezone parameter, append the difference to GMT (=UCT) * including possible daylight savings to the string. The first two digits * hold hours, the next two digits hold minutes. So, GMT+0130 means * that local time is GMT plus one and a half hours. */ if (timezone != null) { int offset = timezone.getRawOffset() / 1000; if (timezone.useDaylightTime()) offset = offset + 3600; String name; if (offset >= 0) { name = " GMT+"; } else { name = " GMT-"; offset = -offset; } result = result + name + intToStr(offset / 3600, 2) + intToStr(offset % 3600, 2); } return result; } /** * Splits a list of elements into a String array. The method takes a String * holding a list of elements as well as a separator character. It splits the * list with regard to the given separator and puts each element into a newly * created String array. Occurences of the separator character enclosed in * double quotes are not treated as separators. The method can be used to * separate those message headers that hold several sub-attributes in one * value (for example a list of recipients in one "From:" field or the * various pieces of information stored in the "Content-Type:" field of a * MIME message. * * @see #getStringName * @see #getStringValue */ public static String[] getStringElements(String list, char separator){ Vector temp = new Vector(); list = list + separator; int len = list.length(); int p = 0; /** * Walk through the string as long as we're not done. From the * current starting position p, look for the next semicolon using a * second counter q. If a semicolon is enclosed in double quotes, * it's not the one we're looking for. */ while (p < len) { int q = p; boolean quote = false; while ((q < len) && ((list.charAt(q) != separator) || quote)) { if (list.charAt(q) == '"') quote = !quote; q++; } String element = list.substring(p, q).trim(); if (element.length() != 0) { temp.addElement(element); } p = q + 1; } /** * Create resulting String array from temporary Vector. */ String[] result = new String[temp.size()]; for (int i = 0; i < temp.size(); i++) { result[i] = (String)(temp.elementAt(i)); } return result; } /** * Returns the name contained in a name/value pair string. The name is * everything up to, but not including, the first ":" sign in the string. If * no ":" can be found, null is returned, assuming the string doesn't hold a * name at all. * * @see #getStringElements * @see #getStringValue */ public static String getStringName(String s) { int p = s.indexOf(':'); if (p == -1) { return null; } else { return s.substring(0, p); } } /** * Returns the value contained in a name/value pair string. The value is * everything following, but not including, the first ":" sign in the string. * If no ":" can be found, the whole string is returned, assuming it holds * only a value, but not name at all. The method unquotes values enclosed in * double quotes automatically. * * @see #getStringElements * @see #getStringName */ public static String getStringValue(String s) { String value; int p = s.indexOf(':'); if (p == -1) { value = s; } else { value = s.substring(p + 1); } /** * Get rid of leading and trailing spaces. */ value = value.trim(); /** * Unquote result, if necessary. */ if ((value.length() > 1) && (value.charAt(0) == '"') && ((value.charAt(value.length() - 1) == '"'))) { value = value.substring(1, value.length() - 1); } return value; } /** * Returns a random string. The method uses the system's millisecond timer * and an additional random number to create a string that can serve as a * basis for (hopefully) unique identifiers in messages or MIME parts. It is * recommended to use this string in conjunction with another string that * identifies the user or the user's system, for example an e-mail address * or a hostname, to ensure uniqueness across machine boundaries. */ public static String getRandomString() { return Long.toString(System.currentTimeMillis(), 36) + "." + Integer.toString(Math.abs(new Random().nextInt()), 36); } /** * Returns the message's internal list of lines. This method grants access to * the Vector that holds the message's header and body lines as well as the * empty line separating them. It is used for package-internal purposes only * (and thus has package visibility). As an example, the MimeDecoder class * needs to see this Vector to find out about all the MIME parts contained in * the message. The alternative would be to duplicate the whole Vector inside * a MimeDecoder, but since messages can become quite large, this would waste * a lot of memory. Another class that makes use of this method is SmtpClient. * * @see MimeDecoder * @see SmtpClient */ Vector getLines() { return lines; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -