Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Base / MS / Internal / IO / Packaging / TrackingMemoryStream.cs / 1 / TrackingMemoryStream.cs
//------------------------------------------------------------------------------ // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This is a stream that is capable of reporting data usage up to the registered // owner // // History: // 05/24/2005: IgorBel: Initial creation. // 11/08/2005: BruceMac: Change namespace // //----------------------------------------------------------------------------- using System; using System.Diagnostics; using System.IO; namespace MS.Internal.IO.Packaging { // making this class sealed as it is taking advantage of some Virtual methods // in MemoryStream(Capacity); therefore, there is a danger of subclass overriding those and unexpected // behavior changes. Consider calls from Constructor->ReportIfNecessary->Capacity // prior to unsealing this class (they would be marked as FxCop violations) internal sealed class TrackingMemoryStream : MemoryStream { // other constructors can be added later, as we need them, for now we only use the following 2 internal TrackingMemoryStream(ITrackingMemoryStreamFactory memoryStreamFactory): base() { // although we could have implemented this constructor in terms of the other constructor; we shouldn't. // It seems safer to always call the equivalent base class constructor, as we might be ignorant about // some minor differences between various MemoryStream constructors Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } internal TrackingMemoryStream (ITrackingMemoryStreamFactory memoryStreamFactory, Int32 capacity) : base(capacity) { Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } // Here are the overrides for members that could possible result in changes in the allocated memory public override int Read(byte[] buffer, int offset, int count) { int result = base.Read(buffer, offset, count); ReportIfNeccessary(); return result; } public override void Write(byte[] buffer, int offset, int count) { base.Write(buffer, offset, count); ReportIfNeccessary(); } public override void SetLength(long value) { base.SetLength(value); ReportIfNeccessary(); } protected override void Dispose(bool disposing) { try { if (disposing) { if (_memoryStreamFactory != null) { // release all the memory, and report it to the TrackingMemoryStreamFactory SetLength(0); Capacity = 0; ReportIfNeccessary(); _memoryStreamFactory = null; } } } finally { base.Dispose(disposing); } } private void ReportIfNeccessary () { if (this.Capacity !=_lastReportedHighWaterMark) { // we need to report the new memory being allocated as a part of the constructor _memoryStreamFactory.ReportMemoryUsageDelta(checked(this.Capacity - _lastReportedHighWaterMark)); _lastReportedHighWaterMark = this.Capacity; } } private ITrackingMemoryStreamFactory _memoryStreamFactory; private int _lastReportedHighWaterMark; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This is a stream that is capable of reporting data usage up to the registered // owner // // History: // 05/24/2005: IgorBel: Initial creation. // 11/08/2005: BruceMac: Change namespace // //----------------------------------------------------------------------------- using System; using System.Diagnostics; using System.IO; namespace MS.Internal.IO.Packaging { // making this class sealed as it is taking advantage of some Virtual methods // in MemoryStream(Capacity); therefore, there is a danger of subclass overriding those and unexpected // behavior changes. Consider calls from Constructor->ReportIfNecessary->Capacity // prior to unsealing this class (they would be marked as FxCop violations) internal sealed class TrackingMemoryStream : MemoryStream { // other constructors can be added later, as we need them, for now we only use the following 2 internal TrackingMemoryStream(ITrackingMemoryStreamFactory memoryStreamFactory): base() { // although we could have implemented this constructor in terms of the other constructor; we shouldn't. // It seems safer to always call the equivalent base class constructor, as we might be ignorant about // some minor differences between various MemoryStream constructors Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } internal TrackingMemoryStream (ITrackingMemoryStreamFactory memoryStreamFactory, Int32 capacity) : base(capacity) { Debug.Assert(memoryStreamFactory != null); _memoryStreamFactory = memoryStreamFactory; ReportIfNeccessary(); } // Here are the overrides for members that could possible result in changes in the allocated memory public override int Read(byte[] buffer, int offset, int count) { int result = base.Read(buffer, offset, count); ReportIfNeccessary(); return result; } public override void Write(byte[] buffer, int offset, int count) { base.Write(buffer, offset, count); ReportIfNeccessary(); } public override void SetLength(long value) { base.SetLength(value); ReportIfNeccessary(); } protected override void Dispose(bool disposing) { try { if (disposing) { if (_memoryStreamFactory != null) { // release all the memory, and report it to the TrackingMemoryStreamFactory SetLength(0); Capacity = 0; ReportIfNeccessary(); _memoryStreamFactory = null; } } } finally { base.Dispose(disposing); } } private void ReportIfNeccessary () { if (this.Capacity !=_lastReportedHighWaterMark) { // we need to report the new memory being allocated as a part of the constructor _memoryStreamFactory.ReportMemoryUsageDelta(checked(this.Capacity - _lastReportedHighWaterMark)); _lastReportedHighWaterMark = this.Capacity; } } private ITrackingMemoryStreamFactory _memoryStreamFactory; private int _lastReportedHighWaterMark; } } // 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
- CapabilitiesSection.cs
- DescendentsWalker.cs
- MessageQueueEnumerator.cs
- OleDbConnection.cs
- WebPartDescription.cs
- DecoderNLS.cs
- WorkflowMarkupElementEventArgs.cs
- AttachmentCollection.cs
- DataGridViewTextBoxEditingControl.cs
- ReadOnlyActivityGlyph.cs
- SessionEndingCancelEventArgs.cs
- XamlToRtfWriter.cs
- ProtocolsConfigurationHandler.cs
- ScriptControl.cs
- Util.cs
- TableRowGroupCollection.cs
- WorkflowWebHostingModule.cs
- XhtmlStyleClass.cs
- ColorIndependentAnimationStorage.cs
- SafeCryptoHandles.cs
- AuthenticationConfig.cs
- PrimitiveXmlSerializers.cs
- OrderByBuilder.cs
- SortDescriptionCollection.cs
- CompositeScriptReferenceEventArgs.cs
- SessionStateItemCollection.cs
- CustomErrorsSection.cs
- SafeNativeMethods.cs
- RootDesignerSerializerAttribute.cs
- DelegateHelpers.Generated.cs
- SoapInteropTypes.cs
- GZipStream.cs
- MaskedTextBoxTextEditorDropDown.cs
- WorkflowDesignerMessageFilter.cs
- IndicCharClassifier.cs
- ZipIOEndOfCentralDirectoryBlock.cs
- ContentDesigner.cs
- ColumnResizeAdorner.cs
- MarkedHighlightComponent.cs
- ResourceReferenceExpression.cs
- BamlLocalizabilityResolver.cs
- XmlConverter.cs
- SrgsOneOf.cs
- BookmarkUndoUnit.cs
- GenericAuthenticationEventArgs.cs
- _SingleItemRequestCache.cs
- DataGridViewLayoutData.cs
- StringInfo.cs
- XamlWriter.cs
- CharacterBuffer.cs
- AsymmetricSignatureDeformatter.cs
- SHA1.cs
- OdbcPermission.cs
- CounterCreationData.cs
- OracleException.cs
- UpDownBase.cs
- RequestValidator.cs
- RootDesignerSerializerAttribute.cs
- MenuItemBindingCollection.cs
- CompensationToken.cs
- odbcmetadatacollectionnames.cs
- Cursors.cs
- MenuAdapter.cs
- ColorAnimationUsingKeyFrames.cs
- ProfileProvider.cs
- OracleFactory.cs
- XmlILOptimizerVisitor.cs
- Schema.cs
- SimpleHandlerFactory.cs
- TextElement.cs
- _ChunkParse.cs
- FileRecordSequence.cs
- LoginUtil.cs
- RewritingProcessor.cs
- TimelineClockCollection.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- DrawingBrush.cs
- XmlFormatExtensionAttribute.cs
- HtmlInputImage.cs
- MouseActionConverter.cs
- TextTreeText.cs
- WorkflowMarkupElementEventArgs.cs
- DataGridViewCellStateChangedEventArgs.cs
- DllNotFoundException.cs
- WindowsListViewSubItem.cs
- StylusCaptureWithinProperty.cs
- CodeExpressionCollection.cs
- Rect3D.cs
- ServiceReference.cs
- DependencyPropertyKey.cs
- XmlNodeChangedEventManager.cs
- SafeSystemMetrics.cs
- COAUTHIDENTITY.cs
- ApplicationFileParser.cs
- CellParaClient.cs
- CodeFieldReferenceExpression.cs
- ResourceDescriptionAttribute.cs
- httpapplicationstate.cs
- SafeCertificateContext.cs
- XmlAutoDetectWriter.cs