Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / 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
- XPathNodeList.cs
- AttachmentService.cs
- SapiRecognizer.cs
- EventPropertyMap.cs
- SharedPersonalizationStateInfo.cs
- MemberInfoSerializationHolder.cs
- ObjectCacheHost.cs
- SspiWrapper.cs
- MasterPageBuildProvider.cs
- XmlWriter.cs
- FilterEventArgs.cs
- NavigatorInput.cs
- GenericsNotImplementedException.cs
- ScrollChrome.cs
- Label.cs
- ReliableRequestSessionChannel.cs
- WebConfigurationHost.cs
- TextStore.cs
- RuntimeVariablesExpression.cs
- counter.cs
- GestureRecognitionResult.cs
- WindowHideOrCloseTracker.cs
- SettingsPropertyValue.cs
- AlphaSortedEnumConverter.cs
- SynchronizedInputPattern.cs
- OrElse.cs
- KerberosTokenFactoryCredential.cs
- SpeechSeg.cs
- Model3DGroup.cs
- documentsequencetextpointer.cs
- DataGridViewIntLinkedList.cs
- LoginStatusDesigner.cs
- Wrapper.cs
- ZipIOLocalFileDataDescriptor.cs
- DataViewManager.cs
- NotificationContext.cs
- SelectingProviderEventArgs.cs
- XmlWellformedWriterHelpers.cs
- PinnedBufferMemoryStream.cs
- CopyNamespacesAction.cs
- SoapObjectWriter.cs
- DynamicHyperLink.cs
- RoleGroup.cs
- Guid.cs
- HandleCollector.cs
- MailHeaderInfo.cs
- ResourceProviderFactory.cs
- ScriptServiceAttribute.cs
- RIPEMD160Managed.cs
- SocketStream.cs
- SymbolMethod.cs
- DateTimeValueSerializerContext.cs
- DataSourceView.cs
- PathBox.cs
- PowerModeChangedEventArgs.cs
- PenContexts.cs
- localization.cs
- TrustSection.cs
- BrowserCapabilitiesFactory.cs
- Mapping.cs
- WebServiceClientProxyGenerator.cs
- MemberRelationshipService.cs
- EventTrigger.cs
- ExtendedPropertyDescriptor.cs
- SchemaNotation.cs
- ConfigurationPermission.cs
- RawStylusInputReport.cs
- Effect.cs
- ScrollBar.cs
- TableRow.cs
- CornerRadius.cs
- XmlChoiceIdentifierAttribute.cs
- Themes.cs
- LightweightCodeGenerator.cs
- TableLayoutPanelCellPosition.cs
- EntityDataSourceView.cs
- ResumeStoryboard.cs
- SmiEventStream.cs
- ObjectViewListener.cs
- CheckBoxAutomationPeer.cs
- PenLineJoinValidation.cs
- ScrollPatternIdentifiers.cs
- PassportAuthenticationModule.cs
- PropertyNames.cs
- UnsafeNativeMethodsTablet.cs
- OracleConnectionString.cs
- DataList.cs
- XmlAttributeCache.cs
- XmlHierarchicalEnumerable.cs
- BitConverter.cs
- XPathParser.cs
- LowerCaseStringConverter.cs
- COAUTHIDENTITY.cs
- AlphaSortedEnumConverter.cs
- NumericUpDownAcceleration.cs
- GridViewRow.cs
- HostProtectionException.cs
- DependencyObjectCodeDomSerializer.cs
- ContextMenuStrip.cs
- SQlBooleanStorage.cs