C# Class Cimbalino.Phone.Toolkit.Compression.DeflateStream

A class for compressing and decompressing streams using the Deflate algorithm.

The DeflateStream is a Decorator on a . It adds DEFLATE compression or decompression to any stream.

Using this stream, applications can compress or decompress data via stream Read and Write operations. Either compresssion or decompression can occur through either reading or writing. The compression format used is DEFLATE, which is documented in IETF RFC 1951, "DEFLATE Compressed Data Format Specification version 1.3.".

This class is similar to ZlibStream, except that ZlibStream adds the RFC 1950 - ZLIB framing bytes to a compressed stream when compressing, or expects the RFC1950 framing bytes when decompressing. The DeflateStream does not.

Inheritance: System.IO.Stream
ファイルを表示 Open project: Cimbalino/Cimbalino-Phone-Toolkit Class Usage Examples

Public Methods

Method Description
CompressBuffer ( byte b ) : byte[]

Compress a byte array into a new byte array using DEFLATE.

Uncompress it with DeflateStream.UncompressBuffer(byte[]).

CompressString ( String s ) : byte[]

Compress a string into a byte array using DEFLATE (RFC 1951).

Uncompress it with DeflateStream.UncompressString(byte[]).

DeflateStream ( System stream, CompressionMode mode ) : System

Create a DeflateStream using the specified CompressionMode.

When mode is CompressionMode.Compress, the DeflateStream will use the default compression level. The "captive" stream will be closed when the DeflateStream is closed.

DeflateStream ( System stream, CompressionMode mode, CompressionLevel level ) : System

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.

When mode is CompressionMode.Decompress, the level parameter is ignored. The "captive" stream will be closed when the DeflateStream is closed.

DeflateStream ( System stream, CompressionMode mode, CompressionLevel level, bool leaveOpen ) : System

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation.

When mode is CompressionMode.Decompress, the level parameter is ignored.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a System.IO.MemoryStream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

DeflateStream ( System stream, CompressionMode mode, bool leaveOpen ) : System

Create a DeflateStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a memory stream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

The DeflateStream will use the default compression level.

See the other overloads of this constructor for example code.

Flush ( ) : void

Flush the stream.

Read ( byte buffer, int offset, int count ) : int

Read data from the stream.

If you wish to use the DeflateStream to compress data while reading, you can create a DeflateStream with CompressionMode.Compress, providing an uncompressed data stream. Then call Read() on that DeflateStream, and the data read will be compressed as you read. If you wish to use the DeflateStream to decompress data while reading, you can create a DeflateStream with CompressionMode.Decompress, providing a readable compressed data stream. Then call Read() on that DeflateStream, and the data read will be decompressed as you read.

A DeflateStream can be used for Read() or Write(), but not both.

Seek ( long offset, System origin ) : long

Calling this method always throws a NotImplementedException.

SetLength ( long value ) : void

Calling this method always throws a NotImplementedException.

UncompressBuffer ( byte compressed ) : byte[]

Uncompress a DEFLATE'd byte array into a byte array.

UncompressString ( byte compressed ) : String

Uncompress a DEFLATE'd byte array into a single string.

Write ( byte buffer, int offset, int count ) : void

Write data to the stream.

If you wish to use the DeflateStream to compress data while writing, you can create a DeflateStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that DeflateStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the data written. If you wish to use the DeflateStream to decompress data while writing, you can create a DeflateStream with CompressionMode.Decompress, and a writable output stream. Then call Write() on that stream, providing previously compressed data. The data sent to the output stream will be the decompressed form of the data written.

A DeflateStream can be used for Read() or Write(), but not both.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Dispose the stream.

This may or may not result in a Close() call on the captive stream. See the constructors that have a leaveOpen parameter for more information.

Application code won't call this code directly. This method may be invoked in two distinct scenarios. If disposing == true, the method has been called directly or indirectly by a user's code, for example via the public Dispose() method. In this case, both managed and unmanaged resources can be referenced and disposed. If disposing == false, the method has been called by the runtime from inside the object finalizer and this method should not reference other objects; in that case only unmanaged resources must be referenced or disposed.

Method Details

CompressBuffer() public static method

Compress a byte array into a new byte array using DEFLATE.
Uncompress it with DeflateStream.UncompressBuffer(byte[]).
public static CompressBuffer ( byte b ) : byte[]
b byte /// A buffer to compress. ///
return byte[]

CompressString() public static method

Compress a string into a byte array using DEFLATE (RFC 1951).
Uncompress it with DeflateStream.UncompressString(byte[]).
public static CompressString ( String s ) : byte[]
s String /// A string to compress. The string will first be encoded /// using UTF8, then compressed. ///
return byte[]

DeflateStream() public method

Create a DeflateStream using the specified CompressionMode.
When mode is CompressionMode.Compress, the DeflateStream will use the default compression level. The "captive" stream will be closed when the DeflateStream is closed.
public DeflateStream ( System stream, CompressionMode mode ) : System
stream System The stream which will be read or written.
mode CompressionMode Indicates whether the DeflateStream will compress or decompress.
return System

DeflateStream() public method

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.

When mode is CompressionMode.Decompress, the level parameter is ignored. The "captive" stream will be closed when the DeflateStream is closed.

public DeflateStream ( System stream, CompressionMode mode, CompressionLevel level ) : System
stream System The stream to be read or written while deflating or inflating.
mode CompressionMode Indicates whether the DeflateStream will compress or decompress.
level CompressionLevel A tuning knob to trade speed for effectiveness.
return System

DeflateStream() public method

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation.

When mode is CompressionMode.Decompress, the level parameter is ignored.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a System.IO.MemoryStream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

public DeflateStream ( System stream, CompressionMode mode, CompressionLevel level, bool leaveOpen ) : System
stream System The stream which will be read or written.
mode CompressionMode Indicates whether the DeflateStream will compress or decompress.
level CompressionLevel A tuning knob to trade speed for effectiveness.
leaveOpen bool true if the application would like the stream to remain open after inflation/deflation.
return System

DeflateStream() public method

Create a DeflateStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a memory stream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

The DeflateStream will use the default compression level.

See the other overloads of this constructor for example code.

public DeflateStream ( System stream, CompressionMode mode, bool leaveOpen ) : System
stream System /// The stream which will be read or written. This is called the /// "captive" stream in other places in this documentation. ///
mode CompressionMode /// Indicates whether the DeflateStream will compress or decompress. ///
leaveOpen bool true if the application would like the stream to /// remain open after inflation/deflation.
return System

Dispose() protected method

Dispose the stream.

This may or may not result in a Close() call on the captive stream. See the constructors that have a leaveOpen parameter for more information.

Application code won't call this code directly. This method may be invoked in two distinct scenarios. If disposing == true, the method has been called directly or indirectly by a user's code, for example via the public Dispose() method. In this case, both managed and unmanaged resources can be referenced and disposed. If disposing == false, the method has been called by the runtime from inside the object finalizer and this method should not reference other objects; in that case only unmanaged resources must be referenced or disposed.

protected Dispose ( bool disposing ) : void
disposing bool /// true if the Dispose method was invoked by user code. ///
return void

Flush() public method

Flush the stream.
public Flush ( ) : void
return void

Read() public method

Read data from the stream.

If you wish to use the DeflateStream to compress data while reading, you can create a DeflateStream with CompressionMode.Compress, providing an uncompressed data stream. Then call Read() on that DeflateStream, and the data read will be compressed as you read. If you wish to use the DeflateStream to decompress data while reading, you can create a DeflateStream with CompressionMode.Decompress, providing a readable compressed data stream. Then call Read() on that DeflateStream, and the data read will be decompressed as you read.

A DeflateStream can be used for Read() or Write(), but not both.

public Read ( byte buffer, int offset, int count ) : int
buffer byte The buffer into which the read data should be placed.
offset int the offset within that data array to put the first byte read.
count int the number of bytes to read.
return int

Seek() public method

Calling this method always throws a NotImplementedException.
public Seek ( long offset, System origin ) : long
offset long this is irrelevant, since it will always throw!
origin System this is irrelevant, since it will always throw!
return long

SetLength() public method

Calling this method always throws a NotImplementedException.
public SetLength ( long value ) : void
value long this is irrelevant, since it will always throw!
return void

UncompressBuffer() public static method

Uncompress a DEFLATE'd byte array into a byte array.
public static UncompressBuffer ( byte compressed ) : byte[]
compressed byte /// A buffer containing data that has been compressed with DEFLATE. ///
return byte[]

UncompressString() public static method

Uncompress a DEFLATE'd byte array into a single string.
public static UncompressString ( byte compressed ) : String
compressed byte /// A buffer containing DEFLATE-compressed data. ///
return String

Write() public method

Write data to the stream.

If you wish to use the DeflateStream to compress data while writing, you can create a DeflateStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that DeflateStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the data written. If you wish to use the DeflateStream to decompress data while writing, you can create a DeflateStream with CompressionMode.Decompress, and a writable output stream. Then call Write() on that stream, providing previously compressed data. The data sent to the output stream will be the decompressed form of the data written.

A DeflateStream can be used for Read() or Write(), but not both.

public Write ( byte buffer, int offset, int count ) : void
buffer byte The buffer holding data to write to the stream.
offset int the offset within that data array to find the first byte to write.
count int the number of bytes to write.
return void