FileWebResponse.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Net.FileWebResponse
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. namespace System.Net
  11. {
  12. [Serializable]
  13. public class FileWebResponse : WebResponse, ISerializable, IDisposable
  14. {
  15. private Uri responseUri;
  16. private FileStream fileStream;
  17. private long contentLength;
  18. private WebHeaderCollection webHeaders;
  19. private bool disposed = false;
  20. // Constructors
  21. protected FileWebResponse () { }
  22. internal FileWebResponse (Uri responseUri, FileStream fileStream)
  23. {
  24. try {
  25. this.responseUri = responseUri;
  26. this.fileStream = fileStream;
  27. this.contentLength = fileStream.Length;
  28. this.webHeaders = new WebHeaderCollection ();
  29. this.webHeaders.Add ("Content-Length", Convert.ToString (contentLength));
  30. this.webHeaders.Add ("Content-Type", "binary/octet-stream");
  31. } catch (Exception e) {
  32. throw new WebException (e.Message, e);
  33. }
  34. }
  35. [MonoTODO]
  36. protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  37. {
  38. throw new NotImplementedException ();
  39. }
  40. // Properties
  41. public override long ContentLength {
  42. get {
  43. try { return this.contentLength; }
  44. finally { CheckDisposed (); }
  45. }
  46. }
  47. public override string ContentType {
  48. get {
  49. try { return "binary/octet-stream"; }
  50. finally { CheckDisposed (); }
  51. }
  52. }
  53. public override WebHeaderCollection Headers {
  54. get {
  55. try { return this.webHeaders; }
  56. finally { CheckDisposed (); }
  57. }
  58. }
  59. public override Uri ResponseUri {
  60. get {
  61. try { return this.responseUri; }
  62. finally { CheckDisposed (); }
  63. }
  64. }
  65. // Methods
  66. [MonoTODO]
  67. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  68. StreamingContext streamingContext)
  69. {
  70. try {
  71. throw new NotImplementedException ();
  72. } finally { CheckDisposed (); }
  73. }
  74. public override Stream GetResponseStream()
  75. {
  76. try { return this.fileStream; }
  77. finally { CheckDisposed (); }
  78. }
  79. // Cleaning up stuff
  80. ~FileWebResponse ()
  81. {
  82. Dispose (false);
  83. }
  84. public override void Close()
  85. {
  86. ((IDisposable) this).Dispose ();
  87. }
  88. void IDisposable.Dispose()
  89. {
  90. Dispose (true);
  91. // see spec, suppress finalization of this object.
  92. GC.SuppressFinalize (this);
  93. }
  94. protected virtual void Dispose (bool disposing)
  95. {
  96. if (this.disposed)
  97. return;
  98. this.disposed = true;
  99. if (disposing) {
  100. // release managed resources
  101. this.responseUri = null;
  102. this.webHeaders = null;
  103. }
  104. // release unmanaged resources
  105. FileStream stream = fileStream;
  106. fileStream = null;
  107. if (stream != null)
  108. stream.Close (); // also closes webRequest
  109. }
  110. private void CheckDisposed ()
  111. {
  112. if (disposed)
  113. throw new ObjectDisposedException (GetType ().FullName);
  114. }
  115. }
  116. }