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

📄 mhfolder.java

📁 一个用java写的mail.里面的代码值得我们去研究!学习。
💻 JAVA
字号:
/*** $Id: MHFolder.java,v 1.5 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.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.Vector;import javax.mail.Flags;import javax.mail.Folder;import javax.mail.Header;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Store;import javax.mail.internet.MimeMessage;/** * The folder class implementing local disk storage for messages. */public class MHFolder  extends Folder{  public static final String    DELIM = "/";  private boolean               debug_ = false;  private int                   maxMsgNum_ = 0;  private String                name_ = null;  private String                path_ = null;  private Vector                fileCache_ = null;  private Vector                dirCache_ = null;  private Vector                msgCache_ = null;  /**   * Constructor.   */  public  MHFolder( Store store, String path, boolean debug ) {    super( store );    debug_ = debug;    if ( debug_ )      System.err.println( "NEW MHFolder: path = '" + path + "'" );    if ( path == null || path.length() == 0 ) {    // special case where store is opening the first folder      path_ = ((MHStore)super.store).getPath();    } else if ( path.endsWith( DELIM ) ) {      path_ = path.substring( 0, path.length() );    } else {      path_ = path;    }    int xindex = path_.lastIndexOf( DELIM );    if ( xindex < 0 ) {      name_ = path_;      path_ = "";    } else {      name_ = path_.substring( xindex + 1 );      path_ = path_.substring( 0, xindex );    }    if ( debug_ ) {      System.err.println( "NEW MHFolder: name = '" + name_ + "'" );      System.err.println( "NEW MHFolder: path = '" + path_ + "'" );    }  }  /**   */  public String  getMessagePath( int msgnum ) {    String xname = (String)fileCache_.elementAt( msgnum - 1 );    return getFullName() + DELIM + xname;  }  /**   * Returns the name of this folder.   * <p>   * Implementation of javax.mail.Folder.getName().   */  public String  getName() {    return name_;  }  /**   * Returns the full name of this folder.   * <p>   * Implementation of javax.mail.Folder.getFullName().   */  public String  getFullName() {    if ( path_.length() == 0 )      return name_;    else      return path_ + DELIM + name_;  }  /**   * Returns the type of this folder.   * <p>   * Implementation of javax.mail.Folder.getType().   *   * @exception MessagingException if a messaging error occurred   */  public int  getType() throws MessagingException {    return Folder.HOLDS_FOLDERS + Folder.HOLDS_MESSAGES;  }  /**   * Return the open mode of this folder.   * <p>   * Implementation of javax.mail.Folder.getMode().   */  public int  getMode() {    return super.mode;  }  /**   * Indicates whether this folder exists.   * <p>   * Implementation of javax.mail.Folder.exists().   *   * @exception MessagingException if a messaging error occurred   */  public boolean  exists() throws MessagingException {    File xdir = new File( getFullName() );    return xdir.exists();  }  /**   * Indicates whether this folder contains new messages.   * <p>   * Implementation of javax.mail.Folder.hasNewMessages().   *   * @exception MessagingException if a messaging error occurred   */  public boolean  hasNewMessages() throws MessagingException {    return false;  }  /**   * Opens this folder.   * <p>   * Implementation of javax.mail.Folder.open().   *   * @exception MessagingException if a messaging error occurred   */  public void  open( int mode ) throws MessagingException  {    if ( debug_ )      System.err.println( "open: '" + name_ + "' mode " + mode );    checkCacheLoaded();    super.mode = mode;  }  /**   * Closes this folder.   * <p>   * Implementation of javax.mail.Folder.close().   *   * @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 ( debug_ )      System.err.println( "close: '" + name_ + "'" );  // expunge any messages if requested    if ( expunge && super.mode == READ_WRITE ) {      expunge();    }  // release all resources    fileCache_.removeAllElements();    fileCache_ = null;    dirCache_.removeAllElements();    dirCache_ = null;    msgCache_.removeAllElements();    msgCache_ = null;    super.mode = -1;  // set to unknown mode  }  /**   * Expunges this folder.   * This deletes all the messages marked as deleted.   * <p>   * Implementation of javax.mail.Folder.expunge().   *   * @exception MessagingException if a messaging error occurred   */  public Message[]  expunge() throws MessagingException {    if ( debug_ )      System.err.println( "expunge: '" + name_ + "'" );    checkCacheLoaded();    int    xsize = fileCache_.size();    Vector xdeleted = new Vector( xsize, 4 );    Vector xmsgCache = new Vector( xsize, 4 );    Vector xfileCache = new Vector( xsize, 4 );    for ( int i = 0 ; i < xsize ; ++i ) {      Message xmsg = (Message) msgCache_.elementAt(i);      if ( xmsg == null )        continue;      Flags xflags = xmsg.getFlags();      if ( xflags.contains( Flags.Flag.DELETED ) ) {        xdeleted.addElement( xmsg );      } else {        xmsgCache.addElement( xmsg );        xfileCache.addElement( fileCache_.elementAt( i ) );      }    }    for ( int i = 0 ; i < xdeleted.size() ; i++ ) {      Message xmessage = (Message)xdeleted.elementAt( i );      File xfile = new File( getMessagePath( xmessage.getMessageNumber() ) );      if ( xfile.exists() && xfile.isFile() ) {        xfile.delete();      }    }    fileCache_ = xfileCache;    msgCache_ = xmsgCache;  // return the deleted messages as an array    Message[] xarray = new Message[ xdeleted.size() ];    xdeleted.copyInto( xarray );    return xarray;  }  /**   * Indicates whether this folder is open.   * <p>   * Implementation of javax.mail.Folder.isOpen().   */  public boolean  isOpen() {    return ( super.mode == READ_ONLY || super.mode == READ_WRITE );  }  /**   * Returns this folder's Store.   */  public Store  getStore() {    return super.store;  }  /**   * Returns the permanent flags for this folder.   * <p>   * Implementation of javax.mail.Folder.getPermanentFlags().   */  public Flags  getPermanentFlags() {    return new Flags();  }    /**   * Returns the number of messages in this folder.   * <p>   * Implementation of javax.mail.Folder.getMessageCount().   *   * @exception MessagingException if a messaging error occurred   */  public int  getMessageCount() throws MessagingException {    if ( ! isOpen() ) return -1;    return fileCache_.size();  }  /**   * Returns the specified message number from this folder.   * <p>   * Implementation of javax.mail.Folder.getMessage().   */  public Message  getMessage( int msgnum ) throws MessagingException {    if ( debug_ )      System.err.println( "getMessage: MsgNum = " + msgnum +                          " cache.length = " + fileCache_.size() );    checkCacheLoaded();    MHMessage xmessage = (MHMessage) msgCache_.elementAt( msgnum - 1 );    if ( xmessage != null ) {      return xmessage;    }    File xfile = new File( getMessagePath( msgnum ) );    if ( xfile.exists() && xfile.isFile() ) {      xmessage = new MHMessage( this, msgnum );      msgCache_.setElementAt( xmessage, msgnum - 1 );    }    return xmessage;  }    /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.appendMessages().   */  public void  appendMessages( Message messages[] ) throws MessagingException {    if ( messages == null || messages.length == 0 )      return;    checkCacheLoaded();    if ( debug_ )      System.err.println( "appendMessages: messages '" + messages.length + "'" );    for ( int i = 0; i < messages.length ; ++i ) {      try {        if ( messages[i] instanceof MimeMessage ) {          maxMsgNum_++;          String xname = "" + maxMsgNum_;          String xpath = getFullName() + DELIM + xname;          File xfile = new File( xpath );          if ( debug_ )            System.err.println( "APPEND: write '" + xfile.getPath() + "'" );          FileOutputStream xstream = new FileOutputStream( xfile );          MimeMessage xmime = (MimeMessage) messages[i];          xmime.writeTo( xstream );          xstream.close();          MHMessage xmessage = new MHMessage( this, msgCache_.size() );          fileCache_.addElement( xname );          msgCache_.addElement( xmessage );        } else {          throw new MessagingException( "can only process MimeMessages, not '" +                                        messages[i].getClass().getName() + "'" );        }      } catch ( IOException ex ) {        throw new MessagingException( "while writing message, " +                                      ex.getMessage() );      }    }  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.getParent().   */  public Folder  getParent() throws MessagingException {    return super.store.getFolder( path_ );  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.list().   */  public Folder[]  list( String s ) throws MessagingException {    if ( debug_ )      System.err.println( "list: '" + name_ + "' nameSpace '" + s + "'" );    Vector xdirectories = new Vector();    checkCacheLoaded();    for ( int i = 0 ; i < dirCache_.size() ; i++ ) {      String xpath = getFullName() + DELIM + (String)dirCache_.elementAt( i );      File   xdir = new File( xpath );      if ( xdir.exists() && xdir.isDirectory() ) {        xdirectories.addElement( xpath );      }    }    MHFolder[] xfolders = new MHFolder[ xdirectories.size() ];    for ( int i = 0 ; i < xdirectories.size() ; ++i ) {      String xdir = (String) xdirectories.elementAt(i);      xfolders[i] = new MHFolder( super.store, xdir, debug_ );    }    return xfolders;  }  /**   *    */  private void  checkCacheLoaded() {    if ( debug_ )      System.err.println( "checkCacheLoaded: '" + name_ + "'" );    if ( fileCache_ == null ) {      maxMsgNum_ = 0;      File xdirectory = new File( getFullName() );      if ( debug_ )        System.err.println( "CHECKCACHE cache '" + getFullName() + "'" );      String[] xdirectories = xdirectory.list();      if ( xdirectories == null )        xdirectories = new String[0];      dirCache_ = new Vector( xdirectories.length, 4 );      fileCache_ = new Vector( xdirectories.length, 4 );      msgCache_ = new Vector( xdirectories.length, 4 );      for ( int i = 0 ; i < xdirectories.length ; ++i ) {      // only add public files and directories on UNIX        if ( xdirectories[i].startsWith( "." ) ) continue;        File xfile = new File( xdirectory, xdirectories[i] );        if ( xfile.exists() && xfile.isFile() ) {          try {            int xmsgNum = Integer.parseInt( xdirectories[i] );            fileCache_.addElement( xdirectories[i] );            msgCache_.addElement( null );            if ( xmsgNum > maxMsgNum_ )              maxMsgNum_ = xmsgNum;          } catch ( NumberFormatException ex ) {            // This is not a mail message file, skip it.          }        } else if ( xfile.exists() && xfile.isDirectory() ) {            dirCache_.addElement( xdirectories[i] );        }      }      if ( debug_ ) {        System.err.println( "CHECKCACHE loaded " + fileCache_.size() +                            " files, " + dirCache_.size() + " dirs." );      }    }  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.getSeparator().   */  public char  getSeparator() {    return DELIM.charAt( 0 );  }  /**   * The type is ignored.   * <p>   * Implementation of javax.mail.Folder.create().   */  public boolean  create( int type ) throws MessagingException {    if ( debug_ )      System.err.println( "create: '" + name_ + "' type = " + type );    String xpath = getFullName();    File   xdir = new File( xpath );    return xdir.mkdirs();  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.delete().   */  public boolean  delete( boolean flag ) throws MessagingException {    if ( debug_ )      System.err.println( "delete: '" + name_ + "' flag " + flag );    if ( fileCache_.size() > 0 )      return false;    String xpath = getFullName();    File   xdir = new File( xpath );    return xdir.delete();  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.renameTo().   */  public boolean  renameTo( Folder folder ) throws MessagingException {    if ( debug_ )      System.err.println( "renameTo: '" + name_ + "' folder " + folder );    String xpath = ((MHStore)super.store).getPath() + DELIM + getFullName();    String xnewPath = ((MHStore)super.store).getPath() + DELIM + folder.getFullName();    if ( debug_ )      System.err.println( "renameTo: old '" + xpath + "' new '" + xnewPath + "'" );    File xdir = new File( xpath );    File xnewDir = new File( xnewPath );    return xdir.renameTo( xnewDir );  }  /**   * aa bb.   * <p>   * Implementation of javax.mail.Folder.getFolder().   */  public Folder  getFolder( String s ) throws MessagingException {    if ( debug_ )      System.err.println( "getFolder: '" + name_ + "' folder " + s );  // REVIEW    return super.store.getFolder( getFullName() + DELIM + s );  }}

⌨️ 快捷键说明

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