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

📄 mboxfolder.java

📁 本系统是用 java 语言实现的一个 Email客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * MboxFolder.java * Copyright (C) 1999 dog <dog@dog.net.uk> *  * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. *  * You also have permission to link it with the Sun Microsystems, Inc.  * JavaMail(tm) extension and run that combination. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *  * You may retrieve the latest version of this library from * http://www.dog.net.uk/knife/ * * Contributor(s): Daniel Thor Kristjan <danielk@cat.nyu.edu> close and expunge clarification. *                 Sverre Huseby <sverrehu@online.no> gzipped mailboxes */package dog.mail.mbox;import java.io.*;import java.net.*;import java.text.*;import java.util.*;import java.util.zip.*;import javax.mail.*;import javax.mail.event.*;import javax.mail.internet.*;import dog.mail.util.*;/** * The folder class implementing a UNIX mbox-format mailbox. * * @author dog@dog.net.uk * @version 1.3.3 */public class MboxFolder extends Folder {	static final DateFormat df = new SimpleDateFormat("EEE MMM d H:m:s yyyy");        static final String KNIFE_MESSAGE_ID = "X-Knife-Message-Id";		File file;	Vector messages;	boolean open = false;	int type = HOLDS_MESSAGES;        boolean inbox = false;        long fileLastModified = 0;            /**	 * Constructor.	 */        protected MboxFolder(Store store, String filename, boolean isInbox) {	    super(store);	    file = new File(filename);	    if (file.exists() && file.isDirectory())		type = HOLDS_FOLDERS;	    inbox = isInbox;	}        /**	 * Constructor.	 */        protected MboxFolder(Store store, String filename) {	    this(store, filename, false);	}	/**	 * Returns the name of this folder.	 */	public String getName() {	    if ( ! inbox)		return file.getName();	    else		return ("INBOX");	}	/**	 * Returns the full name of this folder.	 */	public String getFullName() {	    if (! inbox)		return file.getAbsolutePath();	    else		return ("INBOX");	}	/**	 * Returns the type of this folder.	 * @exception MessagingException if a messaging error occurred	 */	public int getType() throws MessagingException {		return type;	}	/**	 * Indicates whether this folder exists.	 * @exception MessagingException if a messaging error occurred	 */	public boolean exists() throws MessagingException {		return file.exists();	}	/**	 * Indicates whether this folder contains new messages.	 * @exception MessagingException if a messaging error occurred	 */	public boolean hasNewMessages() throws MessagingException {		return getNewMessageCount()>0;	}	/**	 * Opens this folder.	 * @exception MessagingException if a messaging error occurred	 */	public void open(int mode) throws MessagingException {		switch (mode) {		  case READ_WRITE:			if (!file.canWrite())				throw new MessagingException("Folder is read-only");		  case READ_ONLY:		}		if (!file.canRead())			throw new MessagingException("Can't read folder");		try {			if (((MboxStore)store).getSession().getDebug())				System.err.println("DEBUG: mbox: opening "+file.getAbsolutePath());			BufferedReader reader = new BufferedReader(new InputStreamReader(new CRLFInputStream(getInputStream())));			String line = reader.readLine();			if (line!=null && !line.startsWith("From "))				throw new MessagingException("Mailbox format error", new ProtocolException());		} catch (IOException e) {			throw new MessagingException("Unable to open folder", e);		}		open = true;		notifyConnectionListeners(ConnectionEvent.OPENED);	}	/**	 * Closes this folder.	 * @param expunge if the folder is to be expunged before it is closed	 * @exception MessagingException if a messaging error occurred	 */	public void close(boolean expunge) throws MessagingException {		if (open) {			if (expunge) {			    expunge();			    open = false;			    notifyConnectionListeners(ConnectionEvent.CLOSED);			} else { 			    open = false;			    notifyConnectionListeners(ConnectionEvent.CLOSED);			    saveMessages();			}		}		if (((MboxStore)store).getSession().getDebug())			System.err.println("DEBUG: mbox: closing "+file.getAbsolutePath());	}		/**	 * Expunges this folder.	 * This deletes all the messages marked as deleted.	 * @exception MessagingException if a messaging error occurred	 */	public synchronized Message[] expunge() throws MessagingException {		Vector ve = new Vector();		if (open && messages!=null) {			Vector vm = new Vector();			for (Enumeration enum = messages.elements(); enum.hasMoreElements(); ) {				Message message = (Message)enum.nextElement();				Flags flags = message.getFlags();				if (flags.contains(Flags.Flag.DELETED)) {					ve.addElement(message);				} else {					vm.addElement(message);				}			}			messages = vm;		}		Message[] expunged = new Message[ve.size()];		ve.copyInto(expunged);		if (expunged.length>0)			notifyMessageRemovedListeners(true, expunged);		return expunged;	}		/**	 * Indicates whether this folder is open.	 */	public boolean isOpen() {		return open;	}        public Flags permanentFlags = null;	/**	 * Returns the permanent flags for this folder.	 */	public Flags getPermanentFlags() { 	    if (permanentFlags == null) {		Flags tmpFlags = new Flags(); 		tmpFlags.add(Flags.Flag.DELETED);		tmpFlags.add(Flags.Flag.SEEN);		tmpFlags.add(Flags.Flag.RECENT);		permanentFlags = tmpFlags;	    }	    return permanentFlags;	}		/**	 * Returns the number of messages in this folder.	 * @exception MessagingException if a messaging error occurred	 */	public int getMessageCount() throws MessagingException {		return getMessages().length;	}	/**	 * Returns the specified message number from this folder.	 * @exception MessagingException if a messaging error occurred	 */	public Message getMessage(int msgnum) throws MessagingException {		try {			return getMessages()[msgnum-1];		} catch (ArrayIndexOutOfBoundsException e) {			throw new MessagingException("No such message", e);		}	}	/**	 * Returns the messages in this folder.	 * @exception MessagingException if a messaging error occurred	 */	public synchronized Message[] getMessages() throws MessagingException {	    synchronizeMessages();	    saveMessages();	    Message[] m = new Message[messages.size()]; messages.copyInto(m);	    return m;	}	// Reads messages from the disk file.	private Vector readMessages() throws MessagingException {		synchronized (this) {			Vector tmpMessages = new Vector();			int count = 0;			try {				RandomAccessFile raf = new RandomAccessFile(file, "r");				String line;				for (line = raf.readLine(); line!=null; line = raf.readLine()) {					if (line.startsWith("From ")) { // new message						Message message = new MboxMessage(this, raf, count++);						tmpMessages.addElement(message);					}				}				raf.close();			} catch (IOException e) {			    throw new MessagingException("I/O error reading mailbox", e);			}			return tmpMessages;		}	}    /**     * Synchronizes the source file with the current message list.     */    private void synchronizeMessages() throws MessagingException {	if (file.lastModified() == fileLastModified)	    return;	Vector tmpMessages = readMessages();		// we should never be in the position where we've removed messages	// that haven't been removed from the file itself.  at least, let's	// hope so.  :)	// it should also be the case that messages are only appended to	// the file, so if we find a message that doesn't correspond to a	// current message, then it should be both a new message, and have	// no old messages after it.	// FIXME:  these are both really, really, really bad assumptions.	if (messages == null)	    messages = tmpMessages;	else {	    Vector messagesAdded = new Vector();	    Vector finalMessages = new Vector();	    	    for (int i =0,j = -1; i < tmpMessages.size(); i++) {		String tmpUniqueId = ((MboxMessage)tmpMessages.elementAt(i)).getMessageID();		String uniqueId = null;		while ( uniqueId != tmpUniqueId && ( !tmpUniqueId.equals(uniqueId)) && j < messages.size() - 1) { 		    uniqueId = ((MboxMessage)messages.elementAt(++j)).getMessageID();		}		if (j < messages.size() -1) {		    finalMessages.add(messages.elementAt(j));		} else {		    Object newMessage = tmpMessages.elementAt(i);		    finalMessages.add(newMessage);

⌨️ 快捷键说明

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