Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / StreamHelper.cs / 1 / StreamHelper.cs
//------------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// A static utilitly class for stream related functions.
//
//
// History:
// 08/28/2005: [....]: Initial implementation.
//-----------------------------------------------------------------------------
using System;
using System.IO;
namespace MS.Internal.Documents.Application
{
///
/// A static utilitly class for stream related functions.
///
internal static class StreamHelper
{
#region Internal Methods
//-------------------------------------------------------------------------
// Internal Methods
//-------------------------------------------------------------------------
///
/// A simple stream copy from one to another.
///
///
/// If anyone knows of an existing mechanism please contact me and
/// I will remove this code.
///
/// Performance: This block of code is likely to be the most heavily used
/// code with large packages. We should explorer other designs if it
/// becomes an issue.
///
/// The Stream to read from.
/// The Stream to write to.
internal static void CopyStream(Stream source, Stream target)
{
int bufferSize = 4096; // Arbitrary Value
byte[] buffer = new byte[bufferSize];
long originalSourcePosition = source.Position;
long originalTargetPosition = target.Position;
// move to the start
source.Position = 0;
target.Position = 0;
// ensure we have enough space
long size = source.Length;
target.SetLength(size);
// copy the stream syncronously
int read = 0;
long leftToCopy = size;
while (leftToCopy > 0)
{
if (leftToCopy < bufferSize)
{
bufferSize = (int)leftToCopy;
buffer = new byte[bufferSize];
}
read = source.Read(buffer, 0, bufferSize);
target.Write(buffer, 0, read);
leftToCopy -= read;
}
// return the streams to thier orignal locations
source.Position = originalSourcePosition;
target.Position = originalTargetPosition;
Trace.SafeWrite(Trace.File, "Copied: {0} bytes.", target.Length);
}
#if DRT
///
/// Compares two streams byte by byte.
///
/// The original stream
/// The stream to compare with.
internal static void CompareStream(Stream original, Stream comparee)
{
original.Position = 0;
comparee.Position = 0;
int data = 0;
int pos = 0;
while (data != -1)
{
data = original.ReadByte();
pos++;
Invariant.Assert(data == comparee.ReadByte(),
"Data mismatch at postion " + pos);
}
if (pos - 1 == comparee.Length)
{
Trace.SafeWrite(Trace.File, "Validate: {0} bytes.", comparee.Length);
}
}
#endif
#endregion Internal Methods
}
}
// 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
- FillErrorEventArgs.cs
- MappingItemCollection.cs
- EncryptedKey.cs
- SystemUnicastIPAddressInformation.cs
- Point3DAnimation.cs
- WebPartDisplayModeCancelEventArgs.cs
- CroppedBitmap.cs
- DeferrableContentConverter.cs
- SoapAttributeOverrides.cs
- StrokeNode.cs
- StandardTransformFactory.cs
- SelectionItemProviderWrapper.cs
- ColorDialog.cs
- CollectionViewGroup.cs
- FullTrustAssemblyCollection.cs
- PeerCollaborationPermission.cs
- DateTimeFormatInfo.cs
- TreeNodeStyleCollection.cs
- TraceHelpers.cs
- WebPartActionVerb.cs
- ToolStripItemCollection.cs
- GradientStop.cs
- AbsoluteQuery.cs
- LogSwitch.cs
- DelegatingConfigHost.cs
- LinqExpressionNormalizer.cs
- TableStyle.cs
- MobileControlsSectionHelper.cs
- DetailsViewInsertedEventArgs.cs
- SimpleApplicationHost.cs
- PageThemeBuildProvider.cs
- GeneralTransform3DTo2D.cs
- CommandManager.cs
- ValueQuery.cs
- BindingExpressionBase.cs
- DtdParser.cs
- AttachedAnnotationChangedEventArgs.cs
- BitmapCache.cs
- ResXResourceWriter.cs
- StructureChangedEventArgs.cs
- Pen.cs
- WebPartExportVerb.cs
- LateBoundBitmapDecoder.cs
- Aggregates.cs
- ResXResourceSet.cs
- PostBackTrigger.cs
- OutputCacheModule.cs
- Query.cs
- HttpWebRequestElement.cs
- DateTimeConverter2.cs
- SQLInt16.cs
- EntityClassGenerator.cs
- TextEndOfLine.cs
- ConfigurationException.cs
- ProxyFragment.cs
- Rotation3D.cs
- EditorPartDesigner.cs
- SmiContext.cs
- WizardPanelChangingEventArgs.cs
- Base64Stream.cs
- WebMessageFormatHelper.cs
- ContractComponent.cs
- ControlUtil.cs
- ResourceExpressionBuilder.cs
- ContractMapping.cs
- NativeDirectoryServicesQueryAPIs.cs
- SubMenuStyleCollection.cs
- HttpConfigurationSystem.cs
- UnhandledExceptionEventArgs.cs
- OneWayChannelFactory.cs
- DetailsViewDeletedEventArgs.cs
- FileAuthorizationModule.cs
- ContainerFilterService.cs
- GraphicsContainer.cs
- ObjectSecurity.cs
- CompilationUtil.cs
- altserialization.cs
- CompatibleIComparer.cs
- DataGridViewCellCancelEventArgs.cs
- CriticalFinalizerObject.cs
- Utils.cs
- LayeredChannelFactory.cs
- X509Certificate.cs
- IOThreadTimer.cs
- HtmlTableCellCollection.cs
- FunctionImportMapping.cs
- TextHidden.cs
- EntityTransaction.cs
- LicenseException.cs
- TextSchema.cs
- SmtpNetworkElement.cs
- InfiniteTimeSpanConverter.cs
- KeyGestureValueSerializer.cs
- WebPartExportVerb.cs
- handlecollector.cs
- ListComponentEditor.cs
- UnsafePeerToPeerMethods.cs
- TableRowGroup.cs
- AsyncOperationManager.cs
- CalendarDay.cs