Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / 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
- UnsafeNativeMethods.cs
- ResourceProperty.cs
- PageAsyncTaskManager.cs
- GenericUI.cs
- QuotedPrintableStream.cs
- PeerNameRecord.cs
- TransformerInfoCollection.cs
- ClientApiGenerator.cs
- TagPrefixInfo.cs
- _TLSstream.cs
- CompilerError.cs
- DataGridViewCellToolTipTextNeededEventArgs.cs
- CompilerResults.cs
- SerializationInfoEnumerator.cs
- EdmComplexTypeAttribute.cs
- NetworkInterface.cs
- EndpointAddress10.cs
- ToolStripScrollButton.cs
- EmbeddedMailObject.cs
- webeventbuffer.cs
- PublishLicense.cs
- WindowsFormsDesignerOptionService.cs
- BamlTreeUpdater.cs
- XPathNodePointer.cs
- UnsafeNativeMethods.cs
- backend.cs
- DbQueryCommandTree.cs
- ping.cs
- Pens.cs
- CompiledELinqQueryState.cs
- TextDecoration.cs
- WindowsListBox.cs
- NotifyInputEventArgs.cs
- RowBinding.cs
- HideDisabledControlAdapter.cs
- Color.cs
- ServiceElementCollection.cs
- CacheModeConverter.cs
- BaseTreeIterator.cs
- _SSPIWrapper.cs
- DataServiceRequestException.cs
- sqlnorm.cs
- WpfPayload.cs
- XmlMapping.cs
- UITypeEditors.cs
- SmiMetaDataProperty.cs
- WebPartMenuStyle.cs
- StructuralObject.cs
- DataGridViewAccessibleObject.cs
- TraceAsyncResult.cs
- ResXBuildProvider.cs
- PrintPreviewGraphics.cs
- OrderedEnumerableRowCollection.cs
- HoistedLocals.cs
- FamilyTypefaceCollection.cs
- HttpListenerPrefixCollection.cs
- HtmlElement.cs
- DbConnectionStringBuilder.cs
- ImageMetadata.cs
- SmtpTransport.cs
- MsmqInputMessagePool.cs
- ServiceCredentials.cs
- HMACRIPEMD160.cs
- XmlTextAttribute.cs
- CurrentChangedEventManager.cs
- ComPlusDiagnosticTraceRecords.cs
- EventLogSession.cs
- BufferedReadStream.cs
- TableRowGroup.cs
- odbcmetadatafactory.cs
- TypeInitializationException.cs
- InkCollectionBehavior.cs
- ManipulationBoundaryFeedbackEventArgs.cs
- SoapServerMessage.cs
- FixedLineResult.cs
- LoadGrammarCompletedEventArgs.cs
- PrtCap_Base.cs
- PhonemeConverter.cs
- MatrixTransform.cs
- WebPartConnectionsEventArgs.cs
- UriTemplate.cs
- XmlConverter.cs
- PopupControlService.cs
- StaticTextPointer.cs
- FloatUtil.cs
- SmiSettersStream.cs
- HtmlElementErrorEventArgs.cs
- EntityParameter.cs
- TrackingLocation.cs
- DrawingServices.cs
- MenuItem.cs
- WebConfigurationHostFileChange.cs
- RotateTransform3D.cs
- TableRowCollection.cs
- SourceFilter.cs
- LambdaCompiler.Unary.cs
- SafePEFileHandle.cs
- FixedSOMFixedBlock.cs
- NetworkInformationPermission.cs
- LogEntryDeserializer.cs