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
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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 = false;
\r
46 internal FileWebResponse (Uri responseUri, FileStream fileStream)
\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
60 protected FileWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
62 SerializationInfo info = serializationInfo;
\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
71 public override long ContentLength {
\r
74 return this.contentLength;
\r
78 public override string ContentType {
\r
81 return "binary/octet-stream";
\r
85 public override WebHeaderCollection Headers {
\r
88 return this.webHeaders;
\r
92 public override Uri ResponseUri {
\r
95 return this.responseUri;
\r
101 void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
\r
103 SerializationInfo info = serializationInfo;
\r
105 info.AddValue ("responseUri", responseUri, typeof (Uri));
\r
106 info.AddValue ("contentLength", contentLength);
\r
107 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
\r
110 public override Stream GetResponseStream()
\r
113 return this.fileStream;
\r
116 // Cleaning up stuff
\r
118 ~FileWebResponse ()
\r
123 public override void Close()
\r
125 ((IDisposable) this).Dispose ();
\r
128 void IDisposable.Dispose()
\r
132 // see spec, suppress finalization of this object.
\r
133 GC.SuppressFinalize (this);
\r
136 protected virtual void Dispose (bool disposing)
\r
140 this.disposed = true;
\r
143 // release managed resources
\r
144 this.responseUri = null;
\r
145 this.webHeaders = null;
\r
148 // release unmanaged resources
\r
149 FileStream stream = fileStream;
\r
151 if (stream != null)
\r
152 stream.Close (); // also closes webRequest
\r
155 private void CheckDisposed ()
\r
158 throw new ObjectDisposedException (GetType ().FullName);
\r