﻿// Decompiled with JetBrains decompiler
// Type: System.Net.FileWebResponse
// Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 5ABD58FD-DF31-44FD-A492-63F2B47CC9AF
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll

using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace System.Net
{
  /// <summary>
  /// Provides a file system implementation of the <see cref="T:System.Net.WebResponse"/> class.
  /// </summary>
  [Serializable]
  public class FileWebResponse : WebResponse, ISerializable, ICloseEx
  {
    private bool m_closed;
    private long m_contentLength;
    private FileAccess m_fileAccess;
    private WebHeaderCollection m_headers;
    private Stream m_stream;
    private Uri m_uri;
    private const int DefaultFileStreamBufferSize = 8192;
    private const string DefaultFileContentType = "application/octet-stream";

    /// <summary>
    /// Gets the length of the content in the file system resource.
    /// </summary>
    /// 
    /// <returns>
    /// The number of bytes returned from the file system resource.
    /// </returns>
    public override long ContentLength
    {
      get
      {
        this.CheckDisposed();
        return this.m_contentLength;
      }
    }

    /// <summary>
    /// Gets the content type of the file system resource.
    /// </summary>
    /// 
    /// <returns>
    /// The value "binary/octet-stream".
    /// </returns>
    public override string ContentType
    {
      get
      {
        this.CheckDisposed();
        return "application/octet-stream";
      }
    }

    /// <summary>
    /// Gets a collection of header name/value pairs associated with the response.
    /// </summary>
    /// 
    /// <returns>
    /// A <see cref="T:System.Net.WebHeaderCollection"/> that contains the header name/value pairs associated with the response.
    /// </returns>
    public override WebHeaderCollection Headers
    {
      get
      {
        this.CheckDisposed();
        return this.m_headers;
      }
    }

    public override bool SupportsHeaders
    {
      get
      {
        return true;
      }
    }

    /// <summary>
    /// Gets the URI of the file system resource that provided the response.
    /// </summary>
    /// 
    /// <returns>
    /// A <see cref="T:System.Uri"/> that contains the URI of the file system resource that provided the response.
    /// </returns>
    public override Uri ResponseUri
    {
      get
      {
        this.CheckDisposed();
        return this.m_uri;
      }
    }

    internal FileWebResponse(FileWebRequest request, Uri uri, FileAccess access, bool asyncHint)
    {
      try
      {
        this.m_fileAccess = access;
        if (access == FileAccess.Write)
        {
          this.m_stream = Stream.Null;
        }
        else
        {
          this.m_stream = (Stream) new FileWebStream(request, uri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192, asyncHint);
          this.m_contentLength = this.m_stream.Length;
        }
        this.m_headers = new WebHeaderCollection(WebHeaderCollectionType.FileWebResponse);
        this.m_headers.AddInternal("Content-Length", this.m_contentLength.ToString((IFormatProvider) NumberFormatInfo.InvariantInfo));
        this.m_headers.AddInternal("Content-Type", "application/octet-stream");
        this.m_uri = uri;
      }
      catch (Exception ex)
      {
        throw (Exception) new WebException(ex.Message, ex, WebExceptionStatus.ConnectFailure, (WebResponse) null);
      }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Net.FileWebResponse"/> class from the specified instances of the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> and <see cref="T:System.Runtime.Serialization.StreamingContext"/> classes.
    /// </summary>
    /// <param name="serializationInfo">A <see cref="T:System.Runtime.Serialization.SerializationInfo"/> instance that contains the information required to serialize the new <see cref="T:System.Net.FileWebResponse"/> instance. </param><param name="streamingContext">An instance of the <see cref="T:System.Runtime.Serialization.StreamingContext"/> class that contains the source of the serialized stream associated with the new <see cref="T:System.Net.FileWebResponse"/> instance. </param>
    [Obsolete("Serialization is obsoleted for this type. http://go.microsoft.com/fwlink/?linkid=14202")]
    protected FileWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
      : base(serializationInfo, streamingContext)
    {
      this.m_headers = (WebHeaderCollection) serializationInfo.GetValue("headers", typeof (WebHeaderCollection));
      this.m_uri = (Uri) serializationInfo.GetValue("uri", typeof (Uri));
      this.m_contentLength = serializationInfo.GetInt64("contentLength");
      this.m_fileAccess = (FileAccess) serializationInfo.GetInt32("fileAccess");
    }

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter, SerializationFormatter = true)]
    void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
    {
      this.GetObjectData(serializationInfo, streamingContext);
    }

    /// <summary>
    /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
    /// </summary>
    /// <param name="serializationInfo">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data. </param><param name="streamingContext">A <see cref="T:System.Runtime.Serialization.StreamingContext"/> that specifies the destination for this serialization.</param>
    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
    protected override void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
    {
      serializationInfo.AddValue("headers", (object) this.m_headers, typeof (WebHeaderCollection));
      serializationInfo.AddValue("uri", (object) this.m_uri, typeof (Uri));
      serializationInfo.AddValue("contentLength", this.m_contentLength);
      serializationInfo.AddValue("fileAccess", (object) this.m_fileAccess);
      base.GetObjectData(serializationInfo, streamingContext);
    }

    /// <summary>
    /// Closes the response stream.
    /// </summary>
    public override void Close()
    {
      ((ICloseEx) this).CloseEx(CloseExState.Normal);
    }

    void ICloseEx.CloseEx(CloseExState closeState)
    {
      if (this.m_closed)
        return;
      this.m_closed = true;
      Stream stream = this.m_stream;
      if (stream == null)
        return;
      if (stream is ICloseEx)
        ((ICloseEx) stream).CloseEx(closeState);
      else
        stream.Close();
      this.m_stream = (Stream) null;
    }

    /// <summary>
    /// Returns the data stream from the file system resource.
    /// </summary>
    /// 
    /// <returns>
    /// A <see cref="T:System.IO.Stream"/> for reading data from the file system resource.
    /// </returns>
    public override Stream GetResponseStream()
    {
      this.CheckDisposed();
      return this.m_stream;
    }

    private void CheckDisposed()
    {
      if (this.m_closed)
        throw new ObjectDisposedException(this.GetType().FullName);
    }
  }
}
