WriteableOnDemandPackagePart.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / WriteableOnDemandPackagePart.cs / 1 / WriteableOnDemandPackagePart.cs

                            //------------------------------------------------------------------------------ 
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved. 
//
//  
//  This class acts as a type of proxy; it is responsible for forwarding all
//  PackagePart requests to the active PackagePart.  Unlike a proxy it controls 
//  which part is active.  Initially the active part is the stream provided at 
//  construction, at the time of the first write operation the active part is
//  replaced with one provided by a delegate.  
//
// History:
//  07/04/2005: [....]: Initial implementation.
//  08/28/2005: [....]: Adjusted spacing and style to match team better. 
//-----------------------------------------------------------------------------
 
using System; 
using System.Collections.Generic;
using System.IO; 
using System.IO.Packaging;
using System.Text;
using System.Windows.TrustUI;
 
using MS.Internal.IO;
 
namespace MS.Internal.Documents.Application 
{
///  
/// This class acts as a type of proxy; it is responsible for forwarding all
/// PackagePart requests to the active PackagePart.  Unlike a proxy it is
/// controls which part is active.  Initially the active part is the stream
/// provided on construction, at the time of the first write operation the 
/// active part is replaced with one provided by a delegate.The class will
/// get a writing PackagePart only when needed. 
///  
/// 
/// When GetStreamCore is called instead of returning the stream directly we 
/// return a WriteableOnDemandStream a proxy.  We provide this stream proxy
/// a read only stream from the active part and a factory method for
/// creating a writeable stream.
/// 
/// When the WriteableStreamFactory method is called, if our active part is
/// writable we simply forward a GetStream request to our active part. 
/// 
/// Our active part becomes writeable on the first call to our
/// WriteableStreamFactory method, we use the delegate provide at our 
/// construction to get a writeable part and set it as the active part.
///
/// Currently this is the only way our active part becomes writeable.
///  
internal sealed class WriteableOnDemandPackagePart : PackagePart
{ 
    #region Constructors 
    //-------------------------------------------------------------------------
    // Constructors 
    //-------------------------------------------------------------------------

    /// 
    /// Constructs the class with readingPart as the active PackagePart. 
    /// 
    ///  
    /// The Package the PackagePart belongs to. 
    /// 
    /// The read only PackagePart. 
    /// The delegate used to create a
    /// writing part when needed.
    internal WriteableOnDemandPackagePart(
        Package container, 
        PackagePart readingPart,
        WriteablePackagePartFactoryDelegate writeablePartFactory) 
        : base(container, readingPart.Uri) 
    {
        if (container == null) 
        {
            throw new ArgumentNullException("container");
        }
 
        if (readingPart == null)
        { 
            throw new ArgumentNullException("readingPart"); 
        }
 
        if (writeablePartFactory == null)
        {
            throw new ArgumentNullException("writeablePartFactory");
        } 

        _activePart = readingPart; 
        _getWriteablePartInstance = writeablePartFactory; 
    }
    #endregion Constructors 

    #region Internal Properties
    //--------------------------------------------------------------------------
    // Internal Properties 
    //-------------------------------------------------------------------------
 
    ///  
    /// Sets the target PackagePart for this proxy.
    ///  
    internal PackagePart Target
    {
        set
        { 
            _activePart = value;
            _isActiveWriteable = false; 
        } 
    }
 
    #endregion Internal Properties

    #region Internal Delegates
    //-------------------------------------------------------------------------- 
    // Internal Delegates
    //-------------------------------------------------------------------------- 
 
    /// 
    /// This a delegate to get a writeable PackagePart. 
    /// 
    /// The active part that is requesting the
    /// writeable part.
    /// A writable PackagePart. 
    internal delegate PackagePart WriteablePackagePartFactoryDelegate(
        PackagePart requestor); 
    #endregion Internal Delegates 

    #region Protected Methods - PackagePart Overrides 
    //-------------------------------------------------------------------------
    // Protected Methods - PackagePart Overrides
    //--------------------------------------------------------------------------
 
    #region Pure Proxies for PackagePart
    //------------------------------------------------------------------------- 
    // This region of code simply follows the proxy patern and only forwards 
    // the calls to the active part.
    //------------------------------------------------------------------------- 

    /// 
    /// Gets the content type of this PackagePart.
    ///  
    /// The content type of this PackagePart.
    protected override string GetContentTypeCore() 
    { 
        return _activePart.ContentType;
    } 
    #endregion Pure Proxies for PackagePart

    #region Decorating Proxies for PackagePart
    //------------------------------------------------------------------------- 
    // This region of code performs other actions before forwarding
    // to the active part. 
    //-------------------------------------------------------------------------- 

    ///  
    /// Returns a proxy to a stream.
    /// 
    /// Desired FileMode.
    /// Desired FileAccess. 
    /// A stream.
    protected override System.IO.Stream GetStreamCore( 
        System.IO.FileMode mode, 
        System.IO.FileAccess access)
    { 
        Trace.SafeWrite(
            Trace.Packaging,
            "Creating a proxy stream for {0}#{1} for {2} access",
            _activePart.Uri, 
            _activePart.GetHashCode(),
            access); 
 
        // we do not need to wory about cleaning up this proxy
        // when we are closed our base class handles it 
        return new WriteableOnDemandStream(
            _activePart.GetStream(FileMode.Open, FileAccess.Read),
            mode,
            access, 
            WriteableStreamFactory);
    } 
    #endregion Decorating Proxies for PackagePart 

    #endregion Protected Methods - PackagePart Overrides 

    #region Private Methods
    //-------------------------------------------------------------------------
    // Private Methods 
    //--------------------------------------------------------------------------
 
    ///  
    /// This will return a writable stream with the desired access.
    ///  
    /// 
    /// 
    /// Regardless of mode & access a writable stream is returned.
    ///  
    /// Desired FileMode.
    /// Desired FileAccess. 
    /// A writeable stream. 
    private Stream WriteableStreamFactory(
        FileMode mode, 
        FileAccess access)
    {
        if (!_isActiveWriteable)
        { 
            Trace.SafeWrite(
                Trace.Packaging, 
                "Creating a writeable stream for {0} with {1} access", 
                _activePart.Uri,
                access); 

            PackagePart writingPart = _getWriteablePartInstance(this);
            if (writingPart == null)
            { 
                throw new IOException(
                    SR.Get(SRID.PackagingWriteableDelegateGaveNullPart)); 
            } 

            if (writingPart.Equals(this)) 
            {
                throw new IOException(
                    SR.Get(SRID.PackagingCircularReference));
            } 

            _activePart = writingPart; 
            _isActiveWriteable = true; 
        }
 
        return _activePart.GetStream(mode, access);
    }
    #endregion Private Methods
 
    #region Private Fields
    //-------------------------------------------------------------------------- 
    // Private Fields 
    //-------------------------------------------------------------------------
 
    /// 
    /// The current comparee of this proxy.
    /// 
    private PackagePart _activePart; 
    /// 
    /// Is the active part a writing part? 
    ///  
    private bool _isActiveWriteable;
    ///  
    /// Will return a part we can use for writing.
    /// 
    private WriteablePackagePartFactoryDelegate _getWriteablePartInstance;
 
    #endregion Private Fields
} 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK