Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / IO / Packaging / DeflateEmulationStream.cs / 1305600 / 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
- ObjectSet.cs
- SimpleMailWebEventProvider.cs
- LayoutEngine.cs
- StreamSecurityUpgradeAcceptor.cs
- SafeReadContext.cs
- FieldAccessException.cs
- ViewDesigner.cs
- HttpDebugHandler.cs
- InkCanvasAutomationPeer.cs
- XsltConvert.cs
- SafeNativeMemoryHandle.cs
- MLangCodePageEncoding.cs
- SQLGuidStorage.cs
- GeometryGroup.cs
- log.cs
- SignatureHelper.cs
- NamedPipeTransportManager.cs
- BeginSelectCardRequest.cs
- DataGridViewImageCell.cs
- XmlNodeChangedEventManager.cs
- CurrentChangingEventArgs.cs
- AppDomainFactory.cs
- _TransmitFileOverlappedAsyncResult.cs
- ReadWriteObjectLock.cs
- Misc.cs
- SourceLineInfo.cs
- UserValidatedEventArgs.cs
- PageThemeParser.cs
- DrawingAttributes.cs
- DataListCommandEventArgs.cs
- SqlServices.cs
- AnimationClock.cs
- ListViewUpdatedEventArgs.cs
- XamlValidatingReader.cs
- TypeBrowser.xaml.cs
- NetworkStream.cs
- ToggleButtonAutomationPeer.cs
- WorkerRequest.cs
- FixedSOMLineCollection.cs
- ProfilePropertyMetadata.cs
- HostProtectionPermission.cs
- FunctionImportMapping.ReturnTypeRenameMapping.cs
- WebServiceErrorEvent.cs
- SessionEndingEventArgs.cs
- TextBreakpoint.cs
- TheQuery.cs
- Cursor.cs
- DataGridViewRowHeightInfoPushedEventArgs.cs
- FileRecordSequenceCompletedAsyncResult.cs
- TreeBuilderBamlTranslator.cs
- DoubleCollection.cs
- HttpInputStream.cs
- LayoutTableCell.cs
- KerberosTokenFactoryCredential.cs
- XmlSiteMapProvider.cs
- _LocalDataStore.cs
- Config.cs
- WindowsFormsHelpers.cs
- OdbcEnvironment.cs
- HttpServerVarsCollection.cs
- TextRange.cs
- DBDataPermission.cs
- DataGridViewRowCancelEventArgs.cs
- UrlAuthorizationModule.cs
- BmpBitmapEncoder.cs
- XmlSchemaElement.cs
- UrlMappingCollection.cs
- BitStream.cs
- EmptyEnumerator.cs
- Executor.cs
- SqlConnectionStringBuilder.cs
- OneWayChannelListener.cs
- EntityDataSourceSelectedEventArgs.cs
- EntityViewGenerationAttribute.cs
- ToolStripItemImageRenderEventArgs.cs
- VerificationAttribute.cs
- ModifierKeysConverter.cs
- ColumnHeader.cs
- KnownTypes.cs
- OutputScope.cs
- GridViewHeaderRowPresenterAutomationPeer.cs
- GroupStyle.cs
- DNS.cs
- ButtonBaseAutomationPeer.cs
- PropertyKey.cs
- Cell.cs
- ComUdtElement.cs
- TreeIterators.cs
- DynamicUpdateCommand.cs
- ClipboardData.cs
- MetadataReference.cs
- StateElementCollection.cs
- XmlNodeChangedEventManager.cs
- recordstatefactory.cs
- PeerCollaboration.cs
- Journal.cs
- GlyphTypeface.cs
- HierarchicalDataTemplate.cs
- MatrixValueSerializer.cs
- FixedBufferAttribute.cs