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

📄 mailmessageheaders.java

📁 Java Mail Server JAVA编写的邮件服务器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     * *                                                              * * Copyright 2008 Jun Li(SiChuan University, the School of      * * Software Engineering). All rights reserved.                  * *                                                              * * Licensed to the JMS under one  or more contributor license   * * agreements.  See the LICENCE file  distributed with this     * * work for additional information regarding copyright          * * ownership.  The JMS licenses this file  you may not use this * * file except in compliance  with the License.                 * *                                                              * * Unless required by applicable law or agreed to in writing,   * * software distributed under the License is distributed on an  * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       * * KIND, either express or implied.  See the License for the    * * specific language governing permissions and limitations      * * under the License.                                           * ****************************************************************/package org.jpxx.mail.Message;import java.io.BufferedReader;import java.io.IOException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.NoSuchElementException;/** * Mail Message Headers. *  * @author Jun Li * @version $Revision: 0.0.1 $, $Date: 2008/04/29 12:21:00 $ */public class MailMessageHeaders {    /**     * The actual list of Headers, including placeholder entries.     * Placeholder entries are Headers with a null value and     * are never seen by clients of the InternetHeaders class.     * Placeholder entries are used to keep track of the preferred     * order of headers.  Headers are never actually removed from     * the list, they're converted into placeholder entries.     * New headers are added after existing headers of the same name     * (or before in the case of <code>Received</code> and     * <code>Return-Path</code> headers).  If no existing header     * or placeholder for the header is found, new headers are     * added after the special placeholder with the name ":".     */    protected List headers;    /**     * Create an empty MailMessageHeaders object.  Placeholder entries     * are inserted to indicate the preferred order of headers.     */    public MailMessageHeaders() {        headers = new ArrayList(40);        String headerArray[] = getDefaultMailMessageHeaderNames();        for (int i = 0; i < headerArray.length; i++) {            headers.add(new MailMessageHeader(headerArray[i], null));        }    }    /**     * Read and parse the given RFC822 message stream till the      * blank line separating the header from the body. The input      * stream is left positioned at the start of the body. The      * header lines are stored internally. <p>     *     * For efficiency, wrap a BufferedInputStream around the actual     * input stream and pass it as the parameter. <p>     *     * No placeholder entries are inserted; the original order of     * the headers is preserved.     *     * @param br Buffered Reader     */    public MailMessageHeaders(BufferedReader br) throws MessageException {        headers = new ArrayList(40);        try {            load(br);        } catch (IOException ioe) {            throw new MessageException();        }    }    /**     *      * Read and parse the given RFC822 message stream till the     * blank line separating the header from the body. Store the     * header lines inside this InternetHeaders object. The order     * of header lines is preserved. <p>     *     * Note that the header lines are added into this InternetHeaders     * object, so any existing headers in this object will not be     * affected.  Headers are added to the end of the existing list     * of headers, in order.     *     * @param br Buffered Reader     * @throws org.jpxx.mail.Message.MessageException     * @throws java.io.IOException     */    public void load(BufferedReader br) throws MessageException, IOException {        // Read header lines until a blank line. It is valid        // to have BodyParts with no header lines.        String line;        String prevline = null;	// the previous header line, as a string        // a buffer to accumulate the header in, when we know it's needed        StringBuffer lineBuffer = new StringBuffer();        do {            line = br.readLine();            if (line != null && (line.startsWith(" ") || line.startsWith("\t"))) {                // continuation of header                if (prevline != null) {                    lineBuffer.append(prevline);                    prevline = null;                }                lineBuffer.append("\r\n");                lineBuffer.append(line);            } else {                // new header                if (prevline != null) {                    addHeaderLine(prevline);                } else if (lineBuffer.length() > 0) {                    // store previous header first                    addHeaderLine(lineBuffer.toString());                    lineBuffer.setLength(0);                }                prevline = line;            }        } while (line != null && line.length() > 0);            }    /**     * Add an RFC822 header line to the header store.     * If the line starts with a space or tab (a continuation line),     * add it to the last header line in the list.  Otherwise,     * append the new header line to the list.  <p>     *     * Note that RFC822 headers can only contain US-ASCII characters     *     * @param	line	raw RFC822 header line     */    public void addHeaderLine(String line) {        try {            char c = line.charAt(0);            if (c == ' ' || c == '\t') {                int index = headers.size() - 1;                MailMessageHeader h = (MailMessageHeader) headers.get(index);                h.setLine(h.getLine() + "\r\n" + line);                headers.set(index, h);            } else {                MailMessageHeader header = new MailMessageHeader(line);                // Check Header                if(isHeader(header.name))                    headers.add(header);            }        } catch (StringIndexOutOfBoundsException e) {            // line is empty, ignore it            return;        } catch (NoSuchElementException e) {        }    }    /**     * Return all the values for the specified header. The     * values are String objects.  Returns <code>null</code>     * if no headers with the specified name exist.     *     * @param	name 	header name     * @return		array of header values, or null if none     */    public String[] getHeader(String name) {        Iterator e = headers.iterator();        List v = new ArrayList(); // accumulate return values        while (e.hasNext()) {            MailMessageHeader h = (MailMessageHeader) e.next();            if (name.equalsIgnoreCase(h.getName()) && h.getLine() != null) {                v.add(h.getValue());            }        }        if (v.size() == 0) {            return (null);        }        // convert List to an array for return        String r[] = new String[v.size()];        r = (String[]) v.toArray(r);        return (r);    }    /**     * Get all the headers for this header name, returned as a single     * String, with headers separated by the delimiter. If the     * delimiter is <code>null</code>, only the first header is      * returned.  Returns <code>null</code>     * if no headers with the specified name exist.     *     * @param	name 		header name     * @param   delimiter	delimiter     * @return                  the value fields for all headers with     *				this name, or null if none     */    public String getHeader(String name, String delimiter) {        String s[] = getHeader(name);        if (s == null) {            return null;        }        if ((s.length == 1) || delimiter == null) {            return s[0];        }        StringBuffer r = new StringBuffer(s[0]);        for (int i = 1; i < s.length; i++) {            r.append(delimiter);            r.append(s[i]);        }        return r.toString();    }    /**     * Change the first header line that matches name     * to have value, adding a new header if no existing header     * matches. Remove all matching headers but the first. <p>     *     * Note that RFC822 headers can only contain US-ASCII characters     *     * @param	name	header name     * @param	value	header value     */    public void setHeader(String name, String value) {        boolean found = false;        for (int i = 0; i < headers.size(); i++) {            MailMessageHeader h = (MailMessageHeader) headers.get(i);            if (name.equalsIgnoreCase(h.getName())) {                if (!found) {                    int j;                    if (h.getLine() != null &&                            (j = h.getLine().indexOf(':')) >= 0) {                        h.setLine(h.getLine().substring(0, j + 1) + " " + value);                    } else {                        h.setLine(name + ": " + value);                    }                    found = true;                } else {                    headers.remove(i);                    i--;    // have to look at i again                }            }        }        if (!found) {            addHeader(name, value);        }    }    /**

⌨️ 快捷键说明

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