My Project
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Public Member Functions | Properties | List of all members
UniExtensions.Zlib.ZlibStream Class Reference

Represents a Zlib stream for compression or decompression. More...

Inheritance diagram for UniExtensions.Zlib.ZlibStream:

Public Member Functions

 ZlibStream (System.IO.Stream stream, CompressionMode mode)
 Create a ZlibStream using the specified CompressionMode. More...
 
 ZlibStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
 Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel. More...
 
 ZlibStream (System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
 Create a ZlibStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation. More...
 
 ZlibStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
 Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation. More...
 
override void Close ()
 Close the stream. More...
 
override void Flush ()
 Flush the stream. More...
 
override int Read (byte[] buffer, int offset, int count)
 Read data from the stream. More...
 
override long Seek (long offset, System.IO.SeekOrigin origin)
 Calling this method always throws a NotImplementedException. More...
 
override void SetLength (long value)
 Calling this method always throws a NotImplementedException. More...
 
override void Write (byte[] buffer, int offset, int count)
 Write data to the stream. More...
 

Properties

virtual int FlushMode [get, set]
 This property sets the flush behavior on the stream. Sorry, though, not sure exactly how to describe all the various settings. More...
 
int BufferSize [get, set]
 Callers can set the buffer size of the working buffer with this property. More...
 
virtual long TotalIn [get]
 Returns the total number of bytes input so far. More...
 
virtual long TotalOut [get]
 Returns the total number of bytes output so far. More...
 
override bool CanRead [get]
 Indicates whether the stream can be read. More...
 
override bool CanSeek [get]
 Indicates whether the stream supports Seek operations. More...
 
override bool CanWrite [get]
 Indicates whether the stream can be written. More...
 
override long Length [get]
 Reading this property always throws a NotImplementedException. More...
 
override long Position [get, set]
 Reading or Writing this property always throws a NotImplementedException. More...
 

Detailed Description

Represents a Zlib stream for compression or decompression.

Data can be compressed or decompressed, and either of those can be through reading or writing. For more information on the Deflate algorithm, see IETF RFC 1951, "DEFLATE Compressed Data Format Specification version 1.3."

This class is similar to DeflateStream, except that it adds the RFC1950 header bytes to a compressed stream when compressing, or expects the RFC1950 header bytes when decompressing. It is also similar to the GZipStream.

See also
DeflateStream, GZipStream

Constructor & Destructor Documentation

UniExtensions.Zlib.ZlibStream.ZlibStream ( System.IO.Stream  stream,
CompressionMode  mode 
)
inline

Create a ZlibStream using the specified CompressionMode.

The ZlibStream will use the default compression level.

See the documentation for the DeflateStream constructors for example code.

Parameters
streamThe stream which will be read or written.
modeIndicates whether the ZlibStream will compress or decompress.
UniExtensions.Zlib.ZlibStream.ZlibStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level 
)
inline

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

See the documentation for the DeflateStream constructors for example code.

Parameters
streamThe stream to be read or written while deflating or inflating.
modeIndicates whether the ZlibStream will compress or decompress.
levelA tuning knob to trade speed for effectiveness.
UniExtensions.Zlib.ZlibStream.ZlibStream ( System.IO.Stream  stream,
CompressionMode  mode,
bool  leaveOpen 
)
inline

Create a ZlibStream 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 ZlibStream will use the default compression level.

See the documentation for the DeflateStream constructors for example code.

Parameters
streamThe stream which will be read or written. This is called the "captive" stream in other places in this documentation.
modeIndicates whether the ZlibStream will compress or decompress.
leaveOpentrue if the application would like the stream to remain open after inflation/deflation.
UniExtensions.Zlib.ZlibStream.ZlibStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level,
bool  leaveOpen 
)
inline

Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel, 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.

public void TestStreamCompression()
{
System.IO.MemoryStream msSinkCompressed;
System.IO.MemoryStream msSinkDecompressed;
ZlibStream zOut;
String helloOriginal = "Hello, World! This String will be compressed...";
// first, compress:
msSinkCompressed = new System.IO.MemoryStream();
zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.LEVEL9_BEST_COMPRESSION, true);
CopyStream(StringToMemoryStream(helloOriginal), zOut);
zOut.Close();
// at this point, msSinkCompressed contains the compressed bytes
// now, decompress:
msSinkDecompressed = new System.IO.MemoryStream();
zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress);
msSinkCompressed.Position = 0;
CopyStream(msSinkCompressed, zOut);
string result = MemoryStreamToString(msSinkDecompressed);
Console.WriteLine("decompressed: {0}", result);
}
private static void CopyStream(System.IO.Stream src, System.IO.Stream dest)
{
byte[] buffer = new byte[1024];
int len = src.Read(buffer, 0, buffer.Length);
while (len > 0)
{
dest.Write(buffer, 0, len);
len = src.Read(buffer, 0, buffer.Length);
}
dest.Flush();
}
static System.IO.MemoryStream StringToMemoryStream(string s)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
int byteCount = enc.GetByteCount(s.ToCharArray(), 0, s.Length);
byte[] ByteArray = new byte[byteCount];
int bytesEncodedCount = enc.GetBytes(s, 0, s.Length, ByteArray, 0);
System.IO.MemoryStream ms = new System.IO.MemoryStream(ByteArray);
return ms;
}
static String MemoryStreamToString(System.IO.MemoryStream ms)
{
byte[] ByteArray = ms.ToArray();
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
var s = enc.GetString(ByteArray);
return s;
}
Parameters
streamThe stream which will be read or written.
modeIndicates whether the ZlibStream will compress or decompress.
leaveOpentrue if the application would like the stream to remain open after inflation/deflation.
levelA tuning knob to trade speed for effectiveness.

Member Function Documentation

override void UniExtensions.Zlib.ZlibStream.Close ( )
inline

Close the stream.

This may or may not close the captive stream. See the ctor's with leaveOpen parameters for more information.

override void UniExtensions.Zlib.ZlibStream.Flush ( )
inline

Flush the stream.

override int UniExtensions.Zlib.ZlibStream.Read ( byte[]  buffer,
int  offset,
int  count 
)
inline

Read data from the stream.

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

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

Parameters
bufferThe buffer into which the read data should be placed.
offsetthe offset within that data array to put the first byte read.
countthe number of bytes to read.
override long UniExtensions.Zlib.ZlibStream.Seek ( long  offset,
System.IO.SeekOrigin  origin 
)
inline

Calling this method always throws a NotImplementedException.

override void UniExtensions.Zlib.ZlibStream.SetLength ( long  value)
inline

Calling this method always throws a NotImplementedException.

override void UniExtensions.Zlib.ZlibStream.Write ( byte[]  buffer,
int  offset,
int  count 
)
inline

Write data to the stream.

If you wish to use the ZlibStream to compress data while writing, you can create a ZlibStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that ZlibStream, 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 ZlibStream to decompress data while writing, you can create a ZlibStream 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 ZlibStream can be used for Read() or Write(), but not both.

Parameters
bufferThe buffer holding data to write to the stream.
offsetthe offset within that data array to find the first byte to write.
countthe number of bytes to write.

Property Documentation

int UniExtensions.Zlib.ZlibStream.BufferSize
getset

Callers can set the buffer size of the working buffer with this property.

The working buffer is used for all stream operations. The default size is 1024 bytes. The minimum size is 128 bytes. You may get better performance with a larger buffer. Then again, you might not. I don't know, I haven't tested it.

override bool UniExtensions.Zlib.ZlibStream.CanRead
get

Indicates whether the stream can be read.

The return value depends on whether the captive stream supports reading.

override bool UniExtensions.Zlib.ZlibStream.CanSeek
get

Indicates whether the stream supports Seek operations.

Always returns false.

override bool UniExtensions.Zlib.ZlibStream.CanWrite
get

Indicates whether the stream can be written.

The return value depends on whether the captive stream supports writing.

virtual int UniExtensions.Zlib.ZlibStream.FlushMode
getset

This property sets the flush behavior on the stream. Sorry, though, not sure exactly how to describe all the various settings.

override long UniExtensions.Zlib.ZlibStream.Length
get

Reading this property always throws a NotImplementedException.

override long UniExtensions.Zlib.ZlibStream.Position
getset

Reading or Writing this property always throws a NotImplementedException.

virtual long UniExtensions.Zlib.ZlibStream.TotalIn
get

Returns the total number of bytes input so far.

virtual long UniExtensions.Zlib.ZlibStream.TotalOut
get

Returns the total number of bytes output so far.


The documentation for this class was generated from the following file: