Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Base / MS / Internal / IO / Packaging / CompoundFile / VersionedStream.cs / 1 / VersionedStream.cs
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// This class provides file versioning support for streams provided by
// IDataTransform implementations.
//
// History:
// 02/21/2006: BruceMac: Initial implementation.
//
//-----------------------------------------------------------------------------
using System;
using System.IO; // for Stream
using System.Windows; // ExceptionStringTable
using System.Globalization; // for CultureInfo
namespace MS.Internal.IO.Packaging.CompoundFile
{
///
/// Maintains a FormatVersion for this stream and any number of sibling streams that semantically
/// share the same version information (which is only persisted in one of the streams).
///
internal class VersionedStream : Stream
{
//-----------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
#region Stream Methods
///
/// Return the bytes requested from the container
///
public override int Read(byte[] buffer, int offset, int count)
{
CheckDisposed();
// ReadAttempt accepts an optional boolean. If this is true, that means
// we are expecting a legal FormatVersion to exist and that it is readable by our
// code version. We do not want to force this check if we are empty.
_versionOwner.ReadAttempt(_stream.Length > 0);
return _stream.Read(buffer, offset, count);
}
///
/// Write
///
public override void Write(byte[] buffer, int offset, int count)
{
CheckDisposed();
_versionOwner.WriteAttempt();
_stream.Write(buffer, offset, count);
}
///
/// ReadByte
///
public override int ReadByte()
{
CheckDisposed();
// ReadAttempt accepts an optional boolean. If this is true, that means
// we are expecting a legal FormatVersion to exist and that it is readable by our
// code version. We do not want to force this check if we are empty.
_versionOwner.ReadAttempt(_stream.Length > 0);
return _stream.ReadByte();
}
///
/// WriteByte
///
public override void WriteByte(byte b)
{
CheckDisposed();
_versionOwner.WriteAttempt();
_stream.WriteByte(b);
}
///
/// Seek
///
/// offset
/// origin
/// zero
public override long Seek(long offset, SeekOrigin origin)
{
CheckDisposed();
return _stream.Seek(offset, origin);
}
///
/// SetLength
///
public override void SetLength(long newLength)
{
CheckDisposed();
if (newLength < 0)
throw new ArgumentOutOfRangeException("newLength");
_versionOwner.WriteAttempt();
_stream.SetLength(newLength);
}
///
/// Flush
///
public override void Flush()
{
CheckDisposed();
_stream.Flush();
}
#endregion Stream Methods
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
#region Stream Properties
///
/// Current logical position within the stream
///
public override long Position
{
get
{
CheckDisposed();
return _stream.Position;
}
set
{
Seek(value, SeekOrigin.Begin);
}
}
///
/// Length
///
public override long Length
{
get
{
CheckDisposed();
return _stream.Length;
}
}
///
/// Is stream readable?
///
/// returns false when called on disposed stream
public override bool CanRead
{
get
{
return (_stream != null) && _stream.CanRead && _versionOwner.IsReadable;
}
}
///
/// Is stream seekable - should be handled by our owner
///
/// returns false when called on disposed stream
public override bool CanSeek
{
get
{
return (_stream != null) && _stream.CanSeek && _versionOwner.IsReadable;
}
}
///
/// Is stream writeable?
///
/// returns false when called on disposed stream
public override bool CanWrite
{
get
{
return (_stream != null) && _stream.CanWrite && _versionOwner.IsUpdatable;
}
}
#endregion
//------------------------------------------------------
//
// Internal Methods
//
//------------------------------------------------------
///
/// Constructor to use for any stream that shares versioning information with another stream
/// but is not the one that houses the FormatVersion data itself.
///
///
///
internal VersionedStream(Stream baseStream, VersionedStreamOwner versionOwner)
{
if (baseStream == null)
throw new ArgumentNullException("baseStream");
if (versionOwner == null)
throw new ArgumentNullException("versionOwner");
_stream = baseStream;
_versionOwner = versionOwner;
}
//-----------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
///
/// Constructor for use by our subclass VersionedStreamOwner.
///
///
protected VersionedStream(Stream baseStream)
{
if (baseStream == null)
throw new ArgumentNullException("baseStream");
_stream = baseStream;
// we are actually a VersionedStreamOwner
_versionOwner = (VersionedStreamOwner)this;
}
///
/// Sometimes our subclass needs to read/write directly to the stream
///
/// Don't use CheckDisposed() here as we need to return null if we are disposed
protected Stream BaseStream
{
get
{
return _stream;
}
}
///
/// Dispose(bool)
///
///
protected override void Dispose(bool disposing)
{
try
{
if (disposing && (_stream != null))
{
_stream.Close();
}
}
finally
{
_stream = null;
base.Dispose(disposing);
}
}
///
/// Call this before accepting any public API call (except some Stream calls that
/// are allowed to respond even when Closed
///
protected void CheckDisposed()
{
if (_stream == null)
throw new ObjectDisposedException(null, SR.Get(SRID.StreamObjectDisposed));
}
//-----------------------------------------------------
//
// Private Fields
//
//-----------------------------------------------------
private VersionedStreamOwner _versionOwner;
private Stream _stream; // null indicates Disposed state
}
}
// 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 class provides file versioning support for streams provided by
// IDataTransform implementations.
//
// History:
// 02/21/2006: BruceMac: Initial implementation.
//
//-----------------------------------------------------------------------------
using System;
using System.IO; // for Stream
using System.Windows; // ExceptionStringTable
using System.Globalization; // for CultureInfo
namespace MS.Internal.IO.Packaging.CompoundFile
{
///
/// Maintains a FormatVersion for this stream and any number of sibling streams that semantically
/// share the same version information (which is only persisted in one of the streams).
///
internal class VersionedStream : Stream
{
//-----------------------------------------------------
//
// Public Methods
//
//-----------------------------------------------------
#region Stream Methods
///
/// Return the bytes requested from the container
///
public override int Read(byte[] buffer, int offset, int count)
{
CheckDisposed();
// ReadAttempt accepts an optional boolean. If this is true, that means
// we are expecting a legal FormatVersion to exist and that it is readable by our
// code version. We do not want to force this check if we are empty.
_versionOwner.ReadAttempt(_stream.Length > 0);
return _stream.Read(buffer, offset, count);
}
///
/// Write
///
public override void Write(byte[] buffer, int offset, int count)
{
CheckDisposed();
_versionOwner.WriteAttempt();
_stream.Write(buffer, offset, count);
}
///
/// ReadByte
///
public override int ReadByte()
{
CheckDisposed();
// ReadAttempt accepts an optional boolean. If this is true, that means
// we are expecting a legal FormatVersion to exist and that it is readable by our
// code version. We do not want to force this check if we are empty.
_versionOwner.ReadAttempt(_stream.Length > 0);
return _stream.ReadByte();
}
///
/// WriteByte
///
public override void WriteByte(byte b)
{
CheckDisposed();
_versionOwner.WriteAttempt();
_stream.WriteByte(b);
}
///
/// Seek
///
/// offset
/// origin
/// zero
public override long Seek(long offset, SeekOrigin origin)
{
CheckDisposed();
return _stream.Seek(offset, origin);
}
///
/// SetLength
///
public override void SetLength(long newLength)
{
CheckDisposed();
if (newLength < 0)
throw new ArgumentOutOfRangeException("newLength");
_versionOwner.WriteAttempt();
_stream.SetLength(newLength);
}
///
/// Flush
///
public override void Flush()
{
CheckDisposed();
_stream.Flush();
}
#endregion Stream Methods
//------------------------------------------------------
//
// Public Properties
//
//-----------------------------------------------------
#region Stream Properties
///
/// Current logical position within the stream
///
public override long Position
{
get
{
CheckDisposed();
return _stream.Position;
}
set
{
Seek(value, SeekOrigin.Begin);
}
}
///
/// Length
///
public override long Length
{
get
{
CheckDisposed();
return _stream.Length;
}
}
///
/// Is stream readable?
///
/// returns false when called on disposed stream
public override bool CanRead
{
get
{
return (_stream != null) && _stream.CanRead && _versionOwner.IsReadable;
}
}
///
/// Is stream seekable - should be handled by our owner
///
/// returns false when called on disposed stream
public override bool CanSeek
{
get
{
return (_stream != null) && _stream.CanSeek && _versionOwner.IsReadable;
}
}
///
/// Is stream writeable?
///
/// returns false when called on disposed stream
public override bool CanWrite
{
get
{
return (_stream != null) && _stream.CanWrite && _versionOwner.IsUpdatable;
}
}
#endregion
//------------------------------------------------------
//
// Internal Methods
//
//------------------------------------------------------
///
/// Constructor to use for any stream that shares versioning information with another stream
/// but is not the one that houses the FormatVersion data itself.
///
///
///
internal VersionedStream(Stream baseStream, VersionedStreamOwner versionOwner)
{
if (baseStream == null)
throw new ArgumentNullException("baseStream");
if (versionOwner == null)
throw new ArgumentNullException("versionOwner");
_stream = baseStream;
_versionOwner = versionOwner;
}
//-----------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
///
/// Constructor for use by our subclass VersionedStreamOwner.
///
///
protected VersionedStream(Stream baseStream)
{
if (baseStream == null)
throw new ArgumentNullException("baseStream");
_stream = baseStream;
// we are actually a VersionedStreamOwner
_versionOwner = (VersionedStreamOwner)this;
}
///
/// Sometimes our subclass needs to read/write directly to the stream
///
/// Don't use CheckDisposed() here as we need to return null if we are disposed
protected Stream BaseStream
{
get
{
return _stream;
}
}
///
/// Dispose(bool)
///
///
protected override void Dispose(bool disposing)
{
try
{
if (disposing && (_stream != null))
{
_stream.Close();
}
}
finally
{
_stream = null;
base.Dispose(disposing);
}
}
///
/// Call this before accepting any public API call (except some Stream calls that
/// are allowed to respond even when Closed
///
protected void CheckDisposed()
{
if (_stream == null)
throw new ObjectDisposedException(null, SR.Get(SRID.StreamObjectDisposed));
}
//-----------------------------------------------------
//
// Private Fields
//
//-----------------------------------------------------
private VersionedStreamOwner _versionOwner;
private Stream _stream; // null indicates Disposed state
}
}
// 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
- DataGridViewCheckBoxColumn.cs
- indexingfiltermarshaler.cs
- LabelEditEvent.cs
- EncoderReplacementFallback.cs
- StreamGeometry.cs
- UnmanagedMemoryAccessor.cs
- ServicePointManager.cs
- SchemaImporterExtensionsSection.cs
- TrackingLocationCollection.cs
- SchemaImporterExtensionsSection.cs
- ReadOnlyPropertyMetadata.cs
- AdCreatedEventArgs.cs
- LeaseManager.cs
- HuffCodec.cs
- ItemDragEvent.cs
- SoapExtension.cs
- EntityDataSourceChangedEventArgs.cs
- CodeDirectoryCompiler.cs
- DataGridViewCellCancelEventArgs.cs
- RequestSecurityTokenSerializer.cs
- InternalEnumValidatorAttribute.cs
- SpellerHighlightLayer.cs
- ImplicitInputBrush.cs
- ProcessProtocolHandler.cs
- SessionIDManager.cs
- XmlDictionary.cs
- WindowsButton.cs
- NotImplementedException.cs
- ViewStateModeByIdAttribute.cs
- SchemaReference.cs
- PartBasedPackageProperties.cs
- UxThemeWrapper.cs
- XamlWriter.cs
- XmlSchemaSubstitutionGroup.cs
- TimeEnumHelper.cs
- ValidationSummary.cs
- LaxModeSecurityHeaderElementInferenceEngine.cs
- Relationship.cs
- ContextMenuStripActionList.cs
- UpdatePanelControlTrigger.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- DeferredSelectedIndexReference.cs
- DataGridViewCellStyleContentChangedEventArgs.cs
- Image.cs
- ProviderSettings.cs
- ProtocolElement.cs
- TransformConverter.cs
- GPPOINTF.cs
- XmlSchemaAppInfo.cs
- hresults.cs
- CommandHelper.cs
- CollectionEditorDialog.cs
- FtpRequestCacheValidator.cs
- LineGeometry.cs
- XmlQueryOutput.cs
- FilteredReadOnlyMetadataCollection.cs
- XmlSerializableReader.cs
- IndexedWhereQueryOperator.cs
- InProcStateClientManager.cs
- XmlSignificantWhitespace.cs
- TdsParserSafeHandles.cs
- IconHelper.cs
- VisualCollection.cs
- JulianCalendar.cs
- ExpressionNode.cs
- Console.cs
- SerializationStore.cs
- Triplet.cs
- basecomparevalidator.cs
- RestHandlerFactory.cs
- ReferenceTypeElement.cs
- RunWorkerCompletedEventArgs.cs
- PrintPreviewControl.cs
- QuaternionValueSerializer.cs
- ViewPort3D.cs
- ControlCollection.cs
- EventSourceCreationData.cs
- OrCondition.cs
- SqlDataSourceStatusEventArgs.cs
- odbcmetadatacollectionnames.cs
- RelationshipFixer.cs
- FuncTypeConverter.cs
- HttpCapabilitiesBase.cs
- CommandField.cs
- FlowDocumentPage.cs
- XhtmlConformanceSection.cs
- EventMap.cs
- XamlSerializerUtil.cs
- DataTableNameHandler.cs
- SqlConnection.cs
- WebBaseEventKeyComparer.cs
- URL.cs
- DynamicObjectAccessor.cs
- ChtmlPhoneCallAdapter.cs
- HTMLTagNameToTypeMapper.cs
- DocumentDesigner.cs
- SurrogateChar.cs
- Expander.cs
- OracleTransaction.cs
- baseshape.cs