1 /*
2  * @(#) $Id: MessageBoard.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package rmb;
11import java.util.Vector;
12import java.io.*;
13
14public class MessageBoard {
15    private static String RMB_SIGNATURE = "RMB 1.0";
16    private Vector v = new Vector();
17    private static int curidx = 0;
18    private boolean loaded = false;
19    private String path = "rmb.ser";
20    public MessageBoard(){
21    }
22    public void setPath(String path){
23        this.path = path;
24    }
25    public void load(){
26        if (loaded) // Run load() only once.
27            return;
28        loaded = true;
29        try {
30            File f = new File(path);
31            if (!f.exists())
32                return;
33            FileInputStream fis = new FileInputStream(f);
34            ObjectInputStream ois = new ObjectInputStream(fis);
35            String sign = (String)ois.readObject();
36            if (!sign.equals(RMB_SIGNATURE))
37                return;
38            curidx = ois.readInt();
39            v = (Vector)ois.readObject();
40            ois.close();
41        } catch (Exception e){
42            e.printStackTrace();
43        }
44    }
45    public void save(){
46        try {
47            FileOutputStream fos = new FileOutputStream(path);
48            ObjectOutputStream oos = new ObjectOutputStream(fos);
49            oos.writeObject(RMB_SIGNATURE);
50            oos.writeInt(curidx);
51            oos.writeObject(v);
52            oos.close();
53        } catch (Exception e){
54            e.printStackTrace();
55        }
56    }
57    public int size(){
58        return v.size();
59    }
60    public MessageBean get(int index){
61        return (MessageBean)v.elementAt(index);
62    }
63
64    public void add(MessageBean mb){
65        v.add(mb);
66    }
67    public boolean remove(String msgid){
68        for (int i = 0; i < v.size(); i++){
69            MessageBean mb = (MessageBean)v.elementAt(i);
70            if (mb.getMsgid() != null && mb.getMsgid().equals(msgid)){
71                v.remove(mb);
72                return true;
73            }
74        }
75        return false;
76    }
77    public String nextMsgid(){
78        return "msgid_" + curidx++;
79    }
80}