日期:2014-05-20 浏览次数:20728 次
package vmlinux.app; import java.io.*; import java.util.*; public class PropertiesManager { String lang_; String name_; Hashtable strings_; boolean loaded_; private static StringManager instance; public static String get(String name) { return instance.getString(name); } public static PropertiesManager getInstance(String lang) { if(instance!=null) { instance.setLang(lang); } else { instance=new StringManager(lang); } return instance; } public PropertiesManager(String lang) { lang_=lang; loaded_=false; } public void setLang(String lang) { if(lang_==lang) return; lang_=lang; loaded_=false; } public void load(String name) { if(name==name_ && loaded_) return; if(strings_==null) strings_=new Hashtable(); else strings_.clear(); InputStream is=this.getClass().getResourceAsStream("/properties/"+name+"."+lang_); try { byte[] buf=new byte[1024]; int c=is.read(buf); StringBuffer o=new StringBuffer(); String key=null; do{ String data=new String(buf,0,c); int s=0; for(int i=0;i<data.length();++i) { char x=data.charAt(i); if(s==0 && (x==' ' || x=='\t')) { key=o.toString(); o.delete(0,o.length()); s=1; } else if(s==1 && x=='\n') { if(key!=null && key.length()>0) strings_.put(key,o.toString()); key=null; o.delete(0,o.length()); s=0; } else o.append(x); } }while((c=is.read(buf))>0); if(key!=null && key.length()>0) strings_.put(key,o.toString()); is.close(); } catch(Exception ex) { } name_=name; loaded_=true; } public String getString(String name) { Object o=strings_.get(name); if(o==null) return name; else return (String)o; } }