/** * read and return a string of line from the input, ignore any CR, LF * * @return a string of line, null for EOF or IOException */ public String readLine() { if (error) { return null; } StringBuffer sb = null;
try { int ch = r.read(); if (ch == -1) { return null; }
sb = new StringBuffer();
// eat up CR and LF while (ch == CR || ch == LF) { ch = r.read(); }
/** * find the given key in the stored key array * * @param key * a string of key to find * @return an index of matching string */ privateint findKey(String key) { int match = -1; for (int i = 0; i < counter; i++) { if (keys[i].equals(key)) { match = i; break; } } return match; }
/** * find the given value in stored value array * * @param value * a string of value to find * @return an index to the matching string */ privateint findValue(String value) { int match = -1; for (int i = 0; i < counter; i++) { if (values[i].equals(value)) { match = i; break; } } return match; }
public String get(String key) { String os = null;
// loop keys int match = -1; match = findKey(key);
// return value if (match >= 0) { os = values[match]; }
return os; }
public String[] getKeys() { if (keys == null || counter == 0) { return null; }
String[] oss = new String[counter]; for (int i = 0; i < counter; i++) { oss[i] = keys[i]; } return oss; }
public String[] getValues() { if (values == null || counter == 0) { return null; }
String[] oss = new String[counter]; for (int i = 0; i < counter; i++) { oss[i] = values[i]; } return oss; }
publicvoid put(String key, String value) { int match = -1; match = findKey(key); if (match < 0) { // no found match, process add new keys[counter] = key; values[counter] = value; counter++; } else { // found match, process update values[match] = value; } }
publicvoid remove(String key) { int match = -1; match = findKey(key);
if (match < 0) { // no found return; }
// move last to matched position keys[match] = keys[counter]; values[match] = values[counter];
// set last to null keys[counter] = null; values[counter] = null;
counter--;
}
}
/* * Properties.java * Aug 3, 2007 */ package com.mot.lrt.util;
import java.io.InputStream;
import com.mot.lrt.io.BufferedReader;
/** * class works as Java SE properties * * @author e7125c * */ publicclass Properties {
private String curKey;
private String curValue;
private Map dish = new HashMap();
private InputStream is;
public Properties() { // empty }
/** * private method, fill current key and value by given string * * @param s * a string to split into curKey and curValue */ privatevoid expLine(String s) { if (s == null || s.trim().length() < 1) { return; } if (s.indexOf("#") == 0) { // comment return; }
/** * get the value by given key * * @param key * a string of given key * @return a string of the returned value, or null */ public String getProperty(String key) { return dish.get(key); }
/** * get the value by given key, return default value if null * * @param key * a string of given key * @param def * a string of default value * @return a string of returned value */ public String getProperty(String key, String def) { String os = dish.get(key); if (os == null) { return def; } return os; }
/** * load properties from given input stream * * @param IS * an object of input stream */ publicvoid load(InputStream IS) { if (IS == null) { System.out.println("Properties.load() Null InputStream"); return; } is = IS; BufferedReader br = null; try { br = new BufferedReader(is); String line = br.readLine(); while (line != null) { // fill curKey and curValue with value expLine(line); // put current key and value to map dish.put(curKey, curValue); // next line line = br.readLine(); } } catch (OutOfMemoryError e) { // in case ... e.printStackTrace(); } finally { if (br != null) { br.close(); br = null; } } }
/** * return all property names in string array * * @return a string array of property names */ public String[] propertyNames() { return dish.getKeys(); }