/** * load key/values form the file line is separated by the 'space' * * @throws DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException */ public boolean loadMetaInfo() throws DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException { // FileReader fr; BufferedReader br = null; _valid = false; boolean debugEnabled = DbusEventBuffer.LOG.isDebugEnabled(); try { fr = new FileReader(_file); br = new BufferedReader(fr); DbusEventBuffer.LOG.info("loading metaInfoFile " + _file); _info.clear(); String line = br.readLine(); while (line != null) { if (line.isEmpty() || line.charAt(0) == '#') continue; // format is key' 'val int idx = line.indexOf(KEY_VALUE_SEP); if (idx < 0) { DbusEventBuffer.LOG.warn("illegal line in metaInfoFile. line=" + line); continue; } String key = line.substring(0, idx); String val = line.substring(idx + 1); _info.put(key, val); if (debugEnabled) DbusEventBuffer.LOG.debug("\tkey=" + key + "; val=" + val); line = br.readLine(); _valid = true; } } catch (IOException e) { throw new DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException( this, "cannot read metaInfoFile: " + e.getLocalizedMessage()); } finally { if (br != null) try { br.close(); } catch (IOException e) { DbusEventBuffer.LOG.warn("faild to close " + _file); } } int version = getInt("version"); if (version != META_INFO_VERSION) throw new DbusEventBufferMetaInfo.DbusEventBufferMetaInfoException( this, "metaInfoFile version doesn't match. Please remove the metafile and restart"); if (isMetaFileOlderThenMMappedFiles(getSessionId())) { _valid = false; // not valid file - don't use } return _valid; }
public void saveAndClose() throws IOException { // update version setVal("version", Integer.toString(META_INFO_VERSION)); if (_file.exists()) { File renameTo = new File(_file.getAbsoluteFile() + "." + System.currentTimeMillis()); _file.renameTo(renameTo); DbusEventBuffer.LOG.warn("metaInfoFile " + _file + " exists. it is renambed to " + renameTo); } FileWriter fw = new FileWriter(_file); BufferedWriter bw = new BufferedWriter(fw); for (Map.Entry<String, String> e : _info.entrySet()) { bw.write(e.getKey() + KEY_VALUE_SEP + e.getValue()); bw.newLine(); } bw.close(); }