Properties.cs 2.0 KB

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