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
- XmlSchemaException.cs
- WorkflowPageSetupDialog.cs
- LabelTarget.cs
- DemultiplexingDispatchMessageFormatter.cs
- SamlAssertionKeyIdentifierClause.cs
- TrustLevel.cs
- securitycriticaldataformultiplegetandset.cs
- NamedPipeTransportSecurityElement.cs
- MethodAccessException.cs
- HttpCachePolicy.cs
- UnsafeNativeMethods.cs
- ZipPackagePart.cs
- UntrustedRecipientException.cs
- PanelStyle.cs
- RelationshipConverter.cs
- Pen.cs
- SQLInt64Storage.cs
- XPathParser.cs
- Win32Native.cs
- BitmapEffectCollection.cs
- ClientSession.cs
- HTMLTagNameToTypeMapper.cs
- WhitespaceRuleReader.cs
- CategoryNameCollection.cs
- WebPartHeaderCloseVerb.cs
- TextDecorationCollectionConverter.cs
- RawStylusInputReport.cs
- PropertyNames.cs
- DataGridBoundColumn.cs
- ObjectQueryState.cs
- UrlPath.cs
- EdgeModeValidation.cs
- InstalledFontCollection.cs
- StringKeyFrameCollection.cs
- EntityRecordInfo.cs
- StateDesignerConnector.cs
- InfocardClientCredentials.cs
- PackagePartCollection.cs
- EntityParameter.cs
- VisualStyleElement.cs
- NetworkInformationPermission.cs
- WebResourceAttribute.cs
- XmlSerializerVersionAttribute.cs
- LineSegment.cs
- DtrList.cs
- Attribute.cs
- DetailsView.cs
- ServiceOperationParameter.cs
- MimeTextImporter.cs
- SimplePropertyEntry.cs
- ChildTable.cs
- DeploymentSectionCache.cs
- GlyphCache.cs
- SettingsPropertyIsReadOnlyException.cs
- XmlQueryTypeFactory.cs
- PartialClassGenerationTaskInternal.cs
- AddInSegmentDirectoryNotFoundException.cs
- TextAdaptor.cs
- PathData.cs
- RectangleHotSpot.cs
- NullableIntSumAggregationOperator.cs
- SqlProfileProvider.cs
- HttpRequest.cs
- Variable.cs
- HttpRawResponse.cs
- NameValueFileSectionHandler.cs
- ThreadPool.cs
- WindowsImpersonationContext.cs
- FileFormatException.cs
- Rect3DConverter.cs
- MouseButtonEventArgs.cs
- IndexOutOfRangeException.cs
- FileChangesMonitor.cs
- DBSqlParser.cs
- ExpressionBindingCollection.cs
- BaseTreeIterator.cs
- WindowsBrush.cs
- PersonalizationStateQuery.cs
- SafeThreadHandle.cs
- TaskScheduler.cs
- UrlMappingsSection.cs
- LoginUtil.cs
- RegexCapture.cs
- ResourceDescriptionAttribute.cs
- CollaborationHelperFunctions.cs
- EllipseGeometry.cs
- WebException.cs
- ValidatedControlConverter.cs
- NotificationContext.cs
- LoginUtil.cs
- ContainerControl.cs
- OutputCacheModule.cs
- ScriptManager.cs
- RotateTransform3D.cs
- TagPrefixAttribute.cs
- ChangesetResponse.cs
- TextRangeAdaptor.cs
- StorageInfo.cs
- Polygon.cs
- WebPartDisplayModeCollection.cs