Portability | portable (H98 + FFI) |
---|---|
Stability | provisional |
Maintainer | duncan@haskell.org |
Safe Haskell | Safe-Infered |
Codec.Compression.Zlib.Stream
Contents
Description
Zlib wrapper layer
- data Stream a
- run :: Stream a -> a
- unsafeInterleave :: Stream a -> Stream a
- unsafeLiftIO :: IO a -> Stream a
- finalise :: Stream ()
- deflateInit :: Format -> CompressionLevel -> Method -> WindowBits -> MemoryLevel -> CompressionStrategy -> Stream ()
- inflateInit :: Format -> WindowBits -> Stream ()
- data Format
- = GZip
- | Zlib
- | Raw
- | GZipOrZlib
- gzipFormat :: Format
- zlibFormat :: Format
- rawFormat :: Format
- gzipOrZlibFormat :: Format
- data CompressionLevel
- defaultCompression :: CompressionLevel
- noCompression :: CompressionLevel
- bestSpeed :: CompressionLevel
- bestCompression :: CompressionLevel
- compressionLevel :: Int -> CompressionLevel
- data Method = Deflated
- deflateMethod :: Method
- data WindowBits
- defaultWindowBits :: WindowBits
- windowBits :: Int -> WindowBits
- data MemoryLevel
- defaultMemoryLevel :: MemoryLevel
- minMemoryLevel :: MemoryLevel
- maxMemoryLevel :: MemoryLevel
- memoryLevel :: Int -> MemoryLevel
- data CompressionStrategy
- defaultStrategy :: CompressionStrategy
- filteredStrategy :: CompressionStrategy
- huffmanOnlyStrategy :: CompressionStrategy
- deflate :: Flush -> Stream Status
- inflate :: Flush -> Stream Status
- data Status
- data Flush
- data ErrorCode
- = NeedDict
- | FileError
- | StreamError
- | DataError
- | MemoryError
- | BufferError
- | VersionError
- | Unexpected
- pushInputBuffer :: ForeignPtr Word8 -> Int -> Int -> Stream ()
- inputBufferEmpty :: Stream Bool
- pushOutputBuffer :: ForeignPtr Word8 -> Int -> Int -> Stream ()
- popOutputBuffer :: Stream (ForeignPtr Word8, Int, Int)
- outputBufferBytesAvailable :: Stream Int
- outputBufferSpaceRemaining :: Stream Int
- outputBufferFull :: Stream Bool
The Zlib state monad
unsafeInterleave :: Stream a -> Stream aSource
unsafeLiftIO :: IO a -> Stream aSource
This never needs to be used as the stream's resources will be released automatically when no longer needed, however this can be used to release them early. Only use this when you can guarantee that the stream will no longer be needed, for example if an error occurs or if the stream ends.
Initialisation
deflateInit :: Format -> CompressionLevel -> Method -> WindowBits -> MemoryLevel -> CompressionStrategy -> Stream ()Source
inflateInit :: Format -> WindowBits -> Stream ()Source
Initialisation parameters
The format used for compression or decompression. There are three variations.
Constructors
GZip | |
Zlib | |
Raw | |
GZipOrZlib |
The gzip format uses a header with a checksum and some optional meta-data about the compressed file. It is intended primarily for compressing individual files but is also sometimes used for network protocols such as HTTP. The format is described in detail in RFC #1952 http://www.ietf.org/rfc/rfc1952.txt
The zlib format uses a minimal header with a checksum but no other meta-data. It is especially designed for use in network protocols. The format is described in detail in RFC #1950 http://www.ietf.org/rfc/rfc1950.txt
The 'raw' format is just the compressed data stream without any additional header, meta-data or data-integrity checksum. The format is described in detail in RFC #1951 http://www.ietf.org/rfc/rfc1951.txt
gzipOrZlibFormat :: FormatSource
This is not a format as such. It enabled zlib or gzip decoding with automatic header detection. This only makes sense for decompression.
data CompressionLevel Source
The compression level parameter controls the amount of compression. This is a trade-off between the amount of compression and the time required to do the compression.
defaultCompression :: CompressionLevelSource
The default compression level is 6 (that is, biased towards higher compression at expense of speed).
noCompression :: CompressionLevelSource
No compression, just a block copy.
bestSpeed :: CompressionLevelSource
The fastest compression method (less compression)
bestCompression :: CompressionLevelSource
The slowest compression method (best compression).
compressionLevel :: Int -> CompressionLevelSource
A specific compression level between 0 and 9.
'Deflate' is the only method supported in this version of zlib. Indeed it is likely to be the only method that ever will be supported.
data WindowBits Source
This specifies the size of the compression window. Larger values of this parameter result in better compression at the expense of higher memory usage.
The compression window size is the value of the the window bits raised to
the power 2. The window bits must be in the range 8..15
which corresponds
to compression window sizes of 256b to 32Kb. The default is 15 which is also
the maximum size.
The total amount of memory used depends on the window bits and the
MemoryLevel
. See the MemoryLevel
for the details.
Constructors
WindowBits Int | |
DefaultWindowBits |
defaultWindowBits :: WindowBitsSource
The default WindowBits
is 15 which is also the maximum size.
windowBits :: Int -> WindowBitsSource
A specific compression window size, specified in bits in the range 8..15
data MemoryLevel Source
The MemoryLevel
parameter specifies how much memory should be allocated
for the internal compression state. It is a tradoff between memory usage,
compression ratio and compression speed. Using more memory allows faster
compression and a better compression ratio.
The total amount of memory used for compression depends on the WindowBits
and the MemoryLevel
. For decompression it depends only on the
WindowBits
. The totals are given by the functions:
compressTotal windowBits memLevel = 4 * 2^windowBits + 512 * 2^memLevel decompressTotal windowBits = 2^windowBits
For example, for compression with the default windowBits = 15
and
memLevel = 8
uses 256Kb
. So for example a network server with 100
concurrent compressed streams would use 25Mb
. The memory per stream can be
halved (at the cost of somewhat degraded and slower compressionby) by
reducing the windowBits
and memLevel
by one.
Decompression takes less memory, the default windowBits = 15
corresponds
to just 32Kb
.
Constructors
DefaultMemoryLevel | |
MinMemoryLevel | |
MaxMemoryLevel | |
MemoryLevel Int |
defaultMemoryLevel :: MemoryLevelSource
The default memory level. (Equivalent to
)
memoryLevel
8
minMemoryLevel :: MemoryLevelSource
Use minimum memory. This is slow and reduces the compression ratio.
(Equivalent to
)
memoryLevel
1
maxMemoryLevel :: MemoryLevelSource
Use maximum memory for optimal compression speed.
(Equivalent to
)
memoryLevel
9
memoryLevel :: Int -> MemoryLevelSource
A specific level in the range 1..9
data CompressionStrategy Source
The strategy parameter is used to tune the compression algorithm.
The strategy parameter only affects the compression ratio but not the correctness of the compressed output even if it is not set appropriately.
Constructors
DefaultStrategy | |
Filtered | |
HuffmanOnly |
defaultStrategy :: CompressionStrategySource
Use this default compression strategy for normal data.
filteredStrategy :: CompressionStrategySource
Use the filtered compression strategy for data produced by a filter (or
predictor). Filtered data consists mostly of small values with a somewhat
random distribution. In this case, the compression algorithm is tuned to
compress them better. The effect of this strategy is to force more Huffman
coding and less string matching; it is somewhat intermediate between
defaultCompressionStrategy
and huffmanOnlyCompressionStrategy
.
huffmanOnlyStrategy :: CompressionStrategySource
Use the Huffman-only compression strategy to force Huffman encoding only (no string match).
The buisness
Constructors
NeedDict | |
FileError | |
StreamError | |
DataError | |
MemoryError | |
BufferError | No progress was possible or there was not enough room in
the output buffer when |
VersionError | |
Unexpected |
Buffer management
Input buffer
pushInputBuffer :: ForeignPtr Word8 -> Int -> Int -> Stream ()Source
Output buffer
pushOutputBuffer :: ForeignPtr Word8 -> Int -> Int -> Stream ()Source
popOutputBuffer :: Stream (ForeignPtr Word8, Int, Int)Source