sis.db.KeyFile


 package sis.db; import java.util.*; import java.io.*; class KeyFile {    private Map<String, EntryData> keys =       new HashMap<String, EntryData>();    private File file;    KeyFile(String filename) throws IOException {       file = new File(filename);       if (file.exists())          load();    }    void add(String key, long position, int length) {       keys.put(key, new EntryData(position, length));    }    int size() {       return keys.size();    }    boolean containsKey(String key) {       return keys.containsKey(key);    }    long getPosition(String key) {       return keys.get(key).getPosition();    }    int getLength(String key) {       return keys.get(key).getLength();    }    void close() throws IOException {       ObjectOutputStream stream =          new ObjectOutputStream(new FileOutputStream(file));       stream.writeObject(keys);       stream.close();    }    void load() throws IOException {       ObjectInputStream input = null;       try {          input = new ObjectInputStream(new FileInputStream(file));          try {             keys = (Map<String, EntryData>)input.readObject();          }          catch (ClassNotFoundException e) {          }       }       finally {          input.close();       }    }    static class EntryData implements Serializable {       private long position;       private int length;       EntryData(long position, int length) {          this.position = position;          this.length = length;       }       private long getPosition() {          return position;       }       private int getLength() {          return length;       }    } } 

KeyFile stores the key information using a Map named keys. This Map object maps the key to a serializable static nested class, EntryData, which contains the data position and length. When closed, the KeyFile writes the entire Map to the file by using an ObjectOutputStream. It loads the entire Map when opened.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net