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

📄 messageindex.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * MessageIndex.java
 *
 * Copyright (C) 1998-2000 FreeBeans <freebeans@mub.biglobe.ne.jp>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Copyright (C) 1998-2000ꆼ FreeBeans <freebeans@mub.biglobe.ne.jp>
 *
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼFree Software Foundation 
 * ꆼꆼꆼꆼꆼGNU ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * GNU ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 *
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼGNU ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼFree Software Foundation, Inc., 675 Mass Ave,
 * Cambridge, MA 02139, USA ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
 */
package jp.gr.java_conf.roadster.net.pop;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;
import java.util.WeakHashMap;

import javax.mail.Flags;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetHeaders;

/**
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ POP3Folder ꆼꆼꆼꆼꆼꆼꆼ
 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
 */
public class MessageIndex {
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private static final String INDEX_FILE_NAME = "folder.idx";
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private static final String DATA_FILE_SUFFIX = ".dat";

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.(=16KB)
	 */
	private static final int BUFFER_SIZE = 16 * 1024;
	
	/**
	 * ꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private static Hashtable instances = new Hashtable();

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private static SaverThread saverThread;

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private File dataDirectory;
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private File indexFile;
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private Vector elements;
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼWeakHashMap ꆼꆼꆼꆼꆼꆼꆼ
	 * MessageIndexElementꆼꆼꆼꆼꆼꆼꆼInternetHeaders ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 */
	private static HashMap<String,SerializableInternetHeaders> HEADERS_MAP=new HashMap<String,SerializableInternetHeaders>();
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼWeakHashMap ꆼꆼꆼꆼꆼꆼꆼ
	 * MessageIndexElementꆼꆼꆼꆼꆼꆼꆼbyte[] ꆼꆼꆼꆼꆼꆼꆼcontentꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 */
	private static final WeakHashMap CONTENT_MAP=new WeakHashMap();
	
	private static boolean cacheContent=true;  //是否对内容进行缓存,搜索时不缓存

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * MessageIndex ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * MessageIndex ꆼꆼꆼꆼꆼꆼꆼꆼꆼgetInstance() ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	dataDirectory	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @see	#getInstance(java.io.File)
	 */
	private MessageIndex(File dataDirectory) {
		try {
			this.dataDirectory = dataDirectory;
			indexFile = new File(dataDirectory, INDEX_FILE_NAME);
			load();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ MessageIndex ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	dataDirectory	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public static synchronized MessageIndex getInstance(File dataDirectory) {
		if (saverThread == null) {
			saverThread = new SaverThread();
			saverThread.start();
		}
		MessageIndex mfi = (MessageIndex) instances.get(dataDirectory);
		if (mfi == null) {
			mfi = new MessageIndex(dataDirectory);
			instances.put(dataDirectory, mfi);
		}
		return mfi;
	}

	/**
	 * ꆼꆼꆼꆼꆼ elements ꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void initElements() {
		if (elements == null) {
			elements = new Vector();
			elements.addElement(INDEX_FILE_NAME);	// 0ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
		} else {
			elements.setSize(1);
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ POP3Message ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	folder	POP3Folder ꆼꆼꆼꆼꆼꆼꆼ.
	 * @param	msgnum	ꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.lang.IndexOutOfBoundsException	msgnum ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized POP3Message createMessage(POP3Folder folder, int msgnum) throws MessagingException {
		POP3Message message = new POP3Message(folder, msgnum);
		message.setInternalID(elements.elementAt(msgnum));
		return message;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	message	ꆼꆼꆼꆼꆼ.
	 * @return	ꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private synchronized InputStream getInputStream(POP3Message message) throws IOException {
		MessageIndexElement mie = (MessageIndexElement) message.getInternalID();
		return new BufferedInputStream(mie.getInputStream(dataDirectory), BUFFER_SIZE);
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼInternetHeadersꆼꆼꆼꆼ
	 * Java2ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼWeakHashMapꆼꆼꆼꆼꆼꆼꆼꆼꆼJDK1.1ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * JDK1.1ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼnewꆼꆼꆼꆼ
	 *
	 * @param	message	ꆼꆼꆼꆼꆼꆼ
	 * @return	InternetHeaders
	 * @throws	javax.mail.MessagingException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 */
	public synchronized InternetHeaders getHeaders(POP3Message message) throws MessagingException {
		SerializableInternetHeaders ret = null;
		MessageIndexElement mie = (MessageIndexElement) message.getInternalID();
		try {
			if (HEADERS_MAP != null) {
				synchronized (HEADERS_MAP) {
					ret = (SerializableInternetHeaders) HEADERS_MAP.get(mie.getFileName());
				}
			}
			if (ret == null) {	// ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
				InputStream is = getInputStream(message);
				ret = new SerializableInternetHeaders(is);
				is.close();
				if (HEADERS_MAP != null) {	// ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
					synchronized (HEADERS_MAP) {
						HEADERS_MAP.put(mie.getFileName(), ret);
					}
				}
			}
		} catch (IOException ioe) {
			throw new MessagingException("IOException", ioe);
		}
		return ret;
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼcontentꆼꆼꆼ byte[] ꆼꆼꆼꆼ
	 * Java2ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼWeakHashMapꆼꆼꆼꆼꆼꆼꆼꆼꆼJDK1.1ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * JDK1.1ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼnewꆼꆼꆼꆼ
	 *
	 * @param	message	ꆼꆼꆼꆼꆼꆼ
	 * @return	contentꆼꆼꆼbyte[]
	 * @throws	javax.mail.MessagingException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 */
	public synchronized byte[] getContentBytes(POP3Message message) throws MessagingException {
		byte[] ret = null;
		MessageIndexElement mie = (MessageIndexElement) message.getInternalID();
		try {
			if (CONTENT_MAP != null) {
				synchronized (CONTENT_MAP) {
					ret = (byte[]) CONTENT_MAP.get(mie);
				}
			}
			if (ret == null) {	// ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
				InputStream is = getInputStream(message);
				InternetHeaders headers = new SerializableInternetHeaders(is);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buff = new byte[16 * 1024];
				int len;
				while ((len = is.read(buff)) != -1) {
					baos.write(buff, 0, len);
				}
				is.close();
				baos.close();
				ret = baos.toByteArray();
				if (CONTENT_MAP != null && cacheContent) {	// ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
					synchronized (CONTENT_MAP) {
						CONTENT_MAP.put(mie, ret);
					}
				}
			}
		} catch (IOException ioe) {
			throw new MessagingException("IOException", ioe);
		}
		return ret;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼMessageꆼisExpunged()ꆼtrueꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 *
	 * @param	msgs	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *                  ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void append(Message msgs[]) {
		for (int i = 0; i < msgs.length; ++i) {
			try {
				Message message = msgs[i];
				if (! message.isExpunged()) {
					File file = createMessageFile();
					BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
					message.writeTo(bos);
					bos.close();

					MessageIndexElement mie = new MessageIndexElement();
					mie.setFileName(file.getName());
					mie.setFlags(message.getFlags());
					elements.addElement(mie);
					saverThread.enqueue(this);
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} catch (MessagingException me) {
				me.printStackTrace();
			}
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	msgs	ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void remove(POP3Message msgs[]) {
		Vector removeElements = new Vector();
		for (int i = 0; i < msgs.length; ++i) {
			MessageIndexElement elem = (MessageIndexElement) msgs[i].getInternalID();
			elem.delete(dataDirectory);
			removeElements.addElement(elem);
		}
		Enumeration e = removeElements.elements();
		while (e.hasMoreElements()) {
			elements.removeElement(e.nextElement());
		}
		saverThread.enqueue(this);
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 *
	 * @return	ꆼꆼꆼꆼꆼꆼꆼꆼꆼ true.
	 */
	public synchronized boolean removeAll() {
		initElements();
		String files[] = dataDirectory.list(
			new FilenameFilter() {
				public boolean accept(File dir, String name) {
					File file = new File(dir, name);
					if (file.isFile()) {
						return true;
					} else {
						return false;
					}
				}
			});
		boolean result = true;
		for (int i = 0; i < files.length; ++i) {
			File file = new File(dataDirectory, files[i]);
			if (! file.delete()) {
				result = false;
			}
		}
		return result;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	ꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized int getCount() {
		return elements.size() - 1;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private synchronized void save() throws IOException {
		if (elements.size() > 1 || indexFile.exists()) {
			FileOutputStream   fos = new FileOutputStream(indexFile);
			ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos, BUFFER_SIZE));
			oos.writeObject(elements);
			oos.close();
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @exception	java.io.IOException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	static synchronized void saveAll() throws IOException {
		if (saverThread != null) {
			saverThread.saveAllIndexes();
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @exception	java.io.IOException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private synchronized void load() throws IOException {
		initElements();
		try {
			FileInputStream   fis = new FileInputStream(indexFile);
			ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis, BUFFER_SIZE));
			elements = (Vector) ois.readObject();
			ois.close();
		} catch (FileNotFoundException fnfe) {
			recoverIndex();
		} catch (ClassNotFoundException cnfe) {
			recoverIndex();
		}
	}

	public synchronized void reload() throws IOException {
		load();
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	private void recoverIndex() throws IOException {
		initElements();

⌨️ 快捷键说明

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