Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / 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
- UnicodeEncoding.cs
- ColorMap.cs
- RegistryDataKey.cs
- WindowsFormsHostPropertyMap.cs
- ReturnType.cs
- PrePrepareMethodAttribute.cs
- HttpListenerResponse.cs
- TrackingServices.cs
- NoResizeHandleGlyph.cs
- X509SecurityTokenAuthenticator.cs
- RootBrowserWindow.cs
- StrongNameKeyPair.cs
- DataGridItemCollection.cs
- FixedLineResult.cs
- AuthenticatedStream.cs
- MimeTypeAttribute.cs
- Pair.cs
- WebCategoryAttribute.cs
- XmlSchemaSet.cs
- Point3DCollection.cs
- HttpListenerException.cs
- ToolStripRenderer.cs
- MaterialGroup.cs
- AttributeUsageAttribute.cs
- Animatable.cs
- Vector3dCollection.cs
- DataGridViewCellStyleConverter.cs
- ZoneIdentityPermission.cs
- TextLine.cs
- TemplateNameScope.cs
- ModifierKeysConverter.cs
- Vector3DCollectionValueSerializer.cs
- CqlParserHelpers.cs
- WpfSharedXamlSchemaContext.cs
- ApplyImportsAction.cs
- Attribute.cs
- MessageBox.cs
- DateTimeAutomationPeer.cs
- Soap.cs
- PriorityRange.cs
- StateRuntime.cs
- ProgressBarRenderer.cs
- GCHandleCookieTable.cs
- HttpRequest.cs
- ContainerVisual.cs
- RangeBaseAutomationPeer.cs
- SamlSecurityTokenAuthenticator.cs
- DiscoveryEndpointValidator.cs
- UriSection.cs
- WeakReferenceKey.cs
- IsolationInterop.cs
- codemethodreferenceexpression.cs
- PrincipalPermission.cs
- InstalledVoice.cs
- TriState.cs
- InstanceDataCollection.cs
- BuildProviderUtils.cs
- RandomNumberGenerator.cs
- Int64AnimationUsingKeyFrames.cs
- FileSystemEventArgs.cs
- HashSetDebugView.cs
- XmlEncodedRawTextWriter.cs
- WindowsAuthenticationModule.cs
- RequestCache.cs
- Label.cs
- StatusBarPanel.cs
- FixedPageProcessor.cs
- SqlBuffer.cs
- FormatterConverter.cs
- BitmapEffectInputData.cs
- XmlNode.cs
- TextRangeProviderWrapper.cs
- ObjectDisposedException.cs
- SByteStorage.cs
- NameTable.cs
- NavigationPropertySingletonExpression.cs
- ArrayHelper.cs
- PageParser.cs
- CodeExpressionStatement.cs
- BrowserDefinitionCollection.cs
- ResourceLoader.cs
- RelationshipEnd.cs
- Int64AnimationUsingKeyFrames.cs
- ObjectIDGenerator.cs
- ThemeDictionaryExtension.cs
- Int32RectConverter.cs
- Size3DValueSerializer.cs
- ScriptingWebServicesSectionGroup.cs
- PropertyNames.cs
- DynamicMethod.cs
- NameValuePermission.cs
- ErrorRuntimeConfig.cs
- MatrixConverter.cs
- EntityTypeBase.cs
- KoreanLunisolarCalendar.cs
- ListBase.cs
- BaseServiceProvider.cs
- RoutedCommand.cs
- ThreadExceptionEvent.cs
- AggregatePushdown.cs