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
67 [Obsolete ("Serialization is obsoleted for this type", false)]
\r
68 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
70 SerializationInfo info = serializationInfo;
\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
78 internal bool HasError {
\r
79 get { return exception != null; }
\r
82 internal Exception Error {
\r
83 get { return exception; }
\r
86 public override long ContentLength {
\r
89 return this.contentLength;
\r
93 public override string ContentType {
\r
96 return "application/octet-stream";
\r
100 public override WebHeaderCollection Headers {
\r
103 return this.webHeaders;
\r
107 public override Uri ResponseUri {
\r
110 return this.responseUri;
\r
116 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
118 GetObjectData (serializationInfo, streamingContext);
\r
121 protected override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
123 SerializationInfo info = serializationInfo;
\r
125 info.AddValue ("responseUri", responseUri, typeof (Uri));
\r
126 info.AddValue ("contentLength", contentLength);
\r
127 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
\r
130 public override Stream GetResponseStream()
\r
133 return this.fileStream;
\r
136 // Cleaning up stuff
\r
138 ~FileWebResponse ()
\r
143 public override void Close()
\r
145 ((IDisposable) this).Dispose ();
\r
148 #if TARGET_JVM //enable overrides for extenders
\r
149 public override void Dispose()
\r
151 void IDisposable.Dispose()
\r
156 // see spec, suppress finalization of this object.
\r
157 GC.SuppressFinalize (this);
\r
160 void Dispose (bool disposing)
\r
164 this.disposed = true;
\r
167 // release managed resources
\r
168 this.responseUri = null;
\r
169 this.webHeaders = null;
\r
172 // release unmanaged resources
\r
173 FileStream stream = fileStream;
\r
175 if (stream != null)
\r
176 stream.Close (); // also closes webRequest
\r
179 private void CheckDisposed ()
\r
182 throw new ObjectDisposedException (GetType ().FullName);
\r