📄 mhmessage.java
字号:
/*** $Id: MHMessage.java,v 1.4 2001/05/07 12:38:11 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE.*/package org.icemail.javamail.mh;import java.lang.String;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.IOException;import java.util.Date;import java.util.Enumeration;import javax.mail.Address;import javax.mail.Flags;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import javax.mail.internet.InternetHeaders;import javax.activation.DataHandler;/** * Class MHMessage implements a "lazy" version of MimeMessage, instantiating * the message components only when necessary. * <p> * Simple analysis of MimeMessage access methods revealed that all access * boils down to accessing the headers or contents of the message, so * this implementation catches those access points and loads only what is * needed. Of course, the first load is immediately because headers need to * be obtained. However, the contents need not be in memory, * saving LOTS of memory when messages have attachments. * * @see javax.mail.internet.MimeMessage */public class MHMessage extends MimeMessage{ /** * Instantiates a "lazy" message; empty of course. * * @param folder the folder of creation * @param msgnum the message number */ public MHMessage( MHFolder folder, int msgnum ) { super( folder, msgnum ); } /** * Intantiates a full populated message from the input stream. * <p> * It's private to keep others from instantiating a message completely. * This constructor is called only when the contents need to be * brought into memory; giving us access to the protected contents. * * @param folder the folder of creation * @param in the input stream from which to load the contents of the message * @param msgnum the message number * @exception MessagingException the message could not be instantiated from the stream contents */ private MHMessage( MHFolder folder, InputStream in, int msgnum ) throws MessagingException { super( folder, in, msgnum ); }//------------------------------------------------------------// over-loading of MimeMessage access methods public Enumeration getAllHeaderLines() throws MessagingException {// System.err.println( "MHMessage.getAllHeaderLines()" ); loadHeaders(); return super.getAllHeaderLines(); } public Enumeration getAllHeaders() throws MessagingException {// System.err.println( "MHMessage.getAllHeaders()" ); loadHeaders(); return super.getAllHeaders(); } public Address[] getAllRecipients() throws MessagingException {// System.err.println( "MHMessage.getAllRecipients()" ); return super.getAllRecipients(); } public Object getContent() throws IOException, MessagingException {// System.err.println( "MHMessage.getContent()" ); return super.getContent(); } public String getContentID() throws MessagingException {// System.err.println( "MHMessage.getContentID()" ); return super.getContentID(); } public String[] getContentLanguage() throws MessagingException {// System.err.println( "MHMessage.getContentLanguage()" ); return super.getContentLanguage(); } public String getContentMD5() throws MessagingException {// System.err.println( "MHMessage.getContentMD5()" ); return super.getContentMD5(); } protected InputStream getContentStream() throws MessagingException {// System.err.println( "MHMessage.getContentStream()" ); loadContent(); return super.getContentStream(); } public String getContentType() throws MessagingException {// System.err.println( "MHMessage.getContentType()" ); return super.getContentType(); } public DataHandler getDataHandler() throws MessagingException {// System.err.println( "MHMessage.getDataHandler()" ); return super.getDataHandler(); } public String getDescription() throws MessagingException {// System.err.println( "MHMessage.getDescription()" ); return super.getDescription(); } public String getDisposition() throws MessagingException {// System.err.println( "MHMessage.getDisposition()" ); return super.getDisposition(); } public String getEncoding() throws MessagingException {// System.err.println( "MHMessage.getEncoding()" ); return super.getEncoding(); } public String getFileName() throws MessagingException {// System.err.println( "MHMessage.getFileName()" ); return super.getFileName(); } public Flags getFlags() throws MessagingException {// System.err.println( "MHMessage.getFlags()" ); return super.getFlags(); } public Address[] getFrom() throws MessagingException {// System.err.println( "MHMessage.getFrom()" ); return super.getFrom(); } public String[] getHeader(String name) throws MessagingException {// System.err.println( "MHMessage.getHeader( S )" ); loadHeaders(); return super.headers.getHeader( name ); } public String getHeader(String name, String delimiter) throws MessagingException {// System.err.println( "MHMessage.getHeader( S, S )" ); loadHeaders(); return super.headers.getHeader( name, delimiter ); } public InputStream getInputStream() throws IOException, MessagingException {// System.err.println( "MHMessage.getInputStream()" ); return super.getInputStream(); } public int getLineCount() throws MessagingException {// System.err.println( "MHMessage.getLineCount()" ); return super.getLineCount(); } public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {// System.err.println( "MHMessage.getMatchingHeaderLines()" ); return super.getMatchingHeaderLines( names ); } public Enumeration getMatchingHeaders(String[] names) throws MessagingException {// System.err.println( "MHMessage.getMatchingHeaders()" ); return super.getMatchingHeaders( names ); } public String getMessageID() throws MessagingException {// System.err.println( "MHMessage.getMessageID()" ); return super.getMessageID(); } public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {// System.err.println( "MHMessage.getNonMatchingHeaderLines()" ); return super.getNonMatchingHeaderLines( names ); } public Enumeration getNonMatchingHeaders(String[] names) throws MessagingException {// System.err.println( "MHMessage.getNonMatchingHeaders()" ); return super.getNonMatchingHeaders( names ); } public Date getReceivedDate() throws MessagingException {// System.err.println( "MHMessage.getReceivedDate()" ); return super.getReceivedDate(); } public Address[] getRecipients(Message.RecipientType type) throws MessagingException {// System.err.println( "MHMessage.getRecipients()" ); return super.getRecipients( type ); } public Address[] getReplyTo() throws MessagingException {// System.err.println( "MHMessage.getReplyTo()" ); return super.getReplyTo(); } public Date getSentDate() throws MessagingException {// System.err.println( "MHMessage.getSentDate()" ); return super.getSentDate(); } public int getSize() throws MessagingException {// System.err.println( "MHMessage.getSize()" ); return super.getSize(); } public String getSubject() throws MessagingException {// System.err.println( "MHMessage.getSubject()" ); return super.getSubject(); } public boolean isMimeType(String mimeType) throws MessagingException {// System.err.println( "MHMessage.isMimeType()" ); return super.isMimeType( mimeType ); } public boolean isSet(Flags.Flag flag) throws MessagingException {// System.err.println( "MHMessage.isSet()" ); return super.isSet( flag ); } public Message reply(boolean replyToAll) throws MessagingException {// System.err.println( "MHMessage.reply()" ); return super.reply( replyToAll ); }//------------------------------------------------------------// load either headers or content from the file storage /** * Load only the headers for the message from the underlying file storage. * In this case, only the headers are parsed from the beginning of the file, * which is what the Java Mail manual says. Who knows... * <p> * The folder provides the full path to the file, which is named after the * message number. Figure that! */ private void loadHeaders() throws MessagingException { if ( super.headers == null ) {// System.err.println( "MHMessage.loadHeaders()" ); String xpath = ((MHFolder)super.folder).getMessagePath( super.msgnum ); File xfile = new File( xpath ); try { FileInputStream xstream = new FileInputStream( xfile ); super.headers = new InternetHeaders( xstream ); xstream.close(); } catch ( IOException xex ) { throw new MessagingException( xex.toString() ); } } } /** * Load only the contents of the message from the underlying file storage. * In this case, a complete message is instantiated, of which the contents * are directly copied into this message. Shag me baby! * <p> * The folder provides the full path to the file, which is named after the * message number. Figure that! */ private void loadContent() throws MessagingException { if ( super.content == null || super.content.length == 0 ) {// System.err.println( "MHMessage.loadContent()" ); String xpath = ((MHFolder)super.folder).getMessagePath( super.msgnum ); File xfile = new File( xpath ); try { FileInputStream xstream = new FileInputStream( xfile ); MHMessage xmessage = new MHMessage( (MHFolder)super.folder, xstream, 777777 ); xstream.close(); super.content = new byte[ xmessage.content.length ]; System.arraycopy( xmessage.content, 0, super.content, 0, xmessage.content.length ); } catch ( IOException xex ) { throw new MessagingException( xex.toString() ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -