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
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SystemIcmpV6Statistics.cs
- EventLogException.cs
- LeaseManager.cs
- BindingExpressionBase.cs
- LabelEditEvent.cs
- UITypeEditors.cs
- SafeCryptContextHandle.cs
- EntityContainerEmitter.cs
- FunctionNode.cs
- Stroke2.cs
- FocusManager.cs
- StrokeCollection2.cs
- SynchronizedDispatch.cs
- TextOnlyOutput.cs
- DocumentViewerConstants.cs
- X509ChainElement.cs
- ValueTable.cs
- InvalidPropValue.cs
- HtmlPhoneCallAdapter.cs
- StringDictionaryCodeDomSerializer.cs
- TextShapeableCharacters.cs
- ArglessEventHandlerProxy.cs
- SafeNativeMethods.cs
- Vector3DAnimationBase.cs
- XmlValidatingReader.cs
- StringExpressionSet.cs
- MailDefinition.cs
- Sequence.cs
- HttpModuleActionCollection.cs
- ImageCodecInfo.cs
- GCHandleCookieTable.cs
- WebResourceUtil.cs
- XmlILOptimizerVisitor.cs
- SafeBitVector32.cs
- ShimAsPublicXamlType.cs
- DBDataPermission.cs
- SystemKeyConverter.cs
- serverconfig.cs
- RelationalExpressions.cs
- TypefaceMetricsCache.cs
- ZoneMembershipCondition.cs
- LicenseException.cs
- XPathNodeHelper.cs
- assertwrapper.cs
- SqlConnectionFactory.cs
- ProfileSettings.cs
- CodeSnippetTypeMember.cs
- TransformerInfoCollection.cs
- ThreadExceptionEvent.cs
- XmlCDATASection.cs
- validation.cs
- EventBindingService.cs
- MemberInfoSerializationHolder.cs
- NullableIntMinMaxAggregationOperator.cs
- DataTableMappingCollection.cs
- LookupNode.cs
- EncoderExceptionFallback.cs
- AnnotationElement.cs
- BitmapData.cs
- PreservationFileWriter.cs
- DeclarativeConditionsCollection.cs
- ReceiveSecurityHeaderEntry.cs
- PresentationSource.cs
- XmlSortKeyAccumulator.cs
- RegionIterator.cs
- Sql8ExpressionRewriter.cs
- URIFormatException.cs
- SiteOfOriginContainer.cs
- LazyTextWriterCreator.cs
- NativeMethods.cs
- ModelVisual3D.cs
- FeatureAttribute.cs
- ListViewDeletedEventArgs.cs
- ColumnHeaderConverter.cs
- Section.cs
- ConfigurationFileMap.cs
- DbConnectionPoolGroupProviderInfo.cs
- versioninfo.cs
- BitmapData.cs
- StickyNoteContentControl.cs
- ConfigurationStrings.cs
- UntrustedRecipientException.cs
- CachedCompositeFamily.cs
- XmlUtil.cs
- KnownTypeHelper.cs
- MenuDesigner.cs
- SemanticResultKey.cs
- DoubleIndependentAnimationStorage.cs
- CommandField.cs
- SiteMapNodeItemEventArgs.cs
- RoutedEventConverter.cs
- AccessKeyManager.cs
- WebScriptEnablingBehavior.cs
- DataPager.cs
- SymbolType.cs
- TypeNameHelper.cs
- ConnectionPoint.cs
- CircleHotSpot.cs
- WebEventTraceProvider.cs
- SystemDiagnosticsSection.cs