tango.io.stream.Zlib

License:

BSD style: see license.txt

Author:

Daniel Keep

Version:

Feb 08: Added support for different stream encodings, removed old "window bits" ctors.
Dec 07: Added support for "window bits", needed for Zip support.
Jul 07: Initial release.
class ZlibInput : InputFilter
This input filter can be used to perform decompression of zlib streams.
enum Encoding : int
This enumeration allows you to specify the encoding of the compressed stream.
Guess
The code should attempt to automatically determine what the encoding of the stream should be. Note that this cannot detect the case where the stream was compressed with no encoding.
Zlib
Stream has zlib encoding.
Gzip
Stream has gzip encoding.
None
Stream has no encoding.
this(InputStream stream, Encoding encoding, int windowBits = WINDOWBITS_DEFAULT)
this(InputStream stream)
Constructs a new zlib decompression filter. You need to pass in the stream that the decompression filter will read from. If you are using this filter with a conduit, the idiom to use is:
1
2
auto input = new ZlibInput(myConduit.input));
input.read(myContent);

The optional windowBits parameter is the base two logarithm of the window size, and should be in the range 8-15, defaulting to 15 if not specified. Additionally, the windowBits parameter may be negative to indicate that zlib should omit the standard zlib header and trailer, with the window size being -windowBits.

Parameters:

streamCompressed input stream.
encodingStream encoding. Defaults to Encoding.Guess, which should be sufficient unless the stream was compressed with no encoding; in this case, you must manually specify Encoding.None.
windowBitsThe base two logarithm of the window size, and should be in the range 8-15, defaulting to 15 if not specified.
void reset(InputStream stream, Encoding encoding, int windowBits = WINDOWBITS_DEFAULT)
void reset(InputStream stream)
Resets and re-initialises this instance.
If you are creating compression streams inside a loop, you may wish to use this method to re-use a single instance. This prevents the potentially costly re-allocation of internal buffers.

The stream must have already been closed before calling reset.
size_t read(void[] dst) [override]
Decompresses data from the underlying conduit into a target array.
Returns the number of bytes stored into dst, which may be less than requested.
void close() [override]
Closes the compression stream.
class ZlibOutput : OutputFilter
This output filter can be used to perform compression of data into a zlib stream.
enum Level : int
This enumeration represents several pre-defined compression levels.
Any integer between -1 and 9 inclusive may be used as a level, although the symbols in this enumeration should suffice for most use-cases.
Normal
Default compression level. This is selected for a good compromise between speed and compression, and the exact compression level is determined by the underlying zlib library. Should be roughly equivalent to compression level 6.
None
Do not perform compression. This will cause the stream to expand slightly to accommodate stream metadata.
Fast
Minimal compression; the fastest level which performs at least some compression.
Best
Maximal compression.
enum Encoding : int
This enumeration allows you to specify what the encoding of the compressed stream should be.
Zlib
Stream should use zlib encoding.
Gzip
Stream should use gzip encoding.
None
Stream should use no encoding.
this(OutputStream stream, Level level, Encoding encoding, int windowBits = WINDOWBITS_DEFAULT)
this(OutputStream stream, Level level = Normal)
Constructs a new zlib compression filter. You need to pass in the stream that the compression filter will write to. If you are using this filter with a conduit, the idiom to use is:
1
2
auto output = new ZlibOutput(myConduit.output);
output.write(myContent);

The optional windowBits parameter is the base two logarithm of the window size, and should be in the range 8-15, defaulting to 15 if not specified. Additionally, the windowBits parameter may be negative to indicate that zlib should omit the standard zlib header and trailer, with the window size being -windowBits.
void reset(OutputStream stream, Level level, Encoding encoding, int windowBits = WINDOWBITS_DEFAULT)
void reset(OutputStream stream, Level level = Normal)
Resets and re-initialises this instance.
If you are creating compression streams inside a loop, you may wish to use this method to re-use a single instance. This prevents the potentially costly re-allocation of internal buffers.

The stream must have already been closed or committed before calling reset.
size_t write(const(void)[] src) [override]
Compresses the given data to the underlying conduit.
Returns the number of bytes from src that were compressed; write should always consume all data provided to it, although it may not be immediately written to the underlying output stream.
size_t written()
This read-only property returns the number of compressed bytes that have been written to the underlying stream. Following a call to either close or commit, this will contain the total compressed size of the input data stream.
void close() [override]
Close the compression stream. This will cause any buffered content to be committed to the underlying stream.
void commit()
Purge any buffered content. Calling this will implicitly end the zlib stream, so it should not be called until you are finished compressing data. Any calls to either write or commit after a compression filter has been committed will throw an exception.
The only difference between calling this method and calling close is that the underlying stream will not be closed.
class ZlibClosedException : IOException
This exception is thrown if you attempt to perform a read, write or flush operation on a closed zlib filter stream. This can occur if the input stream has finished, or an output stream was flushed.
class ZlibStillOpenException : IOException
This exception is thrown if you attempt to reset a compression stream that is still open. You must either close or commit a stream before it can be reset.
class ZlibException : IOException
This exception is thrown when an error occurs in the underlying zlib library. Where possible, it will indicate both the name of the error, and any textural message zlib has provided.