// 32feet.NET - Personal Area Networking for .NET // // InTheHand.Net.ObexWebResponse // // Copyright (c) 2003-2008 In The Hand Ltd, All rights reserved. // This source code is licensed under the MIT License using System.Net; using System.IO; using System.Diagnostics.CodeAnalysis; namespace InTheHand.Net { /// /// Provides an OBEX implementation of the class. /// [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable", Justification = "Are we!?!")] public class ObexWebResponse : WebResponse { private MemoryStream responseStream; private WebHeaderCollection responseHeaders; private ObexStatusCode statusCode; internal ObexWebResponse(MemoryStream stream, WebHeaderCollection headers, ObexStatusCode code) { this.responseStream = stream; this.responseHeaders = headers; this.statusCode = code; } #region Headers /// /// Gets the headers associated with this response from the server. /// public override WebHeaderCollection Headers { get { return this.responseHeaders; } } #endregion #region Content Length /// /// Gets the length of the content returned by the request. /// public override long ContentLength { get { string len = this.responseHeaders["LENGTH"]; if (len != null && len != string.Empty) { return long.Parse(len); } return 0; } set { } } #endregion #region Content Type /// /// Gets the content type of the response. /// public override string ContentType { get { return this.responseHeaders["TYPE"]; } set { } } #endregion #region Status Code /// /// Returns a status code to indicate the outcome of the request. /// /// - /// Note, if a error occurs locally then the status code /// is returned. /// Therefore that error code could signal local or remote errors. /// /// public ObexStatusCode StatusCode { get { return statusCode; } } #endregion #region GetResponseStream /// /// Gets the stream used to read the body of the response from the server. /// /// - /// A containing the body of the response. public override Stream GetResponseStream() { return responseStream; } #endregion #region Close /// /// Frees the resources held by the response. /// public override void Close() { if (responseStream != null) { responseStream.Close(); } } #endregion #region WriteFile /// /// Writes the contents of the response to the specified file path. /// /// The filename (including the path) from which to read. [System.Obsolete()] public void WriteFile(string fileName) { FileStream fs = File.Create(fileName); //read in 1k chunks byte[] buffer = new byte[1024]; int readBytes; do { readBytes = responseStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, readBytes); } while (readBytes > 0); responseStream.Close(); fs.Close(); } #endregion } }