Merge branch 'master' of http://github.com/mono/mono
[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 \r
8 //\r
9 // Permission is hereby granted, free of charge, to any person obtaining\r
10 // a copy of this software and associated documentation files (the\r
11 // "Software"), to deal in the Software without restriction, including\r
12 // without limitation the rights to use, copy, modify, merge, publish,\r
13 // distribute, sublicense, and/or sell copies of the Software, and to\r
14 // permit persons to whom the Software is furnished to do so, subject to\r
15 // the following conditions:\r
16 // \r
17 // The above copyright notice and this permission notice shall be\r
18 // included in all copies or substantial portions of the Software.\r
19 // \r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
27 //\r
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;\r
43                 Exception exception;\r
44                 \r
45                 // Constructors\r
46                 \r
47                 internal FileWebResponse (Uri responseUri, FileStream fileStream)\r
48                 {\r
49                         try {\r
50                                 this.responseUri = responseUri;\r
51                                 this.fileStream = fileStream;\r
52                                 this.contentLength = fileStream.Length;\r
53                                 this.webHeaders = new WebHeaderCollection ();\r
54                                 this.webHeaders.Add ("Content-Length", Convert.ToString (contentLength));\r
55                                 this.webHeaders.Add ("Content-Type", "application/octet-stream");\r
56                         } catch (Exception e) {\r
57                                 throw new WebException (e.Message, e);\r
58                         }\r
59                 }\r
60 \r
61                 internal FileWebResponse (Uri responseUri, WebException exception)\r
62                 {\r
63                         this.responseUri = responseUri;\r
64                         this.exception = exception;\r
65                 }\r
66                 \r
67 #if NET_2_0\r
68                 [Obsolete ("Serialization is obsoleted for this type", false)]\r
69 #endif\r
70                 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
71                 {\r
72                         SerializationInfo info = serializationInfo;\r
73 \r
74                         responseUri = (Uri) info.GetValue ("responseUri", typeof (Uri));\r
75                         contentLength = info.GetInt64 ("contentLength");\r
76                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));\r
77                 }\r
78                 \r
79                 // Properties\r
80                 internal bool HasError {\r
81                         get { return exception != null; }\r
82                 }\r
83 \r
84                 internal Exception Error {\r
85                         get { return exception; }\r
86                 }\r
87                 \r
88                 public override long ContentLength {\r
89                         get {\r
90                                 CheckDisposed ();\r
91                                 return this.contentLength;\r
92                         }\r
93                 }\r
94                 \r
95                 public override string ContentType {\r
96                         get {\r
97                                 CheckDisposed ();\r
98                                 return "application/octet-stream";\r
99                         }\r
100                 }\r
101                 \r
102                 public override WebHeaderCollection Headers {\r
103                         get {\r
104                                 CheckDisposed ();\r
105                                 return this.webHeaders;\r
106                         }\r
107                 }\r
108                 \r
109                 public override Uri ResponseUri {\r
110                         get {\r
111                                 CheckDisposed ();\r
112                                 return this.responseUri;\r
113                         }\r
114                 }\r
115 \r
116                 // Methods\r
117 \r
118                 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
119                 {\r
120                         GetObjectData (serializationInfo, streamingContext);\r
121                 }\r
122 \r
123 #if NET_2_0\r
124                 protected override\r
125 #endif\r
126                 void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
127                 {\r
128                         SerializationInfo info = serializationInfo;\r
129 \r
130                         info.AddValue ("responseUri", responseUri, typeof (Uri));\r
131                         info.AddValue ("contentLength", contentLength);\r
132                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));\r
133                 }\r
134 \r
135                 public override Stream GetResponseStream()\r
136                 {\r
137                         CheckDisposed ();\r
138                         return this.fileStream;\r
139                 }\r
140                                 \r
141                 // Cleaning up stuff\r
142                 \r
143                 ~FileWebResponse ()\r
144                 {\r
145                         Dispose (false);\r
146                 }               \r
147                 \r
148                 public override void Close()\r
149                 {\r
150                         ((IDisposable) this).Dispose ();\r
151                 }\r
152 \r
153 #if TARGET_JVM //enable overrides for extenders\r
154                 public override void Dispose()\r
155 #else\r
156                 void IDisposable.Dispose()\r
157 #endif\r
158                 {\r
159                         Dispose (true);\r
160                         \r
161                         // see spec, suppress finalization of this object.\r
162                         GC.SuppressFinalize (this);  \r
163                 }\r
164                 \r
165 #if !NET_2_0\r
166                 protected virtual\r
167 #endif\r
168                 void Dispose (bool disposing)\r
169                 {\r
170                         if (this.disposed)\r
171                                 return;\r
172                         this.disposed = true;\r
173                         \r
174                         if (disposing) {\r
175                                 // release managed resources\r
176                                 this.responseUri = null;\r
177                                 this.webHeaders = null;\r
178                         }\r
179                         \r
180                         // release unmanaged resources\r
181                         FileStream stream = fileStream;\r
182                         fileStream = null;\r
183                         if (stream != null)\r
184                                 stream.Close (); // also closes webRequest\r
185                 }\r
186                 \r
187                 private void CheckDisposed ()\r
188                 {\r
189                         if (disposed)\r
190                                 throw new ObjectDisposedException (GetType ().FullName);\r
191                 }               \r
192         }\r
193 }\r