FileWebResponse.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // System.Net.FileWebResponse
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.IO;
  29. using System.Runtime.Serialization;
  30. namespace System.Net
  31. {
  32. [Serializable]
  33. public class FileWebResponse : WebResponse, ISerializable, IDisposable
  34. {
  35. private Uri responseUri;
  36. private FileStream fileStream;
  37. private long contentLength;
  38. private WebHeaderCollection webHeaders;
  39. private bool disposed = false;
  40. // Constructors
  41. internal FileWebResponse (Uri responseUri, FileStream fileStream)
  42. {
  43. try {
  44. this.responseUri = responseUri;
  45. this.fileStream = fileStream;
  46. this.contentLength = fileStream.Length;
  47. this.webHeaders = new WebHeaderCollection ();
  48. this.webHeaders.Add ("Content-Length", Convert.ToString (contentLength));
  49. this.webHeaders.Add ("Content-Type", "binary/octet-stream");
  50. } catch (Exception e) {
  51. throw new WebException (e.Message, e);
  52. }
  53. }
  54. protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
  55. {
  56. SerializationInfo info = serializationInfo;
  57. responseUri = (Uri) info.GetValue ("responseUri", typeof (Uri));
  58. contentLength = info.GetInt64 ("contentLength");
  59. webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
  60. }
  61. // Properties
  62. public override long ContentLength {
  63. get {
  64. CheckDisposed ();
  65. return this.contentLength;
  66. }
  67. }
  68. public override string ContentType {
  69. get {
  70. CheckDisposed ();
  71. return "binary/octet-stream";
  72. }
  73. }
  74. public override WebHeaderCollection Headers {
  75. get {
  76. CheckDisposed ();
  77. return this.webHeaders;
  78. }
  79. }
  80. public override Uri ResponseUri {
  81. get {
  82. CheckDisposed ();
  83. return this.responseUri;
  84. }
  85. }
  86. // Methods
  87. void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
  88. {
  89. SerializationInfo info = serializationInfo;
  90. info.AddValue ("responseUri", responseUri, typeof (Uri));
  91. info.AddValue ("contentLength", contentLength);
  92. info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
  93. }
  94. public override Stream GetResponseStream()
  95. {
  96. CheckDisposed ();
  97. return this.fileStream;
  98. }
  99. // Cleaning up stuff
  100. ~FileWebResponse ()
  101. {
  102. Dispose (false);
  103. }
  104. public override void Close()
  105. {
  106. ((IDisposable) this).Dispose ();
  107. }
  108. void IDisposable.Dispose()
  109. {
  110. Dispose (true);
  111. // see spec, suppress finalization of this object.
  112. GC.SuppressFinalize (this);
  113. }
  114. protected virtual void Dispose (bool disposing)
  115. {
  116. if (this.disposed)
  117. return;
  118. this.disposed = true;
  119. if (disposing) {
  120. // release managed resources
  121. this.responseUri = null;
  122. this.webHeaders = null;
  123. }
  124. // release unmanaged resources
  125. FileStream stream = fileStream;
  126. fileStream = null;
  127. if (stream != null)
  128. stream.Close (); // also closes webRequest
  129. }
  130. private void CheckDisposed ()
  131. {
  132. if (disposed)
  133. throw new ObjectDisposedException (GetType ().FullName);
  134. }
  135. }
  136. }