CookieCollection.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // System.Net.CookieCollection
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Runtime.Serialization;
  10. namespace System.Net
  11. {
  12. [Serializable]
  13. public class CookieCollection : ICollection, IEnumerable
  14. {
  15. private ArrayList list = new ArrayList ();
  16. // ctor
  17. public CookieCollection ()
  18. {
  19. }
  20. // ICollection
  21. public int Count {
  22. get { return list.Count; }
  23. }
  24. public bool IsSynchronized {
  25. get { return false; }
  26. }
  27. public Object SyncRoot {
  28. get { return this; }
  29. }
  30. public void CopyTo (Array array, int arrayIndex)
  31. {
  32. list.CopyTo (array, arrayIndex);
  33. }
  34. // IEnumerable
  35. public IEnumerator GetEnumerator ()
  36. {
  37. return list.GetEnumerator ();
  38. }
  39. // This
  40. // LAMESPEC: So how is one supposed to create a writable CookieCollection
  41. // instance?? We simply ignore this property, as this collection is always
  42. // writable.
  43. public bool IsReadOnly {
  44. get { return true; }
  45. }
  46. // LAMESPEC: Which exception should we throw when the read only
  47. // property is set to true??
  48. public void Add (Cookie cookie)
  49. {
  50. if (cookie == null)
  51. throw new ArgumentNullException ("cookie");
  52. int pos = list.IndexOf (cookie);
  53. if (pos == -1)
  54. list.Add (cookie);
  55. else
  56. list [pos] = cookie;
  57. }
  58. // LAMESPEC: Which exception should we throw when the read only
  59. // property is set to true??
  60. public void Add (CookieCollection cookies)
  61. {
  62. if (cookies == null)
  63. throw new ArgumentNullException ("cookies");
  64. IEnumerator enumerator = cookies.list.GetEnumerator ();
  65. while (enumerator.MoveNext ())
  66. Add ((Cookie) enumerator.Current);
  67. }
  68. public Cookie this [int index] {
  69. get {
  70. if (index < 0 || index >= list.Count)
  71. throw new ArgumentOutOfRangeException ("index");
  72. return (Cookie) list [index];
  73. }
  74. }
  75. public Cookie this [string name] {
  76. get {
  77. lock (this) {
  78. IEnumerator enumerator = list.GetEnumerator ();
  79. while (enumerator.MoveNext ())
  80. if (String.Compare (((Cookie) enumerator.Current).Name, name, true) == 0)
  81. return (Cookie) enumerator.Current;
  82. }
  83. return null;
  84. }
  85. }
  86. } // CookieCollection
  87. } // System.Net