ezEngine  Milestone 7
ezCompressedStreamWriter Class Reference

A stream writer that will compress all incoming data and then passes it on into another stream. More...

#include <CompressedStream.h>

Inheritance diagram for ezCompressedStreamWriter:

Public Types

enum  Compression {
  Uncompressed = 0, Fastest = 1, Fast = 3, Average = 5,
  High = 7, Highest = 9, Default = Fastest
}
 Specifies the compression level of the stream. More...
 

Public Member Functions

 ezCompressedStreamWriter (ezStreamWriterBase &OutputStream, Compression Ratio)
 The constructor takes another stream writer to pass the output into, and a compression level.
 
 ~ezCompressedStreamWriter ()
 Calls CloseStream() internally.
 
virtual ezResult WriteBytes (const void *pWriteBuffer, ezUInt64 uiBytesToWrite) override
 Compresses uiBytesToWrite from pWriteBuffer. More...
 
ezResult CloseStream ()
 Finishes the stream and writes all remaining data to the output stream. More...
 
ezUInt32 GetUncompressedSize () const
 Returns the size of the data in its uncompressed state.
 
ezUInt32 GetCompressedSize () const
 Returns the current compressed size of the data. More...
 
virtual ezResult Flush () override
 Writes the currently available compressed data to the stream. More...
 
- Public Member Functions inherited from ezStreamWriterBase
 ezStreamWriterBase ()
 Constructor.
 
virtual ~ezStreamWriterBase ()
 Virtual destructor to ensure correct cleanup.
 
template<typename T >
ezResult WriteWordValue (const T *pWordValue)
 Helper method to write a word value correctly (copes with potentially different endianess)
 
template<typename T >
ezResult WriteDWordValue (const T *pDWordValue)
 Helper method to write a dword value correctly (copes with potentially different endianess)
 
template<typename T >
ezResult WriteQWordValue (const T *pQWordValue)
 Helper method to write a qword value correctly (copes with potentially different endianess)
 

Private Attributes

ezUInt32 m_uiUncompressedSize
 
ezUInt32 m_uiCompressedSize
 
ezStreamWriterBasem_OutputStream
 
z_stream_s * m_pZLibStream
 
ezUInt8 m_CompressedCache [256]
 

Detailed Description

A stream writer that will compress all incoming data and then passes it on into another stream.

The stream uses an internal cache of 255 Bytes to compress data, before it passes that on to the output stream. It does not need to compress the entire data first, and it will not do any dynamic memory allocations. Calling Flush() will write the current amount of compressed data to the output stream. Calling this frequently might reduce the compression ratio and it should only be used to reduce output lag. However, there is absolutely no guarantee that all the data that was put into the stream will be readable from the output stream, after calling Flush(). In fact, it is quite likely that a large amount of data has still not been written to it, because it is still inside the compressor. Note that currently neither ezCompressedStreamReader nor ezCompressedStreamWriter are able to handle streams that are larger than 4 GB.

Member Enumeration Documentation

Specifies the compression level of the stream.

Enumerator
Default 

Should be preferred, good compression and good speed. Higher compression ratios save not much space but take considerably longer.

Member Function Documentation

ezResult ezCompressedStreamWriter::CloseStream ( )

Finishes the stream and writes all remaining data to the output stream.

After calling this function, no more data can be written to the stream. GetCompressedSize() will return the final compressed size of the data. Note that this function is not the same as Flush(), since Flush() assumes that more data can be written to the stream afterwards, which is not the case for CloseStream().

ezResult ezCompressedStreamWriter::Flush ( )
overridevirtual

Writes the currently available compressed data to the stream.

This does NOT guarantee that you can read all the uncompressed data from the output stream afterwards, because a lot of data will still be inside the compressor and thus not yet written to the stream.

Reimplemented from ezStreamWriterBase.

ezUInt32 ezCompressedStreamWriter::GetCompressedSize ( ) const
inline

Returns the current compressed size of the data.

This value is only accurate after CloseStream() has been called. Before that it is only a rough value, because a lot of data might still be cached and not yet accounted for. Note that GetCompressedSize() returns the compressed size of the data, not the size of the data that was written to the output stream, which will be larger (1 additional byte per 255 compressed bytes, plus one zero terminator byte).

ezResult ezCompressedStreamWriter::WriteBytes ( const void *  pWriteBuffer,
ezUInt64  uiBytesToWrite 
)
overridevirtual

Compresses uiBytesToWrite from pWriteBuffer.

Will output bursts of 256 bytes to the output stream every once in a while.

Implements ezStreamWriterBase.


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