Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Base / MS / Internal / IO / Packaging / DeflateEmulationStream.cs / 1 / DeflateEmulationStream.cs
//------------------------------------------------------------------------------ // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // Implementation of a helper class that provides a fully functional Stream on a restricted functionality // Compression stream (System.IO.Compression.DeflateStream). // // History: // 10/05/2005: BruceMac: Split out from CompressEmulationStream //----------------------------------------------------------------------------- using System; using System.IO; using System.IO.Compression; // for DeflateStream using System.Diagnostics; using System.IO.Packaging; using System.Windows; namespace MS.Internal.IO.Packaging { //----------------------------------------------------- // // Internal Members // //----------------------------------------------------- ////// Emulates a fully functional stream using restricted functionality DeflateStream /// internal class DeflateEmulationTransform : IDeflateTransform { ////// Extract from DeflateStream to temp stream /// ///Caller is responsible for correctly positioning source and sink stream pointers before calling. public void Decompress(Stream source, Stream sink) { // for non-empty stream create deflate stream that can // actually decompress using (DeflateStream deflateStream = new DeflateStream( source, // source of compressed data CompressionMode.Decompress, // compress or decompress true)) // leave base stream open when the deflate stream is closed { int bytesRead = 0; do { bytesRead = deflateStream.Read(Buffer, 0, Buffer.Length); if (bytesRead > 0) sink.Write(Buffer, 0, bytesRead); } while (bytesRead > 0); } } ////// Compress from the temp stream into the base stream /// ///Caller is responsible for correctly positioning source and sink stream pointers before calling. public void Compress(Stream source, Stream sink) { // create deflate stream that can actually compress or decompress using (DeflateStream deflateStream = new DeflateStream( sink, // destination for compressed data CompressionMode.Compress, // compress or decompress true)) // leave base stream open when the deflate stream is closed { // persist to deflated stream from working stream int bytesRead = 0; do { bytesRead = source.Read(Buffer, 0, Buffer.Length); if (bytesRead > 0) deflateStream.Write(Buffer, 0, bytesRead); } while (bytesRead > 0); } // truncate if necessary and possible if (sink.CanSeek) sink.SetLength(sink.Position); } //------------------------------------------------------ // // Private Properties // //----------------------------------------------------- private byte[] Buffer { get { if (_buffer == null) _buffer = new byte[0x1000]; // 4k return _buffer; } } //------------------------------------------------------ // // Private Members // //------------------------------------------------------ private byte[] _buffer; // alloc and re-use to reduce memory fragmentation // this is safe because we are not thread-safe } } // 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: // Implementation of a helper class that provides a fully functional Stream on a restricted functionality // Compression stream (System.IO.Compression.DeflateStream). // // History: // 10/05/2005: BruceMac: Split out from CompressEmulationStream //----------------------------------------------------------------------------- using System; using System.IO; using System.IO.Compression; // for DeflateStream using System.Diagnostics; using System.IO.Packaging; using System.Windows; namespace MS.Internal.IO.Packaging { //----------------------------------------------------- // // Internal Members // //----------------------------------------------------- ////// Emulates a fully functional stream using restricted functionality DeflateStream /// internal class DeflateEmulationTransform : IDeflateTransform { ////// Extract from DeflateStream to temp stream /// ///Caller is responsible for correctly positioning source and sink stream pointers before calling. public void Decompress(Stream source, Stream sink) { // for non-empty stream create deflate stream that can // actually decompress using (DeflateStream deflateStream = new DeflateStream( source, // source of compressed data CompressionMode.Decompress, // compress or decompress true)) // leave base stream open when the deflate stream is closed { int bytesRead = 0; do { bytesRead = deflateStream.Read(Buffer, 0, Buffer.Length); if (bytesRead > 0) sink.Write(Buffer, 0, bytesRead); } while (bytesRead > 0); } } ////// Compress from the temp stream into the base stream /// ///Caller is responsible for correctly positioning source and sink stream pointers before calling. public void Compress(Stream source, Stream sink) { // create deflate stream that can actually compress or decompress using (DeflateStream deflateStream = new DeflateStream( sink, // destination for compressed data CompressionMode.Compress, // compress or decompress true)) // leave base stream open when the deflate stream is closed { // persist to deflated stream from working stream int bytesRead = 0; do { bytesRead = source.Read(Buffer, 0, Buffer.Length); if (bytesRead > 0) deflateStream.Write(Buffer, 0, bytesRead); } while (bytesRead > 0); } // truncate if necessary and possible if (sink.CanSeek) sink.SetLength(sink.Position); } //------------------------------------------------------ // // Private Properties // //----------------------------------------------------- private byte[] Buffer { get { if (_buffer == null) _buffer = new byte[0x1000]; // 4k return _buffer; } } //------------------------------------------------------ // // Private Members // //------------------------------------------------------ private byte[] _buffer; // alloc and re-use to reduce memory fragmentation // this is safe because we are not thread-safe } } // 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
- TextViewBase.cs
- EventLogTraceListener.cs
- EdmItemCollection.cs
- VectorConverter.cs
- EnumUnknown.cs
- _Rfc2616CacheValidators.cs
- DecimalAnimation.cs
- EditingCoordinator.cs
- NavigationFailedEventArgs.cs
- EncoderExceptionFallback.cs
- ImageField.cs
- HtmlInputRadioButton.cs
- TargetControlTypeCache.cs
- ExpressionBuilder.cs
- PartialArray.cs
- DataShape.cs
- DesignTimeVisibleAttribute.cs
- QuaternionAnimationUsingKeyFrames.cs
- WebHttpSecurity.cs
- ComponentConverter.cs
- AnimationTimeline.cs
- ItemCheckedEvent.cs
- bindurihelper.cs
- IERequestCache.cs
- AutoGeneratedField.cs
- ControlBuilder.cs
- MappingException.cs
- ImageSourceConverter.cs
- DbInsertCommandTree.cs
- ViewCellRelation.cs
- CircleHotSpot.cs
- GridViewDeleteEventArgs.cs
- PasswordRecovery.cs
- WebReferencesBuildProvider.cs
- QueryParameter.cs
- ActivityTypeResolver.xaml.cs
- brushes.cs
- SpStreamWrapper.cs
- AlphaSortedEnumConverter.cs
- ConnectionManagementSection.cs
- DoubleAnimationUsingPath.cs
- NamespaceExpr.cs
- LoadedOrUnloadedOperation.cs
- ProfileParameter.cs
- XmlSchemaObject.cs
- RowBinding.cs
- GacUtil.cs
- WinFormsComponentEditor.cs
- RegexBoyerMoore.cs
- InputLanguageEventArgs.cs
- ThousandthOfEmRealPoints.cs
- ping.cs
- SignatureConfirmations.cs
- FileInfo.cs
- PointAnimationUsingKeyFrames.cs
- ThreadAbortException.cs
- UserControlAutomationPeer.cs
- Span.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- MessageQueueTransaction.cs
- NamespaceList.cs
- Model3DGroup.cs
- CollectionViewProxy.cs
- DataChangedEventManager.cs
- InfiniteTimeSpanConverter.cs
- ProtocolElement.cs
- MarkupWriter.cs
- ImageBrush.cs
- XmlWellformedWriter.cs
- SystemResources.cs
- Crc32Helper.cs
- ListSourceHelper.cs
- TypeInformation.cs
- CollectionViewGroup.cs
- NoResizeHandleGlyph.cs
- PageAsyncTask.cs
- ValidatingReaderNodeData.cs
- CommonProperties.cs
- XmlChildNodes.cs
- WebConvert.cs
- SingleKeyFrameCollection.cs
- GregorianCalendarHelper.cs
- DbMetaDataCollectionNames.cs
- EmptyElement.cs
- TableItemProviderWrapper.cs
- Validator.cs
- PropertiesTab.cs
- StringCollectionMarkupSerializer.cs
- PageStatePersister.cs
- TreeNodeMouseHoverEvent.cs
- WSFederationHttpSecurityMode.cs
- CustomDictionarySources.cs
- StatusBar.cs
- XmlImplementation.cs
- AssociationType.cs
- Fault.cs
- SmtpException.cs
- SecurityState.cs
- TextAutomationPeer.cs
- AssociatedControlConverter.cs