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
- CodeGotoStatement.cs
- AffineTransform3D.cs
- ConfigXmlComment.cs
- ToolStripDropDown.cs
- BatchParser.cs
- NullableDecimalAverageAggregationOperator.cs
- SafeTimerHandle.cs
- XpsSerializerWriter.cs
- TouchDevice.cs
- HScrollBar.cs
- SystemFonts.cs
- MetaType.cs
- BindingSource.cs
- CompositeActivityTypeDescriptor.cs
- DataControlField.cs
- WriteLine.cs
- BasicCellRelation.cs
- HttpServerVarsCollection.cs
- ImageDrawing.cs
- MultiByteCodec.cs
- ToolStripItemBehavior.cs
- ObjectPersistData.cs
- ExceptionUtility.cs
- MsmqHostedTransportManager.cs
- PropertyBuilder.cs
- ObjectManager.cs
- TextContainer.cs
- MetaModel.cs
- StreamMarshaler.cs
- ReversePositionQuery.cs
- OleDbParameterCollection.cs
- AppSettingsExpressionBuilder.cs
- ComponentRenameEvent.cs
- HttpValueCollection.cs
- ComPersistableTypeElement.cs
- BindingValueChangedEventArgs.cs
- DataGridToolTip.cs
- ProtocolsConfigurationEntry.cs
- OpCopier.cs
- InputMethod.cs
- SspiWrapper.cs
- WebConfigurationManager.cs
- AnonymousIdentificationSection.cs
- GenericEnumerator.cs
- RequestBringIntoViewEventArgs.cs
- PasswordDeriveBytes.cs
- OletxTransactionManager.cs
- RemotingServices.cs
- IProvider.cs
- CompositeCollection.cs
- DataControlFieldCollection.cs
- regiisutil.cs
- FormsAuthenticationUserCollection.cs
- RestHandlerFactory.cs
- ParameterCollection.cs
- Color.cs
- XmlUnspecifiedAttribute.cs
- WebPartConnectionsConnectVerb.cs
- CodeDelegateInvokeExpression.cs
- ActivityCodeDomReferenceService.cs
- MimeBasePart.cs
- EventDescriptor.cs
- PageCatalogPart.cs
- XmlUtf8RawTextWriter.cs
- XNameTypeConverter.cs
- AppManager.cs
- SyndicationSerializer.cs
- _LocalDataStore.cs
- CompletionProxy.cs
- ListViewDeleteEventArgs.cs
- Brush.cs
- AvTrace.cs
- AffineTransform3D.cs
- CodeMemberEvent.cs
- User.cs
- wgx_exports.cs
- Vector.cs
- ToolStripItemTextRenderEventArgs.cs
- NaturalLanguageHyphenator.cs
- SimpleApplicationHost.cs
- ParamArrayAttribute.cs
- DataGridViewTextBoxEditingControl.cs
- DialogWindow.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- StringStorage.cs
- MarshalByValueComponent.cs
- _AutoWebProxyScriptHelper.cs
- SqlPersonalizationProvider.cs
- GetIndexBinder.cs
- PrintController.cs
- XmlConverter.cs
- XmlAttributeProperties.cs
- Focus.cs
- SqlConnectionPoolGroupProviderInfo.cs
- ObjectManager.cs
- TextPointerBase.cs
- InProcStateClientManager.cs
- RepeatButton.cs
- MSAAEventDispatcher.cs
- RuntimeIdentifierPropertyAttribute.cs