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
- LicFileLicenseProvider.cs
- _StreamFramer.cs
- SiteMapPath.cs
- HttpConfigurationContext.cs
- MatrixConverter.cs
- SmiEventSink_Default.cs
- DataGridViewCell.cs
- BitmapEffectDrawing.cs
- Int16Storage.cs
- IncrementalCompileAnalyzer.cs
- BlockCollection.cs
- ExpressionBuilder.cs
- X509SecurityToken.cs
- CompositeCollection.cs
- Configuration.cs
- BulletedListEventArgs.cs
- ComponentGlyph.cs
- _AutoWebProxyScriptHelper.cs
- AVElementHelper.cs
- RoleManagerEventArgs.cs
- Transform3DGroup.cs
- CodeTypeReferenceCollection.cs
- FixedBufferAttribute.cs
- Pens.cs
- RowsCopiedEventArgs.cs
- CachedFontFace.cs
- TextServicesManager.cs
- XmlSyndicationContent.cs
- CatalogZone.cs
- DockProviderWrapper.cs
- CommandPlan.cs
- safelink.cs
- AutomationPeer.cs
- BinaryReader.cs
- TemplatedWizardStep.cs
- DiagnosticsConfiguration.cs
- Invariant.cs
- SpoolingTaskBase.cs
- BitmapInitialize.cs
- CompositeFontParser.cs
- ServiceActivationException.cs
- ByteAnimationUsingKeyFrames.cs
- SystemFonts.cs
- FlowPosition.cs
- FixedSOMSemanticBox.cs
- __ConsoleStream.cs
- CacheOutputQuery.cs
- MeasureItemEvent.cs
- XmlSchemaCollection.cs
- SelfIssuedSamlTokenFactory.cs
- SafeTokenHandle.cs
- ToolStripGripRenderEventArgs.cs
- EntityDataSourceUtil.cs
- Pen.cs
- ClientTarget.cs
- X509CertificateClaimSet.cs
- MouseGestureValueSerializer.cs
- RelationshipType.cs
- MultilineStringConverter.cs
- EmbeddedMailObjectsCollection.cs
- ConfigPathUtility.cs
- ArrangedElement.cs
- TextTreePropertyUndoUnit.cs
- PointLightBase.cs
- MsmqInputChannelListener.cs
- InternalConfigHost.cs
- HandleCollector.cs
- TableLayoutSettingsTypeConverter.cs
- ExternalFile.cs
- Inline.cs
- JournalEntryStack.cs
- Matrix.cs
- ComplexBindingPropertiesAttribute.cs
- RewritingProcessor.cs
- BaseTreeIterator.cs
- LogWriteRestartAreaAsyncResult.cs
- COSERVERINFO.cs
- IntersectQueryOperator.cs
- RectangleF.cs
- Button.cs
- MailBnfHelper.cs
- XmlDictionaryString.cs
- HostingPreferredMapPath.cs
- UrlAuthorizationModule.cs
- SchemaElementDecl.cs
- AdRotatorDesigner.cs
- ApplicationBuildProvider.cs
- LineSegment.cs
- CodeAttributeDeclarationCollection.cs
- FileUpload.cs
- SplitterPanelDesigner.cs
- DecimalConstantAttribute.cs
- ArraySegment.cs
- CursorInteropHelper.cs
- HtmlSelect.cs
- XmlDocumentSurrogate.cs
- ObjectItemCachedAssemblyLoader.cs
- SendParametersContent.cs
- Parsers.cs
- _SpnDictionary.cs