2 // System.Net.FileWebResponse
5 // Lawrence Pit (loz@cable.a2000.nl)
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
;
36 public class FileWebResponse
: WebResponse
, ISerializable
, IDisposable
38 private Uri responseUri
;
39 private FileStream fileStream
;
40 private long contentLength
;
41 private WebHeaderCollection webHeaders
;
42 private bool disposed
= false;
46 internal FileWebResponse (Uri responseUri
, FileStream fileStream
)
49 this.responseUri
= responseUri
;
50 this.fileStream
= fileStream
;
51 this.contentLength
= fileStream
.Length
;
52 this.webHeaders
= new WebHeaderCollection ();
53 this.webHeaders
.Add ("Content-Length", Convert
.ToString (contentLength
));
54 this.webHeaders
.Add ("Content-Type", "binary/octet-stream");
55 } catch (Exception e
) {
56 throw new WebException (e
.Message
, e
);
60 protected FileWebResponse (SerializationInfo serializationInfo
, StreamingContext streamingContext
)
62 SerializationInfo info
= serializationInfo
;
64 responseUri
= (Uri
) info
.GetValue ("responseUri", typeof (Uri
));
65 contentLength
= info
.GetInt64 ("contentLength");
66 webHeaders
= (WebHeaderCollection
) info
.GetValue ("webHeaders", typeof (WebHeaderCollection
));
71 public override long ContentLength
{
74 return this.contentLength
;
78 public override string ContentType
{
81 return "binary/octet-stream";
85 public override WebHeaderCollection Headers
{
88 return this.webHeaders
;
92 public override Uri ResponseUri
{
95 return this.responseUri
;
101 void ISerializable
.GetObjectData (SerializationInfo serializationInfo
, StreamingContext streamingContext
)
103 SerializationInfo info
= serializationInfo
;
105 info
.AddValue ("responseUri", responseUri
, typeof (Uri
));
106 info
.AddValue ("contentLength", contentLength
);
107 info
.AddValue ("webHeaders", webHeaders
, typeof (WebHeaderCollection
));
110 public override Stream
GetResponseStream()
113 return this.fileStream
;
123 public override void Close()
125 ((IDisposable
) this).Dispose ();
128 void IDisposable
.Dispose()
132 // see spec, suppress finalization of this object.
133 GC
.SuppressFinalize (this);
136 protected virtual void Dispose (bool disposing
)
140 this.disposed
= true;
143 // release managed resources
144 this.responseUri
= null;
145 this.webHeaders
= null;
148 // release unmanaged resources
149 FileStream stream
= fileStream
;
152 stream
.Close (); // also closes webRequest
155 private void CheckDisposed ()
158 throw new ObjectDisposedException (GetType ().FullName
);