2 // System.Net.FileWebResponse
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\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
17 // The above copyright notice and this permission notice shall be
\r
18 // included in all copies or substantial portions of the Software.
\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
31 using System.Runtime.Serialization;
\r
33 namespace System.Net
\r
36 public class FileWebResponse : WebResponse, ISerializable, IDisposable
\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
47 internal FileWebResponse (Uri responseUri, FileStream fileStream)
\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
61 internal FileWebResponse (Uri responseUri, WebException exception)
\r
63 this.responseUri = responseUri;
\r
64 this.exception = exception;
\r
68 [Obsolete ("Serialization is obsoleted for this type", false)]
\r
70 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
72 SerializationInfo info = serializationInfo;
\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
80 internal bool HasError {
\r
81 get { return exception != null; }
\r
84 internal Exception Error {
\r
85 get { return exception; }
\r
88 public override long ContentLength {
\r
91 return this.contentLength;
\r
95 public override string ContentType {
\r
98 return "application/octet-stream";
\r
102 public override WebHeaderCollection Headers {
\r
105 return this.webHeaders;
\r
109 public override Uri ResponseUri {
\r
112 return this.responseUri;
\r
118 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
120 GetObjectData (serializationInfo, streamingContext);
\r
126 void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
128 SerializationInfo info = serializationInfo;
\r
130 info.AddValue ("responseUri", responseUri, typeof (Uri));
\r
131 info.AddValue ("contentLength", contentLength);
\r
132 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
\r
135 public override Stream GetResponseStream()
\r
138 return this.fileStream;
\r
141 // Cleaning up stuff
\r
143 ~FileWebResponse ()
\r
148 public override void Close()
\r
150 ((IDisposable) this).Dispose ();
\r
153 #if TARGET_JVM //enable overrides for extenders
\r
154 public override void Dispose()
\r
156 void IDisposable.Dispose()
\r
161 // see spec, suppress finalization of this object.
\r
162 GC.SuppressFinalize (this);
\r
168 void Dispose (bool disposing)
\r
172 this.disposed = true;
\r
175 // release managed resources
\r
176 this.responseUri = null;
\r
177 this.webHeaders = null;
\r
180 // release unmanaged resources
\r
181 FileStream stream = fileStream;
\r
183 if (stream != null)
\r
184 stream.Close (); // also closes webRequest
\r
187 private void CheckDisposed ()
\r
190 throw new ObjectDisposedException (GetType ().FullName);
\r