///------------------------------------------------------------------------------
/// 
///     Copyright (c) Microsoft Corporation.  All rights reserved.
///                                
///
/// gpaperin
///------------------------------------------------------------------------------
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using ZErrorCode = System.IO.Compression.ZLibNative.ErrorCode;
namespace System.IO.Compression {    
    
/// 
/// This is the exception that is thrown when a ZLib returns an error code inticating an unrecovarable error.
/// 
#if SILVERLIGHT
internal class ZLibException : IOException {
#else
    [Serializable]
internal class ZLibException : IOException, ISerializable {
#endif
    private string zlibErrorContext = null;
    private string zlibErrorMessage = null;
    private ZErrorCode zlibErrorCode = ZErrorCode.Ok;
    
    /// 
    /// This is the preferred constructor to use.
    /// The other constructors are provided for compliance to Fx design guidelines.
    /// 
    /// A (localised) human readable error description.
    /// A description of the context within zlib where the error occured (e.g. the function name).
    /// The error code returned by a ZLib function that casued this exception.
    /// The string provided by ZLib as error information (unloicalised).
    public ZLibException(string message, string zlibErrorContext, int zlibErrorCode, string zlibErrorMessage) :
        base(message) {
        Init(zlibErrorContext, (ZErrorCode) zlibErrorCode, zlibErrorMessage);
    }
    /// 
    /// This constructor is provided in compliance with common NetFx design patterns;
    /// developers should prefer using the constructor
    /// public ZLibException(string message, string zlibErrorContext, ZLibNative.ErrorCode zlibErrorCode, string zlibErrorMessage).
    ///     
    public ZLibException()
        : base() {
        Init();
    }
    /// 
    /// This constructor is provided in compliance with common NetFx design patterns;
    /// developers should prefer using the constructor
    /// public ZLibException(string message, string zlibErrorContext, ZLibNative.ErrorCode zlibErrorCode, string zlibErrorMessage).
    /// 
    /// The error message that explains the reason for the exception.
    public ZLibException(string message)
        : base(message) {
        Init();
    }
    /// 
    /// This constructor is provided in compliance with common NetFx design patterns;
    /// developers should prefer using the constructor
    /// public ZLibException(string message, string zlibErrorContext, ZLibNative.ErrorCode zlibErrorCode, string zlibErrorMessage).
    /// 
    /// The error message that explains the reason for the exception.
    /// The exception that is the cause of the current exception, or a null.
    public ZLibException(string message, Exception inner)
        : base(message, inner) {
        Init();
    }
    #if !SILVERLIGHT
    /// 
    /// Initializes a new ZLibException with serialized data.
    /// 
    /// The SerializationInfo  that holds the serialized object data about the exception being thrown.
    /// The StreamingContext  that contains contextual information about the source or destination.
    [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter=true)]
    protected ZLibException(SerializationInfo info, StreamingContext context) :
        base(info, context) {
        string errContext = info.GetString("zlibErrorContext");
        ZErrorCode errCode = (ZErrorCode) info.GetInt32("zlibErrorCode");
        string errMessage = info.GetString("zlibErrorMessage");
        Init(errContext, errCode, errMessage);
    }
        
    [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter=true)]
    void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context) {
        base.GetObjectData(si, context);
        si.AddValue("zlibErrorContext", this.zlibErrorContext);
        si.AddValue("zlibErrorCode", (Int32) this.zlibErrorCode);
        si.AddValue("zlibErrorMessage", zlibErrorMessage);
    }
    #endif // !SILVERLIGHT
    private void Init() {
        Init("", ZErrorCode.Ok, "");
    }
    private void Init(string zlibErrorContext, ZErrorCode zlibErrorCode, string zlibErrorMessage) {
        this.zlibErrorContext = zlibErrorContext;
        this.zlibErrorCode = zlibErrorCode;
        this.zlibErrorMessage = zlibErrorMessage;
    }
    
    public string ZLibContext {
        #if SILVERLIGHT
        [SecurityCritical]
        #else
        [PermissionSet(SecurityAction.LinkDemand, Unrestricted=true)]
        #endif
        get { return zlibErrorContext; }
    }
    public int ZLibErrorCode {
        #if SILVERLIGHT
        [SecurityCritical]
        #else
        [PermissionSet(SecurityAction.LinkDemand, Unrestricted=true)]
        #endif
        get { return (int) zlibErrorCode; }
    }
    public string ZLibErrorMessage {
        #if SILVERLIGHT
        [SecurityCritical]
        #else
        [PermissionSet(SecurityAction.LinkDemand, Unrestricted=true)]
        #endif
        get { return zlibErrorMessage; }
    }    
} // internal class ZLibException
} // namespace System.IO.Compression
// file ZLibException.cs