Properties.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.IO;
  2. namespace SharpCifs.Util.Sharpen
  3. {
  4. public class Properties
  5. {
  6. protected Hashtable _properties;
  7. public Properties()
  8. {
  9. _properties = new Hashtable();
  10. }
  11. public Properties(Properties defaultProp): this()
  12. {
  13. PutAll(defaultProp._properties);
  14. }
  15. public void PutAll(Hashtable properties)
  16. {
  17. foreach (var key in properties.Keys)
  18. {
  19. //_properties.Add(key, properties[key]);
  20. _properties.Put(key, properties[key]);
  21. }
  22. }
  23. public void SetProperty(object key, object value)
  24. {
  25. //_properties.Add(key, value);
  26. _properties.Put(key, value);
  27. }
  28. public object GetProperty(object key)
  29. {
  30. return _properties.Keys.Contains(key) ? _properties[key] : null;
  31. }
  32. public object GetProperty(object key, object def)
  33. {
  34. /*if (_properties.ContainsKey(key))
  35. {
  36. return _properties[key];
  37. }
  38. return def;*/
  39. object value = _properties.Get(key);
  40. return value ?? def;
  41. }
  42. public void Load(InputStream input)
  43. {
  44. StreamReader sr = new StreamReader(input);
  45. while (!sr.EndOfStream)
  46. {
  47. string line = sr.ReadLine();
  48. if (!string.IsNullOrEmpty(line))
  49. {
  50. string[] tokens = line.Split('=');
  51. //_properties.Add(tokens[0], tokens[1]);
  52. _properties.Put(tokens[0], tokens[1]);
  53. }
  54. }
  55. }
  56. public void Store(OutputStream output)
  57. {
  58. StreamWriter sw = new StreamWriter(output);
  59. foreach (var key in _properties.Keys)
  60. {
  61. string line = string.Format("{0}={1}", key, _properties[key]);
  62. sw.WriteLine(line);
  63. }
  64. }
  65. public void Store(TextWriter output)
  66. {
  67. foreach (var key in _properties.Keys)
  68. {
  69. string line = string.Format("{0}={1}", key, _properties[key]);
  70. output.WriteLine(line);
  71. }
  72. }
  73. }
  74. }