001: /*
002: * MboxSummary.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: MboxSummary.java,v 1.1.1.1 2002/09/24 16:10:13 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.io.BufferedInputStream;
025: import java.io.InvalidClassException;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.io.Serializable;
030: import java.util.ArrayList;
031: import java.util.List;
032: import org.armedbear.j.File;
033: import org.armedbear.j.Log;
034: import org.armedbear.j.Utilities;
035:
036: public final class MboxSummary implements Serializable {
037: private final ArrayList entries;
038: private final String path;
039: private long lastModified;
040: private long length;
041:
042: public MboxSummary(File mailboxFile, List entries) {
043: this .entries = new ArrayList(entries);
044: path = mailboxFile.canonicalPath();
045: lastModified = mailboxFile.lastModified();
046: length = mailboxFile.length();
047: }
048:
049: public synchronized ArrayList getEntries() {
050: return entries;
051: }
052:
053: public synchronized long length() {
054: return length;
055: }
056:
057: public synchronized long lastModified() {
058: return lastModified;
059: }
060:
061: public synchronized void write(File file) {
062: try {
063: Log.debug("MboxSummary.write");
064: long start = System.currentTimeMillis();
065: File temp = Utilities.getTempFile();
066: ObjectOutputStream objectOut = new ObjectOutputStream(temp
067: .getOutputStream());
068: objectOut.writeObject(this );
069: objectOut.flush();
070: objectOut.close();
071: Utilities.deleteRename(temp, file);
072: long elapsed = System.currentTimeMillis() - start;
073: Log.debug("MboxSummary.write completed " + elapsed + " ms");
074: } catch (Exception e) {
075: Log.error(e);
076: }
077: }
078:
079: public static MboxSummary read(File file) {
080: Log.debug("MboxSummary.read");
081: if (file == null || !file.isFile())
082: return null;
083: ObjectInputStream in = null;
084: try {
085: in = new ObjectInputStream(new BufferedInputStream(file
086: .getInputStream()));
087: MboxSummary summary = (MboxSummary) in.readObject();
088: File mailboxFile = File.getInstance(summary.path);
089: if (mailboxFile != null && mailboxFile.isFile()) {
090: if (summary.length == mailboxFile.length())
091: if (summary.lastModified == mailboxFile
092: .lastModified())
093: return summary;
094: }
095: } catch (InvalidClassException e) {
096: // Expected if an incompatible change has been made to the
097: // serialized class. No big deal.
098: Log.debug(e);
099: } catch (Exception e) {
100: Log.error(e);
101: } finally {
102: if (in != null) {
103: try {
104: in.close();
105: } catch (IOException e) {
106: Log.error(e);
107: }
108: }
109: }
110: return null;
111: }
112: }
|