Skip to content

Commit ebbeb7e

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 07a6885 commit ebbeb7e

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,
@@ -105,7 +106,8 @@ data CompressParams = CompressParams {
105106
compressMemoryLevel :: !Stream.MemoryLevel,
106107
compressStrategy :: !Stream.CompressionStrategy,
107108
compressBufferSize :: !Int,
108-
compressDictionary :: Maybe S.ByteString
109+
compressDictionary :: Maybe S.ByteString,
110+
compressFlush :: !Stream.Flush
109111
} deriving Show
110112

111113
-- | The full set of parameters for decompression. The defaults are
@@ -131,7 +133,8 @@ data DecompressParams = DecompressParams {
131133
decompressWindowBits :: !Stream.WindowBits,
132134
decompressBufferSize :: !Int,
133135
decompressDictionary :: Maybe S.ByteString,
134-
decompressAllMembers :: Bool
136+
decompressAllMembers :: Bool,
137+
decompressFlush :: !Stream.Flush
135138
} deriving Show
136139

137140
-- | The default set of parameters for compression. This is typically used with
@@ -145,7 +148,8 @@ defaultCompressParams = CompressParams {
145148
compressMemoryLevel = Stream.defaultMemoryLevel,
146149
compressStrategy = Stream.defaultStrategy,
147150
compressBufferSize = defaultCompressBufferSize,
148-
compressDictionary = Nothing
151+
compressDictionary = Nothing,
152+
compressFlush = Stream.NoFlush
149153
}
150154

151155
-- | The default set of parameters for decompression. This is typically used with
@@ -156,7 +160,8 @@ defaultDecompressParams = DecompressParams {
156160
decompressWindowBits = Stream.defaultWindowBits,
157161
decompressBufferSize = defaultDecompressBufferSize,
158162
decompressDictionary = Nothing,
159-
decompressAllMembers = True
163+
decompressAllMembers = True,
164+
decompressFlush = Stream.NoFlush
160165
}
161166

162167
-- | The default chunk sizes for the output of compression and decompression
@@ -460,7 +465,7 @@ compressIO format params = compressStreamIO format params
460465
compressStream :: Stream.Format -> CompressParams -> S.ByteString
461466
-> Stream (CompressStream Stream)
462467
compressStream format (CompressParams compLevel method bits memLevel
463-
strategy initChunkSize mdict) =
468+
strategy initChunkSize mdict flushStrategy) =
464469

465470
\chunk -> do
466471
Stream.deflateInit format compLevel method bits memLevel strategy
@@ -520,13 +525,13 @@ compressStream format (CompressParams compLevel method bits memLevel
520525
-- this invariant guarantees we can always make forward progress
521526
-- and that therefore a BufferError is impossible
522527

523-
let flush = if lastChunk then Stream.Finish else Stream.NoFlush
528+
let flush = if lastChunk then Stream.Finish else flushStrategy
524529
status <- Stream.deflate flush
525530

526531
case status of
527532
Stream.Ok -> do
528533
outputBufferFull <- Stream.outputBufferFull
529-
if outputBufferFull
534+
if outputBufferFull || flushStrategy /= Stream.NoFlush
530535
then do (outFPtr, offset, length) <- Stream.popOutputBuffer
531536
let chunk = S.PS outFPtr offset length
532537
return $ CompressOutputAvailable chunk $ do
@@ -590,7 +595,7 @@ decompressIO format params = decompressStreamIO format params
590595
decompressStream :: Stream.Format -> DecompressParams
591596
-> Bool -> S.ByteString
592597
-> Stream (DecompressStream Stream)
593-
decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
598+
decompressStream format (DecompressParams bits initChunkSize mdict allMembers flushStrategy)
594599
resume =
595600

596601
\chunk -> do
@@ -669,12 +674,12 @@ decompressStream format (DecompressParams bits initChunkSize mdict allMembers)
669674
-- this invariant guarantees we can always make forward progress or at
670675
-- least if a BufferError does occur that it must be due to a premature EOF
671676

672-
status <- Stream.inflate Stream.NoFlush
677+
status <- Stream.inflate flushStrategy
673678

674679
case status of
675680
Stream.Ok -> do
676681
outputBufferFull <- Stream.outputBufferFull
677-
if outputBufferFull
682+
if outputBufferFull || flushStrategy /= Stream.NoFlush
678683
then do (outFPtr, offset, length) <- Stream.popOutputBuffer
679684
let chunk = S.PS outFPtr offset length
680685
return $ DecompressOutputAvailable chunk $ do

Codec/Compression/Zlib/Stream.hsc

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

571572
fromFlush :: Flush -> CInt
572573
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)