// Copyright (c) 2006-2007, Mihai Preda. // Available under the MIT License (see COPYING). import java.util.*; import java.io.*; class Config { Hashtable ht = new Hashtable(); Store rs; int recId; Config(Store rs, int recId) { this.rs = rs; this.recId = recId; DataInputStream is = rs.readIS(recId); if (is != null) { String s1, s2; int sz = 0; try { sz = is.readInt(); for (int i = 0; i < sz; ++i) { s1 = is.readUTF(); s2 = is.readUTF(); ht.put(s1, s2); Log.log("--- " + s1 + " " + s2 + " ---"); } } catch (IOException e) { if (sz != ht.size()) { throw new Error("config read " + e); } } } } void set(String key, String value) { ht.put(key, value); } String get(String key, String def) { Object o = ht.get(key); return (o == null) ? def : (String)o; } int size() { return ht.size(); } DataOut out = new DataOut(); void save() { int sz = ht.size(); Enumeration keys = ht.keys(); String s1, s2; try { out.writeInt(sz); for (int i = 0; i < sz; ++i) { s1 = (String) keys.nextElement(); s2 = get(s1, null); out.writeUTF(s1); out.writeUTF(s2); } } catch (IOException e) { throw new Error("config save " + e); } rs.write(recId, out.getBytesAndReset()); Log.log("config saved"); } }