1 21 22 package org.armedbear.j.mail; 23 24 import java.io.BufferedInputStream ; 25 import java.io.InvalidClassException ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.Serializable ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import org.armedbear.j.File; 33 import org.armedbear.j.Log; 34 import org.armedbear.j.Utilities; 35 36 public final class MboxSummary implements Serializable 37 { 38 private final ArrayList entries; 39 private final String path; 40 private long lastModified; 41 private long length; 42 43 public MboxSummary(File mailboxFile, List entries) 44 { 45 this.entries = new ArrayList (entries); 46 path = mailboxFile.canonicalPath(); 47 lastModified = mailboxFile.lastModified(); 48 length = mailboxFile.length(); 49 } 50 51 public synchronized ArrayList getEntries() 52 { 53 return entries; 54 } 55 56 public synchronized long length() 57 { 58 return length; 59 } 60 61 public synchronized long lastModified() 62 { 63 return lastModified; 64 } 65 66 public synchronized void write(File file) 67 { 68 try { 69 Log.debug("MboxSummary.write"); 70 long start = System.currentTimeMillis(); 71 File temp = Utilities.getTempFile(); 72 ObjectOutputStream objectOut = new ObjectOutputStream (temp.getOutputStream()); 73 objectOut.writeObject(this); 74 objectOut.flush(); 75 objectOut.close(); 76 Utilities.deleteRename(temp, file); 77 long elapsed = System.currentTimeMillis() - start; 78 Log.debug("MboxSummary.write completed " + elapsed + " ms"); 79 } 80 catch (Exception e) { 81 Log.error(e); 82 } 83 } 84 85 public static MboxSummary read(File file) 86 { 87 Log.debug("MboxSummary.read"); 88 if (file == null || !file.isFile()) 89 return null; 90 ObjectInputStream in = null; 91 try { 92 in = new ObjectInputStream (new BufferedInputStream (file.getInputStream())); 93 MboxSummary summary = (MboxSummary) in.readObject(); 94 File mailboxFile = File.getInstance(summary.path); 95 if (mailboxFile != null && mailboxFile.isFile()) { 96 if (summary.length == mailboxFile.length()) 97 if (summary.lastModified == mailboxFile.lastModified()) 98 return summary; 99 } 100 } 101 catch (InvalidClassException e) { 102 Log.debug(e); 105 } 106 catch (Exception e) { 107 Log.error(e); 108 } 109 finally { 110 if (in != null) { 111 try { 112 in.close(); 113 } 114 catch (IOException e) { 115 Log.error(e); 116 } 117 } 118 } 119 return null; 120 } 121 } 122 | Popular Tags |