module Streaming.Zlib
(
decompressStream
, decompressStream'
, compressStream
, compressStream'
, CompressionLevel
, WindowBits
, defaultWindowBits
, defaultCompressionLevel)
where
import Codec.Picture.Png.Streaming.Util
import Data.Streaming.Zlib (Popper, PopperRes (PRDone, PRNext, PRError),
WindowBits,
defaultWindowBits)
import qualified Data.Streaming.Zlib as Z
import Control.Monad.Catch (MonadThrow (..))
import Control.Monad.IO.Class (MonadIO (..))
import Data.ByteString.Streaming (ByteString)
import qualified Data.ByteString.Streaming as Q
exhaustPopper
:: (MonadIO m, MonadThrow m)
=> Popper
-> ByteString m ()
exhaustPopper popper = go
where
go = do pres <- liftIO popper
case pres of
PRDone -> return ()
PRNext bs -> do Q.chunk bs; go
PRError e -> throwM e
decompressStream'
:: (MonadIO m, MonadThrow m)
=> WindowBits
-> ByteString m r
-> ByteString m r
decompressStream' windowBits input =
do inflate <- liftIO $ Z.initInflate windowBits
let build bs =
do mchnk <- Q.nextChunk bs
case mchnk of
Left r -> return (Left r)
Right (chnk, bs') ->
do popper <- liftIO $ Z.feedInflate inflate chnk
return (Right (exhaustPopper popper, bs'))
res <- buildByteString build input
remaining <- liftIO $ Z.finishInflate inflate
Q.chunk remaining
return res
decompressStream
:: (MonadIO m, MonadThrow m)
=> ByteString m r
-> ByteString m r
decompressStream = decompressStream' defaultWindowBits
type CompressionLevel = Int
defaultCompressionLevel :: CompressionLevel
defaultCompressionLevel = 6
compressStream'
:: (MonadIO m, MonadThrow m)
=> CompressionLevel
-> WindowBits
-> ByteString m r
-> ByteString m r
compressStream' compressLevel windowBits input =
do deflate <- liftIO $ Z.initDeflate compressLevel windowBits
let build bs =
do mchnk <- Q.nextChunk bs
case mchnk of
Left r -> return (Left r)
Right (chnk, bs') ->
do popper <- liftIO $ Z.feedDeflate deflate chnk
return (Right (exhaustPopper popper, bs'))
res <- buildByteString build input
exhaustPopper $ Z.finishDeflate deflate
return res
compressStream
:: (MonadIO m, MonadThrow m)
=> ByteString m r
-> ByteString m r
compressStream = compressStream' defaultCompressionLevel defaultWindowBits