/** * Retrieves the hashTable from the file specified by <i>source</i>. * * @param source the name of the file from which the hashTable is to be retrieved. * @pre <i>None</i> * @post If there is any error ⇒ LocalObjectId.nextId = LocalObjectId.INITIAL_ID and * |hashTable| = 0 Else the hashTable in the specified file is retrieved and stored in * hashTable AND LocalObjectId.nextId = |hashTable| */ public void restore(String source) { String fileLocation = DEFAULT_FILE_LOCATION; if (FILE_LOCATION != null) { fileLocation = FILE_LOCATION; } if (source != null) { fileLocation = source; } try { ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(fileLocation))); int nextId = ois.read(); LocalObjectId.setNextId(nextId); hashTable = (Hashtable<LocalObjectId, Object>) ois.readObject(); ois.close(); } catch (Exception e) { LocalObjectId.setNextId(LocalObjectId.INITIAL_ID); hashTable = new Hashtable<LocalObjectId, Object>(); } }
/** * Saves the hashTable in the file specified by the <i>destination</i>. * * * @pre destination ≠ null AND location in file system where file is to be created exists AND * the file location is writable AND every object in the ObjectDB is serializable. * @post the hashTable has been saved to the file named <i>destination</i> along with the size of * the hashTable. */ public void save(String destination) { String fileLocation = DEFAULT_FILE_LOCATION; if (FILE_LOCATION != null) { fileLocation = FILE_LOCATION; } if (destination != null) { fileLocation = destination; } try { ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileLocation))); oos.write(LocalObjectId.getNextId()); oos.writeObject(hashTable); oos.flush(); oos.close(); } catch (Exception e) { System.err.println("In communicator.ObjectDB::save(String) -- ERROR could not save ObjectDB"); } }
/** * Empties the ObjectDB and sets the next LocalObjectID to the initial LocalObjectId. * * @pre <i>None</i> * @post |hashTable| = 0 AND LocalObjectId.nextId = LocalObjectId.INITIAL_ID */ public void clear() { hashTable.clear(); LocalObjectId.setNextId(LocalObjectId.INITIAL_ID); }