New test.
[mono.git] / mcs / class / System / System.Net / FileWebResponse.cs
1 //\r
2 // System.Net.FileWebResponse\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7
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.
27 //
28 \r
29 using System;\r
30 using System.IO;\r
31 using System.Runtime.Serialization;\r
32 \r
33 namespace System.Net \r
34 {\r
35         [Serializable]\r
36         public class FileWebResponse : WebResponse, ISerializable, IDisposable\r
37         {\r
38                 private Uri responseUri;\r
39                 private FileStream fileStream;\r
40                 private long contentLength;\r
41                 private WebHeaderCollection webHeaders;\r
42                 private bool disposed = false;\r
43                 \r
44                 // Constructors\r
45                 \r
46                 internal FileWebResponse (Uri responseUri, FileStream fileStream)\r
47                 {\r
48                         try {\r
49                                 this.responseUri = responseUri;\r
50                                 this.fileStream = fileStream;\r
51                                 this.contentLength = fileStream.Length;\r
52                                 this.webHeaders = new WebHeaderCollection ();\r
53                                 this.webHeaders.Add ("Content-Length", Convert.ToString (contentLength));\r
54                                 this.webHeaders.Add ("Content-Type", "binary/octet-stream");\r
55                         } catch (Exception e) {\r
56                                 throw new WebException (e.Message, e);\r
57                         }\r
58                 }\r
59                 \r
60                 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
61                 {\r
62                         SerializationInfo info = serializationInfo;\r
63 \r
64                         responseUri = (Uri) info.GetValue ("responseUri", typeof (Uri));\r
65                         contentLength = info.GetInt64 ("contentLength");\r
66                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));\r
67                 }\r
68                 \r
69                 // Properties\r
70                 \r
71                 public override long ContentLength {            \r
72                         get {\r
73                                 CheckDisposed ();\r
74                                 return this.contentLength;\r
75                         }\r
76                 }\r
77                 \r
78                 public override string ContentType {            \r
79                         get {\r
80                                 CheckDisposed ();\r
81                                 return "binary/octet-stream";\r
82                         }\r
83                 }\r
84                 \r
85                 public override WebHeaderCollection Headers {           \r
86                         get {\r
87                                 CheckDisposed ();\r
88                                 return this.webHeaders;\r
89                         }\r
90                 }\r
91                 \r
92                 public override Uri ResponseUri {               \r
93                         get {\r
94                                 CheckDisposed ();\r
95                                 return this.responseUri;\r
96                         }\r
97                 }               \r
98 \r
99                 // Methods\r
100 \r
101                 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
102                 {\r
103                         SerializationInfo info = serializationInfo;\r
104 \r
105                         info.AddValue ("responseUri", responseUri, typeof (Uri));\r
106                         info.AddValue ("contentLength", contentLength);\r
107                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));\r
108                 }               \r
109 \r
110                 public override Stream GetResponseStream()\r
111                 {\r
112                         CheckDisposed ();\r
113                         return this.fileStream;\r
114                 }\r
115                                 \r
116                 // Cleaning up stuff\r
117                 \r
118                 ~FileWebResponse ()\r
119                 {\r
120                         Dispose (false);\r
121                 }               \r
122                 \r
123                 public override void Close()\r
124                 {\r
125                         ((IDisposable) this).Dispose ();\r
126                 }\r
127 \r
128                 void IDisposable.Dispose()\r
129                 {\r
130                         Dispose (true);\r
131                         \r
132                         // see spec, suppress finalization of this object.\r
133                         GC.SuppressFinalize (this);  \r
134                 }\r
135                 \r
136                 protected virtual void Dispose (bool disposing)\r
137                 {\r
138                         if (this.disposed)\r
139                                 return;\r
140                         this.disposed = true;\r
141                         \r
142                         if (disposing) {\r
143                                 // release managed resources\r
144                                 this.responseUri = null;\r
145                                 this.webHeaders = null;\r
146                         }\r
147                         \r
148                         // release unmanaged resources\r
149                         FileStream stream = fileStream;\r
150                         fileStream = null;\r
151                         if (stream != null)\r
152                                 stream.Close (); // also closes webRequest\r
153                 }\r
154                 \r
155                 private void CheckDisposed ()\r
156                 {\r
157                         if (disposed)\r
158                                 throw new ObjectDisposedException (GetType ().FullName);\r
159                 }               \r
160         }\r
161 }\r