Permalink
Browse files

gzip/ungzip shorthand. Factor out callback.

  • Loading branch information...
1 parent 3dd0430 commit c0d092f7cefb3f2494a881e742accdca1e1b705b @maltem committed Mar 24, 2011
Showing with 34 additions and 31 deletions.
  1. +18 −31 Codec/Zlib/Enum.hs
  2. +16 −0 test.hs
View
49 Codec/Zlib/Enum.hs
@@ -1,6 +1,6 @@
module Codec.Zlib.Enum (
-- * Enumeratees
- compress, decompress,
+ compress, decompress, gzip, ungzip,
-- * Re-exported from zlib-bindings
WindowBits (..), defaultWindowBits
) where
@@ -10,8 +10,14 @@ import Data.Enumerator as E
import qualified Data.Enumerator.List as EL
import Control.Monad.Trans (MonadIO, liftIO, lift)
import Data.ByteString (ByteString)
-import Control.Monad (join)
+-- | Gzip compression with default parameters.
+gzip :: MonadIO m => Enumeratee ByteString ByteString m a
+gzip = compress 1 defaultWindowBits
+
+-- | Gzip decompression with default parameters.
+ungzip :: MonadIO m => Enumeratee ByteString ByteString m a
+ungzip = decompress defaultWindowBits
-- |
-- Decompress (inflate) a stream of 'ByteString's. For example:
@@ -34,15 +40,9 @@ decompress' inf (Continue k) = do
chunk <- liftIO $ finishInflate inf
lift $ runIteratee $ k $ Chunks [chunk]
Just bs -> do
- chunks <- liftIO $ withInflateInput inf bs $ go id
+ chunks <- liftIO $ withInflateInput inf bs $ callback
step <- lift $ runIteratee $ k $ Chunks chunks
decompress' inf step
- where
- go front pop = do
- x <- pop
- case x of
- Nothing -> return $ front []
- Just y -> go (front . (:) y) pop
decompress' _ step = return step
-- |
@@ -63,31 +63,18 @@ compress' def (Continue k) = do
x <- EL.head
case x of
Nothing -> do
- chunks <- liftIO $ finishDeflate def $ go id
+ chunks <- liftIO $ finishDeflate def $ callback
lift $ runIteratee $ k $ Chunks chunks
Just bs -> do
- chunks <- liftIO $ withDeflateInput def bs $ go id
+ chunks <- liftIO $ withDeflateInput def bs $ callback
step <- lift $ runIteratee $ k $ Chunks chunks
compress' def step
- where
- go front pop = do
- x <- pop
- case x of
- Nothing -> return $ front []
- Just y -> go (front . (:) y) pop
compress' _ step = return step
-
--- testInflate = do
--- h <- openBinaryFile "test-out" WriteMode
--- run $ enumFile "test.z"
--- $$ decompress defaultWindowBits
--- $$ iterHandle h
--- hClose h
---
--- testDeflate = do
--- h <- openBinaryFile "test.z" WriteMode
--- run $ enumFile "test"
--- $$ compress 7 defaultWindowBits
--- $$ iterHandle h
--- hClose h
+callback :: Monad m => m (Maybe a) -> m [a]
+callback pop = go id where
+ go front = do
+ x <- pop
+ case x of
+ Nothing -> return $ front []
+ Just y -> go (front . (:) y)
View
16 test.hs
@@ -126,6 +126,14 @@ compressDecompress win xs =
$$ joinI $ Z.decompress win
$$ consume
+-- | The same, but for 'gzip', 'ungzip'
+gzipUngzip :: MonadIO m => [ByteString] -> m ByteString
+gzipUngzip xs =
+ E.run_ $ E.enumList 1 xs
+ $$ joinI $ Z.gzip
+ $$ joinI $ Z.ungzip
+ $$ consume
+
-- | Compress and decompress a ByteString with given WindowBits,
-- piping the stream with an Enumeratee.
compressDecompressWith
@@ -242,6 +250,13 @@ prop_compress_decompress' win xs = monadicIO $ do
ys <- Q.run $ compressDecompress win xs
assert (B.concat xs == ys)
+-- | Check: bs == gzipUngzip bs
+-- (just to see if the default parameters aren't broken)
+prop_gzip_ungzip :: [ByteString] -> Property
+prop_gzip_ungzip xs = monadicIO $ do
+ ys <- Q.run $ gzipUngzip xs
+ assert (B.concat xs == ys)
+
-- | Check if using an Iteratee that consumes only a few bytes works
prop_unconsumed :: WindowBits -> [ByteString] -> Property
prop_unconsumed win xs = monadicIO $ do
@@ -296,6 +311,7 @@ tests = let testFile = "zlib-enum-test-file" in
, testGroup "enumList"
[ testProperty "compress_decompress" prop_compress_decompress
, testProperty "compress_decompress'" prop_compress_decompress'
+ , testProperty "gzip_ungzip" prop_gzip_ungzip
, testProperty "unconsumed" prop_unconsumed
, testProperty "map_id" prop_map_id
, testProperty "map_revrev" prop_map_revrev

0 comments on commit c0d092f

Please sign in to comment.