Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Shared / MS / Internal / IO / SynchronizingStream.cs / 1305600 / SynchronizingStream.cs
//------------------------------------------------------------------------------
// Microsoft Avalon
// Copyright (c) Microsoft Corporation, 2001
//
// File: SynchronizingStream.cs
//
// Description: Stream that locks on given syncRoot before entering any public API's.
//
// History: 01/20/06 - brucemac - created
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
namespace MS.Internal.IO.Packaging
{
///
/// Wrap returned stream to protect non-thread-safe API's from race conditions
///
internal class SynchronizingStream : Stream
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
///
/// Serializes access by locking on the given object
///
/// stream to read from (baseStream)
/// object to lock on
internal SynchronizingStream(Stream stream, Object syncRoot)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (syncRoot == null)
throw new ArgumentNullException("syncRoot");
_baseStream = stream;
_syncRoot = syncRoot;
}
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
///
/// Return the bytes requested
///
/// destination buffer
/// offset to write into that buffer
/// how many bytes requested
/// how many bytes were written into buffer
public override int Read(byte[] buffer, int offset, int count)
{
lock (_syncRoot)
{
CheckDisposed();
return (_baseStream.Read(buffer, offset, count));
}
}
///
/// Read a single byte
///
/// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
public override int ReadByte()
{
lock (_syncRoot)
{
CheckDisposed();
return (_baseStream.ReadByte());
}
}
///
/// Write a single byte
///
/// byte to write
public override void WriteByte(byte b)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.WriteByte(b);
}
}
///
/// Seek
///
/// only zero is supported
/// only SeekOrigin.Begin is supported
/// zero
public override long Seek(long offset, SeekOrigin origin)
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Seek(offset, origin);
}
}
///
/// SetLength
///
/// not supported
public override void SetLength(long newLength)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.SetLength(newLength);
}
}
///
/// Write
///
/// not supported
public override void Write(byte[] buf, int offset, int count)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Write(buf, offset, count);
}
}
///
/// Flush
///
/// not supported
public override void Flush()
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Flush();
}
}
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
///
/// Is stream readable?
///
public override bool CanRead
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanRead);
}
}
}
///
/// Is stream seekable?
///
/// We MUST support seek as this is used to implement ILockBytes.ReadAt()
public override bool CanSeek
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanSeek);
}
}
}
///
/// Is stream writeable?
///
public override bool CanWrite
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanWrite);
}
}
}
///
/// Logical byte position in this stream
///
public override long Position
{
get
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Position;
}
}
set
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Position = value;
}
}
}
///
/// Length
///
public override long Length
{
get
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Length;
}
}
}
//-----------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
protected override void Dispose(bool disposing)
{
lock (_syncRoot)
{
try
{
if (disposing && (_baseStream != null))
{
// close the underlying Stream
_baseStream.Close();
// NOTE: We cannot set _syncRoot to null because it is used
// on entry to every public method and property.
}
}
finally
{
base.Dispose(disposing);
_baseStream = null;
}
}
}
//-----------------------------------------------------
//
// Private Methods
//
//-----------------------------------------------------
///
/// CheckDisposed
///
/// Pre-condition that lock has been acquired.
private void CheckDisposed()
{
if (_baseStream == null)
throw new ObjectDisposedException("Stream");
}
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
private Stream _baseStream; // stream we are wrapping
private Object _syncRoot; // object to lock on
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
// Microsoft Avalon
// Copyright (c) Microsoft Corporation, 2001
//
// File: SynchronizingStream.cs
//
// Description: Stream that locks on given syncRoot before entering any public API's.
//
// History: 01/20/06 - brucemac - created
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
namespace MS.Internal.IO.Packaging
{
///
/// Wrap returned stream to protect non-thread-safe API's from race conditions
///
internal class SynchronizingStream : Stream
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
///
/// Serializes access by locking on the given object
///
/// stream to read from (baseStream)
/// object to lock on
internal SynchronizingStream(Stream stream, Object syncRoot)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (syncRoot == null)
throw new ArgumentNullException("syncRoot");
_baseStream = stream;
_syncRoot = syncRoot;
}
//------------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
///
/// Return the bytes requested
///
/// destination buffer
/// offset to write into that buffer
/// how many bytes requested
/// how many bytes were written into buffer
public override int Read(byte[] buffer, int offset, int count)
{
lock (_syncRoot)
{
CheckDisposed();
return (_baseStream.Read(buffer, offset, count));
}
}
///
/// Read a single byte
///
/// The unsigned byte cast to an Int32, or -1 if at the end of the stream.
public override int ReadByte()
{
lock (_syncRoot)
{
CheckDisposed();
return (_baseStream.ReadByte());
}
}
///
/// Write a single byte
///
/// byte to write
public override void WriteByte(byte b)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.WriteByte(b);
}
}
///
/// Seek
///
/// only zero is supported
/// only SeekOrigin.Begin is supported
/// zero
public override long Seek(long offset, SeekOrigin origin)
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Seek(offset, origin);
}
}
///
/// SetLength
///
/// not supported
public override void SetLength(long newLength)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.SetLength(newLength);
}
}
///
/// Write
///
/// not supported
public override void Write(byte[] buf, int offset, int count)
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Write(buf, offset, count);
}
}
///
/// Flush
///
/// not supported
public override void Flush()
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Flush();
}
}
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
///
/// Is stream readable?
///
public override bool CanRead
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanRead);
}
}
}
///
/// Is stream seekable?
///
/// We MUST support seek as this is used to implement ILockBytes.ReadAt()
public override bool CanSeek
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanSeek);
}
}
}
///
/// Is stream writeable?
///
public override bool CanWrite
{
get
{
lock (_syncRoot)
{
return ((_baseStream != null) && _baseStream.CanWrite);
}
}
}
///
/// Logical byte position in this stream
///
public override long Position
{
get
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Position;
}
}
set
{
lock (_syncRoot)
{
CheckDisposed();
_baseStream.Position = value;
}
}
}
///
/// Length
///
public override long Length
{
get
{
lock (_syncRoot)
{
CheckDisposed();
return _baseStream.Length;
}
}
}
//-----------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
protected override void Dispose(bool disposing)
{
lock (_syncRoot)
{
try
{
if (disposing && (_baseStream != null))
{
// close the underlying Stream
_baseStream.Close();
// NOTE: We cannot set _syncRoot to null because it is used
// on entry to every public method and property.
}
}
finally
{
base.Dispose(disposing);
_baseStream = null;
}
}
}
//-----------------------------------------------------
//
// Private Methods
//
//-----------------------------------------------------
///
/// CheckDisposed
///
/// Pre-condition that lock has been acquired.
private void CheckDisposed()
{
if (_baseStream == null)
throw new ObjectDisposedException("Stream");
}
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
private Stream _baseStream; // stream we are wrapping
private Object _syncRoot; // object to lock on
}
}
// 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
- ContainerAction.cs
- coordinator.cs
- FilterEventArgs.cs
- XmlSchemaProviderAttribute.cs
- UrlParameterReader.cs
- RootBrowserWindowAutomationPeer.cs
- DragStartedEventArgs.cs
- StatusBarItem.cs
- FormDesigner.cs
- SystemResourceHost.cs
- Int32CollectionConverter.cs
- FormsAuthentication.cs
- ComponentCollection.cs
- CommaDelimitedStringAttributeCollectionConverter.cs
- DynamicDocumentPaginator.cs
- Button.cs
- SmiTypedGetterSetter.cs
- ObjectParameter.cs
- WebScriptMetadataMessage.cs
- EventLogEntry.cs
- ItemAutomationPeer.cs
- DayRenderEvent.cs
- precedingsibling.cs
- ProgressBarRenderer.cs
- TemplateBindingExtensionConverter.cs
- TraceSource.cs
- BackEase.cs
- StringSource.cs
- CodePropertyReferenceExpression.cs
- CustomGrammar.cs
- SeparatorAutomationPeer.cs
- DbSetClause.cs
- DataServiceHostFactory.cs
- DataKey.cs
- ImageIndexConverter.cs
- SettingsBase.cs
- HttpCapabilitiesSectionHandler.cs
- Win32MouseDevice.cs
- UpdatePanelControlTrigger.cs
- DataMemberConverter.cs
- CapabilitiesRule.cs
- ScaleTransform3D.cs
- BuildProvider.cs
- ChannelManager.cs
- WinEventWrap.cs
- RunInstallerAttribute.cs
- MouseCaptureWithinProperty.cs
- ColumnResult.cs
- EntityChangedParams.cs
- CommentEmitter.cs
- SelectionPattern.cs
- Transactions.cs
- GenerateScriptTypeAttribute.cs
- SecurityChannelFactory.cs
- ChangeNode.cs
- SystemBrushes.cs
- SequenceDesigner.cs
- DecoderExceptionFallback.cs
- InvalidDocumentContentsException.cs
- XmlWriterDelegator.cs
- TransmissionStrategy.cs
- SqlGatherConsumedAliases.cs
- GenerateTemporaryTargetAssembly.cs
- TiffBitmapDecoder.cs
- XmlReaderSettings.cs
- ObjectStateEntryDbDataRecord.cs
- XmlNavigatorStack.cs
- ToolStripItem.cs
- embossbitmapeffect.cs
- Clock.cs
- RuntimeHelpers.cs
- SQLDateTime.cs
- AnnotationAdorner.cs
- System.Data.OracleClient_BID.cs
- XmlEncodedRawTextWriter.cs
- FastPropertyAccessor.cs
- ReservationCollection.cs
- RegexStringValidator.cs
- NavigationProgressEventArgs.cs
- EntityDataSourceContextCreatedEventArgs.cs
- SqlDependency.cs
- CommaDelimitedStringAttributeCollectionConverter.cs
- XPathConvert.cs
- Socket.cs
- SystemIcons.cs
- SessionIDManager.cs
- NonBatchDirectoryCompiler.cs
- TextDecorationLocationValidation.cs
- EventSinkHelperWriter.cs
- Item.cs
- RegionData.cs
- MouseGestureConverter.cs
- WebColorConverter.cs
- PathFigureCollection.cs
- GPPOINT.cs
- MatrixUtil.cs
- StreamUpgradeProvider.cs
- ConfigurationSectionCollection.cs
- DiagnosticsConfigurationHandler.cs
- MemoryPressure.cs