**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Net / FileWebResponse.cs
blobbe115208908c2a55fdfb4d245eabef7d8e5752f4
1 //
2 // System.Net.FileWebResponse
3 //
4 // Author:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.IO;
31 using System.Runtime.Serialization;
33 namespace System.Net
35 [Serializable]
36 public class FileWebResponse : WebResponse, ISerializable, IDisposable
38 private Uri responseUri;
39 private FileStream fileStream;
40 private long contentLength;
41 private WebHeaderCollection webHeaders;
42 private bool disposed = false;
44 // Constructors
46 internal FileWebResponse (Uri responseUri, FileStream fileStream)
48 try {
49 this.responseUri = responseUri;
50 this.fileStream = fileStream;
51 this.contentLength = fileStream.Length;
52 this.webHeaders = new WebHeaderCollection ();
53 this.webHeaders.Add ("Content-Length", Convert.ToString (contentLength));
54 this.webHeaders.Add ("Content-Type", "binary/octet-stream");
55 } catch (Exception e) {
56 throw new WebException (e.Message, e);
60 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
62 SerializationInfo info = serializationInfo;
64 responseUri = (Uri) info.GetValue ("responseUri", typeof (Uri));
65 contentLength = info.GetInt64 ("contentLength");
66 webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
69 // Properties
71 public override long ContentLength {
72 get {
73 CheckDisposed ();
74 return this.contentLength;
78 public override string ContentType {
79 get {
80 CheckDisposed ();
81 return "binary/octet-stream";
85 public override WebHeaderCollection Headers {
86 get {
87 CheckDisposed ();
88 return this.webHeaders;
92 public override Uri ResponseUri {
93 get {
94 CheckDisposed ();
95 return this.responseUri;
99 // Methods
101 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
103 SerializationInfo info = serializationInfo;
105 info.AddValue ("responseUri", responseUri, typeof (Uri));
106 info.AddValue ("contentLength", contentLength);
107 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
110 public override Stream GetResponseStream()
112 CheckDisposed ();
113 return this.fileStream;
116 // Cleaning up stuff
118 ~FileWebResponse ()
120 Dispose (false);
123 public override void Close()
125 ((IDisposable) this).Dispose ();
128 void IDisposable.Dispose()
130 Dispose (true);
132 // see spec, suppress finalization of this object.
133 GC.SuppressFinalize (this);
136 protected virtual void Dispose (bool disposing)
138 if (this.disposed)
139 return;
140 this.disposed = true;
142 if (disposing) {
143 // release managed resources
144 this.responseUri = null;
145 this.webHeaders = null;
148 // release unmanaged resources
149 FileStream stream = fileStream;
150 fileStream = null;
151 if (stream != null)
152 stream.Close (); // also closes webRequest
155 private void CheckDisposed ()
157 if (disposed)
158 throw new ObjectDisposedException (GetType ().FullName);