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

📄 mboxfolder.java

📁 本系统是用 java 语言实现的一个 Email客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		    messagesAdded.add(newMessage);		}							    	    }	    messages = finalMessages;	    if (messagesAdded.size() > 0) {		Message[] n = new Message[messagesAdded.size()]; messagesAdded.copyInto(n);		notifyMessageAddedListeners(n);				saveMessages();	    }	}    }		// Saves messages to the disk file.	private void saveMessages() throws MessagingException {		synchronized (this) {			if (messages!=null) {				try {					Message[] m = new Message[messages.size()];					messages.copyInto(m);					// make sure content has been retrieved for all messages					for (int i=0; i<m.length; i++)						if (m[i] instanceof MboxMessage)							((MboxMessage)m[i]).retrieveContent();									OutputStream os = new BufferedOutputStream(getOutputStream());					MboxOutputStream mos = new MboxOutputStream(os);					for (int i=0; i<m.length; i++) {						Address[] f = m[i].getFrom();						String from = "-";						if (f.length>0) {							if (f[0] instanceof InternetAddress)								from = ((InternetAddress)f[0]).getAddress();							else								from = f[0].toString();						}						Date date = m[i].getSentDate();						if (date==null)							date = m[i].getReceivedDate();						if (date==null)							date = new Date();											String top = "From "+from+ " "+df.format(date)+"\n";						os.write(top.getBytes());						m[i].writeTo(mos);						mos.flush();					}					os.close();					fileLastModified = file.lastModified();				} catch (IOException e) {					throw new MessagingException("I/O error writing mailbox", e);				}			}		}	}		public synchronized void appendMessages(Message[] messages) throws MessagingException {		synchronizeMessages();		Vector added = new Vector();		for (int i=0; i<messages.length; i++) {			if (messages[i] instanceof MimeMessage) {				MboxMessage message = new MboxMessage(this, (MimeMessage)messages[i], i);				added.addElement(message);				this.messages.addElement(message);			}		}		if (added.size()>0) {			Message[] n = new Message[added.size()]; added.copyInto(n);			notifyMessageAddedListeners(n);		}		saveMessages();	}	/**	 * Does nothing.	 * The messages <i>must</i> be fetched in their entirety by getMessages() -	 * this is the nature of the Mbox protocol.	 * @exception MessagingException ignore	 */	public void fetch(Message amessage[], FetchProfile fetchprofile) throws MessagingException {	}	/**	 * Returns the parent folder.	 */	public Folder getParent() throws MessagingException {		return store.getFolder(file.getParent());	}	/**	 * Returns the subfolders of this folder.	 */	public Folder[] list() throws MessagingException {		if (type!=HOLDS_FOLDERS)			throw new MessagingException("This folder can't contain subfolders");		try {			String[] files = file.list();			Folder[] folders = new Folder[files.length];			for (int i=0; i<files.length; i++)				folders[i] = store.getFolder(file.getAbsolutePath()+File.separator+files[i]);			return folders;		} catch (SecurityException e) {			throw new MessagingException("Access denied", e);		}	}	/**	 * Returns the subfolders of this folder matching the specified pattern.	 */    public Folder[] list(String pattern) throws MessagingException {		if (type!=HOLDS_FOLDERS)			throw new MessagingException("This folder can't contain subfolders");		try {			String[] files = file.list(new MboxFilenameFilter(pattern));			Folder[] folders = new Folder[files.length];			for (int i=0; i<files.length; i++)				folders[i] = store.getFolder(file.getAbsolutePath()+File.separator+files[i]);			return folders;		} catch (SecurityException e) {			throw new MessagingException("Access denied", e);		}	}	/**	 * Returns the separator character.	 */	public char getSeparator() throws MessagingException {		return File.separatorChar;	}	/**	 * Creates this folder in the store.	 */	public boolean create(int type) throws MessagingException {		if (file.exists())			throw new MessagingException("Folder already exists");		switch (type) {		  case HOLDS_FOLDERS:			try {				file.mkdirs();				this.type = type;				notifyFolderListeners(FolderEvent.CREATED);				return true;			} catch (SecurityException e) {				throw new MessagingException("Access denied", e);			}		  case HOLDS_MESSAGES:			try {				// save the changes				synchronized (this) {                    if (messages==null) messages = new Vector();					OutputStream os = new BufferedOutputStream(getOutputStream());					Message[] m = new Message[messages.size()]; messages.copyInto(m);					for (int i=0; i<m.length; i++) {						Address[] f = m[i].getFrom();						String top = "From "+((f.length>0) ? f[0].toString() : "-")+" "+df.format(m[i].getSentDate())+"\n";						os.write(top.getBytes());						m[i].writeTo(os);					}					os.close();				}				this.type = type;				notifyFolderListeners(FolderEvent.CREATED);				return true;			} catch (IOException e) {				throw new MessagingException("I/O error writing mailbox", e);			} catch (SecurityException e) {				throw new MessagingException("Access denied", e);			}		}		return false;	}	/**	 * Deletes this folder.	 */	public boolean delete(boolean recurse) throws MessagingException {		if (recurse) {			try {				if (type==HOLDS_FOLDERS) {					Folder[] folders = list();					for (int i=0; i<folders.length; i++)						if (!folders[i].delete(recurse))							return false;				}				file.delete();				notifyFolderListeners(FolderEvent.DELETED);				return true;			} catch (SecurityException e) {				throw new MessagingException("Access denied", e);			}		} else {			try {				if (type==HOLDS_FOLDERS) {					Folder[] folders = list();					if (folders.length>0)						return false;				}				file.delete();				notifyFolderListeners(FolderEvent.DELETED);				return true;			} catch (SecurityException e) {				throw new MessagingException("Access denied", e);			}		}	}	/**	 * Mbox folders cannot be created, deleted, or renamed.	 */	public boolean renameTo(Folder folder) throws MessagingException {		try {            String filename = folder.getFullName();			if (filename!=null) {				file.renameTo(new File(filename));				((MboxStore)store).folders.clear();				notifyFolderListeners(FolderEvent.RENAMED);				return true;			} else				throw new MessagingException("Illegal filename: null");		} catch (SecurityException e) {			throw new MessagingException("Access denied", e);		}	}	/**	 * Mbox folders cannot contain subfolders.	 */	public Folder getFolder(String filename) throws MessagingException {		return store.getFolder(file.getAbsolutePath()+File.separator+filename);	}	/**	 * Checks if the current file is or is supposed to be	 * compressed. Uses the filename to figure it out.	 */	private boolean isGzip() {		return file.getName().toLowerCase().endsWith(".gz");	}	/**	 * Creates an output stream that possibly will compress	 * whatever is sent to it, based on the current filename.	 */	private OutputStream getOutputStream() throws IOException {		OutputStream out;		out = new FileOutputStream(file);		if (isGzip())			out = new GZIPOutputStream(out);		return out;	}    /**      * Locks this mailbox.  Not implementented yet.     */    public synchronized boolean acquireLock() {	return true;    }    /**     * Unlocks this mailbox.  Not implemented yet.     */    public synchronized boolean releaseLock() {	return true;    }	/**	 * Creates an input stream that possibly will decompress the	 * file contents.	 */	private InputStream getInputStream() throws IOException {		InputStream in;		in = new FileInputStream(file);		if (isGzip())			in = new GZIPInputStream(in);		return in;	}	class MboxFilenameFilter implements FilenameFilter {		String pattern;		int asteriskIndex, percentIndex;		MboxFilenameFilter(String pattern) {			this.pattern = pattern;			asteriskIndex = pattern.indexOf('*');			percentIndex = pattern.indexOf('%');		}				public boolean accept(File directory, String name) {			if (asteriskIndex>-1) {				String start = pattern.substring(0, asteriskIndex), end = pattern.substring(asteriskIndex+1, pattern.length());				return (name.startsWith(start) && name.endsWith(end));			} else if (percentIndex>-1) {				String start = pattern.substring(0, percentIndex), end = pattern.substring(percentIndex+1, pattern.length());				return (directory.equals(file) && name.startsWith(start) && name.endsWith(end));			}			return name.equals(pattern);		}			}    }

⌨️ 快捷键说明

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