Merge pull request #1500 from criteo-forks/criteo
[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                 [Obsolete ("Serialization is obsoleted for this type", false)]\r
68                 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
69                 {\r
70                         SerializationInfo info = serializationInfo;\r
71 \r
72                         responseUri = (Uri) info.GetValue ("responseUri", typeof (Uri));\r
73                         contentLength = info.GetInt64 ("contentLength");\r
74                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));\r
75                 }\r
76                 \r
77                 // Properties\r
78                 internal bool HasError {\r
79                         get { return exception != null; }\r
80                 }\r
81 \r
82                 internal Exception Error {\r
83                         get { return exception; }\r
84                 }\r
85                 \r
86                 public override long ContentLength {\r
87                         get {\r
88                                 CheckDisposed ();\r
89                                 return this.contentLength;\r
90                         }\r
91                 }\r
92                 \r
93                 public override string ContentType {\r
94                         get {\r
95                                 CheckDisposed ();\r
96                                 return "application/octet-stream";\r
97                         }\r
98                 }\r
99                 \r
100                 public override WebHeaderCollection Headers {\r
101                         get {\r
102                                 CheckDisposed ();\r
103                                 return this.webHeaders;\r
104                         }\r
105                 }\r
106                 \r
107                 public override Uri ResponseUri {\r
108                         get {\r
109                                 CheckDisposed ();\r
110                                 return this.responseUri;\r
111                         }\r
112                 }\r
113 \r
114                 // Methods\r
115 \r
116                 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
117                 {\r
118                         GetObjectData (serializationInfo, streamingContext);\r
119                 }\r
120 \r
121                 protected override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
122                 {\r
123                         SerializationInfo info = serializationInfo;\r
124 \r
125                         info.AddValue ("responseUri", responseUri, typeof (Uri));\r
126                         info.AddValue ("contentLength", contentLength);\r
127                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));\r
128                 }\r
129 \r
130                 public override Stream GetResponseStream()\r
131                 {\r
132                         CheckDisposed ();\r
133                         return this.fileStream;\r
134                 }\r
135                                 \r
136                 // Cleaning up stuff\r
137                 \r
138                 ~FileWebResponse ()\r
139                 {\r
140                         Dispose (false);\r
141                 }               \r
142                 \r
143                 public override void Close()\r
144                 {\r
145                         ((IDisposable) this).Dispose ();\r
146                 }\r
147 \r
148                 void IDisposable.Dispose()\r
149                 {\r
150                         Dispose (true);\r
151                         \r
152                         // see spec, suppress finalization of this object.\r
153                         GC.SuppressFinalize (this);  \r
154                 }\r
155                 \r
156                 protected override\r
157                 void Dispose (bool disposing)\r
158                 {\r
159                         if (this.disposed)\r
160                                 return;\r
161                         this.disposed = true;\r
162                         \r
163                         if (disposing) {\r
164                                 // release managed resources\r
165                                 this.responseUri = null;\r
166                                 this.webHeaders = null;\r
167                         }\r
168                         \r
169                         // release unmanaged resources\r
170                         FileStream stream = fileStream;\r
171                         fileStream = null;\r
172                         if (stream != null)\r
173                                 stream.Close (); // also closes webRequest\r
174                         base.Dispose (disposing);\r
175                 }\r
176                 \r
177                 private void CheckDisposed ()\r
178                 {\r
179                         if (disposed)\r
180                                 throw new ObjectDisposedException (GetType ().FullName);\r
181                 }               \r
182         }\r
183 }\r