KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > MboxSummary


1 /*
2  * MboxSummary.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: MboxSummary.java,v 1.1.1.1 2002/09/24 16:10:13 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.InvalidClassException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.Serializable JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
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 JavaDoc
37 {
38     private final ArrayList JavaDoc entries;
39     private final String JavaDoc path;
40     private long lastModified;
41     private long length;
42
43     public MboxSummary(File mailboxFile, List JavaDoc entries)
44     {
45         this.entries = new ArrayList JavaDoc(entries);
46         path = mailboxFile.canonicalPath();
47         lastModified = mailboxFile.lastModified();
48         length = mailboxFile.length();
49     }
50
51     public synchronized ArrayList JavaDoc 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 JavaDoc objectOut = new ObjectOutputStream JavaDoc(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 JavaDoc 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 JavaDoc in = null;
91         try {
92             in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(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 JavaDoc e) {
102             // Expected if an incompatible change has been made to the
103
// serialized class. No big deal.
104
Log.debug(e);
105         }
106         catch (Exception JavaDoc e) {
107             Log.error(e);
108         }
109         finally {
110             if (in != null) {
111                 try {
112                     in.close();
113                 }
114                 catch (IOException JavaDoc e) {
115                     Log.error(e);
116                 }
117             }
118         }
119         return null;
120     }
121 }
122
Popular Tags