Skip to content

Commit 6ebe98f

Browse files
committed
Allow to select flushing strategy NoFlush is the default, but the SyncFlush is useful with the incremental compression eg. websockets
1 parent 365f14b commit 6ebe98f

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

Codec/Compression/Zlib/Internal.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module Codec.Compression.Zlib.Internal (
4545
defaultCompressParams,
4646
DecompressParams(..),
4747
defaultDecompressParams,
48+
Stream.Flush(..),
4849
Stream.Format(..),
4950
Stream.gzipFormat,
5051
Stream.zlibFormat,
@@ -111,7 +112,8 @@ data CompressParams = CompressParams {
111112
compressMemoryLevel :: !Stream.MemoryLevel,
112113
compressStrategy :: !Stream.CompressionStrategy,
113114
compressBufferSize :: !Int,
114-
compressDictionary :: Maybe S.ByteString
115+
compressDictionary :: Maybe S.ByteString,
116+
compressFlush :: !Stream.Flush
115117
} deriving Show
116118

117119
-- | The full set of parameters for decompression. The defaults are
@@ -137,7 +139,8 @@ data DecompressParams = DecompressParams {
137139
decompressWindowBits :: !Stream.WindowBits,
138140
decompressBufferSize :: !Int,
139141
decompressDictionary :: Maybe S.ByteString,
140-
decompressAllMembers :: Bool
142+
decompressAllMembers :: Bool,
143+
decompressFlush :: !Stream.Flush
141144
} deriving Show
142145

143146
-- | The default set of parameters for compression. This is typically used with
@@ -151,7 +154,8 @@ defaultCompressParams = CompressParams {
151154
compressMemoryLevel = Stream.defaultMemoryLevel,
152155
compressStrategy = Stream.defaultStrategy,
153156
compressBufferSize = defaultCompressBufferSize,
154-
compressDictionary = Nothing
157+
compressDictionary = Nothing,
158+
compressFlush = Stream.NoFlush
155159
}
156160

157161
-- | The default set of parameters for decompression. This is typically used with
@@ -162,7 +166,8 @@ defaultDecompressParams = DecompressParams {
162166
decompressWindowBits = Stream.defaultWindowBits,
163167
decompressBufferSize = defaultDecompressBufferSize,
164168
decompressDictionary = Nothing,
165-
decompressAllMembers = True
169+
decompressAllMembers = True,
170+
decompressFlush = Stream.NoFlush
166171
}
167172

168173
-- | The default chunk sizes for the output of compression and decompression
@@ -466,7 +471,7 @@ compressIO format params = compressStreamIO format params
466471
compressStream :: Stream.Format -> CompressParams -> S.ByteString
467472
-> Stream (CompressStream Stream)
468473
compressStream format (CompressParams compLevel method bits memLevel
469-
strategy initChunkSize mdict) =
474+
strategy initChunkSize mdict flushStrategy) =
470475

471476
\chunk -> do
472477
Stream.deflateInit format compLevel method bits memLevel strategy
@@ -526,13 +531,13 @@ compressStream format (CompressParams compLevel method bits memLevel
526531
-- this invariant guarantees we can always make forward progress
527532
-- and that therefore a BufferError is impossible
528533

529-
let flush = if lastChunk then Stream.Finish else Stream.NoFlush
534+
let flush = if lastChunk then Stream.Finish else flushStrategy
530535
status <- Stream.deflate flush
531536

532537
case status of
533538
Stream.Ok -> do
534539
outputBufferFull <- Stream.outputBufferFull
535-
if outputBufferFull
540+
if outputBufferFull || flushStrategy /= Stream.NoFlush
536541
then do (outFPtr, offset, length) <- Stream.popOutputBuffer
537542
let chunk = S.PS outFPtr offset length
538543
return $ CompressOutputAvailable chunk $ do
@@ -596,7 +601,7 @@ decompressIO format params = decompressStreamIO format params
596601
decompressStream :: Stream.Format -> DecompressParams
597602
-> Bool -> S.ByteString
598603
-> Stream (DecompressStream Stream)
599-
decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
604+
decompressStream format (DecompressParams bits initChunkSize mdict allMembers flushStrategy)
600605
resume =
601606

602607
\chunk -> do
@@ -675,12 +680,12 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
675680
-- this invariant guarantees we can always make forward progress or at
676681
-- least if a BufferError does occur that it must be due to a premature EOF
677682

678-
status <- Stream.inflate Stream.NoFlush
683+
status <- Stream.inflate flushStrategy
679684

680685
case status of
681686
Stream.Ok -> do
682687
outputBufferFull <- Stream.outputBufferFull
683-
if outputBufferFull
688+
if outputBufferFull || flushStrategy /= Stream.NoFlush
684689
then do (outFPtr, offset, length) <- Stream.popOutputBuffer
685690
let chunk = S.PS outFPtr offset length
686691
return $ DecompressOutputAvailable chunk $ do

Codec/Compression/Zlib/Stream.hsc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ data Flush =
568568
| FullFlush
569569
| Finish
570570
-- | Block -- only available in zlib 1.2 and later, uncomment if you need it.
571+
deriving (Show, Eq)
571572

572573
fromFlush :: Flush -> CInt
573574
fromFlush NoFlush = #{const Z_NO_FLUSH}

test/Test/Codec/Compression/Zlib/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ instance Arbitrary CompressParams where
1616
`ap` arbitrary `ap` arbitrary
1717
`ap` arbitrary `ap` arbitraryBufferSize
1818
`ap` return Nothing
19+
`ap` arbitrary
1920

2021
arbitraryBufferSize :: Gen Int
2122
arbitraryBufferSize = frequency $ [(10, return n) | n <- [1..1024]] ++
@@ -29,4 +30,5 @@ instance Arbitrary DecompressParams where
2930
`ap` arbitraryBufferSize
3031
`ap` return Nothing
3132
`ap` arbitrary
33+
`ap` arbitrary
3234

test/Test/Codec/Compression/Zlib/Stream.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ instance Arbitrary Format where
1616
instance Arbitrary Method where
1717
arbitrary = return deflateMethod
1818

19+
instance Arbitrary Flush where
20+
arbitrary = elements [NoFlush]
21+
-- SyncFlush, Finish, FullFlush
1922

2023
instance Arbitrary CompressionLevel where
2124
arbitrary = elements $ [defaultCompression, noCompression,

0 commit comments

Comments
 (0)