#include "inline.hs"
module Streamly.Internal.FileSystem.File
(
withFile
, read
, readChunksWith
, readChunks
, readerWith
, reader
, chunkReaderWith
, chunkReaderFromToWith
, chunkReader
, putChunk
, write
, writeWith
, writeChunks
, fromBytes
, fromBytesWith
, fromChunks
, append
, appendWith
, appendArray
, appendChunks
, readWithBufferOf
, readChunksWithBufferOf
, readChunksFromToWith
, toBytes
, toChunks
, toChunksWithBufferOf
, writeWithBufferOf
, fromBytesWithBufferOf
)
where
import Control.Monad.Catch (MonadCatch)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import System.IO (Handle, openFile, IOMode(..), hClose)
import Prelude hiding (read)
import qualified Control.Monad.Catch as MC
import qualified System.IO as SIO
import Streamly.Data.Fold (groupsOf, drain)
import Streamly.Internal.Data.Array.Type (Array(..), writeNUnsafe)
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Data.Stream (Stream)
import Streamly.Internal.Data.Unboxed (Unbox)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (defaultChunkSize)
import qualified Streamly.Data.Array as A
import qualified Streamly.Data.Stream as S
import qualified Streamly.Data.Unfold as UF
import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
import qualified Streamly.Internal.Data.Fold.Type as FL
(Step(..), snoc, reduce)
import qualified Streamly.Internal.FileSystem.Handle as FH
{-# INLINE withFile #-}
withFile :: (MonadIO m, MonadCatch m)
=> FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile :: FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
mode = IO Handle
-> (Handle -> IO ()) -> (Handle -> Stream m a) -> Stream m a
forall (m :: * -> *) b c a.
(MonadIO m, MonadCatch m) =>
IO b -> (b -> IO c) -> (b -> Stream m a) -> Stream m a
S.bracketIO (FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
mode) Handle -> IO ()
hClose
{-# INLINE usingFile #-}
usingFile :: (MonadIO m, MonadCatch m)
=> Unfold m Handle a -> Unfold m FilePath a
usingFile :: Unfold m Handle a -> Unfold m FilePath a
usingFile = (FilePath -> IO Handle)
-> (Handle -> IO ()) -> Unfold m Handle a -> Unfold m FilePath a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (FilePath -> IOMode -> IO Handle
`openFile` IOMode
ReadMode) Handle -> IO ()
hClose
{-# INLINE usingFile2 #-}
usingFile2 :: (MonadIO m, MonadCatch m)
=> Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 :: Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 = ((x, FilePath) -> IO (x, Handle))
-> ((x, Handle) -> IO ())
-> Unfold m (x, Handle) a
-> Unfold m (x, FilePath) a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (x, FilePath) -> IO (x, Handle)
forall a. (a, FilePath) -> IO (a, Handle)
before (x, Handle) -> IO ()
forall a. (a, Handle) -> IO ()
after
where
before :: (a, FilePath) -> IO (a, Handle)
before (a
x, FilePath
file) = do
Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
ReadMode
(a, Handle) -> IO (a, Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, Handle
h)
after :: (a, Handle) -> IO ()
after (a
_, Handle
h) = Handle -> IO ()
hClose Handle
h
{-# INLINE usingFile3 #-}
usingFile3 :: (MonadIO m, MonadCatch m)
=> Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 :: Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 = ((x, y, z, FilePath) -> IO (x, y, z, Handle))
-> ((x, y, z, Handle) -> IO ())
-> Unfold m (x, y, z, Handle) a
-> Unfold m (x, y, z, FilePath) a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (x, y, z, FilePath) -> IO (x, y, z, Handle)
forall a b c. (a, b, c, FilePath) -> IO (a, b, c, Handle)
before (x, y, z, Handle) -> IO ()
forall a b c. (a, b, c, Handle) -> IO ()
after
where
before :: (a, b, c, FilePath) -> IO (a, b, c, Handle)
before (a
x, b
y, c
z, FilePath
file) = do
Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
ReadMode
(a, b, c, Handle) -> IO (a, b, c, Handle)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, b
y, c
z, Handle
h)
after :: (a, b, c, Handle) -> IO ()
after (a
_, b
_, c
_, Handle
h) = Handle -> IO ()
hClose Handle
h
{-# INLINABLE putChunk #-}
putChunk :: FilePath -> Array a -> IO ()
putChunk :: FilePath -> Array a -> IO ()
putChunk FilePath
file Array a
arr = FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
SIO.withFile FilePath
file IOMode
WriteMode (Handle -> Array a -> IO ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
`FH.putChunk` Array a
arr)
{-# INLINABLE appendArray #-}
appendArray :: FilePath -> Array a -> IO ()
appendArray :: FilePath -> Array a -> IO ()
appendArray FilePath
file Array a
arr = FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
SIO.withFile FilePath
file IOMode
AppendMode (Handle -> Array a -> IO ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
`FH.putChunk` Array a
arr)
{-# INLINE readChunksWith #-}
readChunksWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m (Array Word8)
readChunksWith :: Int -> FilePath -> Stream m (Array Word8)
readChunksWith Int
size FilePath
file =
FilePath
-> IOMode
-> (Handle -> Stream m (Array Word8))
-> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
ReadMode (Int -> Handle -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Stream m (Array Word8)
FH.readChunksWith Int
size)
{-# DEPRECATED toChunksWithBufferOf "Please use 'readChunksWith' instead" #-}
{-# INLINE toChunksWithBufferOf #-}
toChunksWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m (Array Word8)
toChunksWithBufferOf :: Int -> FilePath -> Stream m (Array Word8)
toChunksWithBufferOf = Int -> FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
readChunksWith
{-# INLINE readChunks #-}
readChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array Word8)
readChunks :: FilePath -> Stream m (Array Word8)
readChunks = Int -> FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
readChunksWith Int
defaultChunkSize
{-# DEPRECATED toChunks "Please use 'readChunks' instead" #-}
{-# INLINE toChunks #-}
toChunks :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Array Word8)
toChunks :: FilePath -> Stream m (Array Word8)
toChunks = FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array Word8)
readChunks
{-# INLINE chunkReaderWith #-}
chunkReaderWith :: (MonadIO m, MonadCatch m)
=> Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith :: Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith = Unfold m (Int, Handle) (Array Word8)
-> Unfold m (Int, FilePath) (Array Word8)
forall (m :: * -> *) x a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 Unfold m (Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Handle) (Array Word8)
FH.chunkReaderWith
{-# DEPRECATED readChunksWithBufferOf
"Please use 'chunkReaderWith' instead" #-}
{-# INLINE readChunksWithBufferOf #-}
readChunksWithBufferOf :: (MonadIO m, MonadCatch m)
=> Unfold m (Int, FilePath) (Array Word8)
readChunksWithBufferOf :: Unfold m (Int, FilePath) (Array Word8)
readChunksWithBufferOf = Unfold m (Int, FilePath) (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith
{-# INLINE chunkReaderFromToWith #-}
chunkReaderFromToWith :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith :: Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith = Unfold m (Int, Int, Int, Handle) (Array Word8)
-> Unfold m (Int, Int, Int, FilePath) (Array Word8)
forall (m :: * -> *) x y z a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 Unfold m (Int, Int, Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Int, Int, Handle) (Array Word8)
FH.chunkReaderFromToWith
{-# DEPRECATED readChunksFromToWith
"Please use 'chunkReaderFromToWith' instead" #-}
{-# INLINE readChunksFromToWith #-}
readChunksFromToWith :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
readChunksFromToWith :: Unfold m (Int, Int, Int, FilePath) (Array Word8)
readChunksFromToWith = Unfold m (Int, Int, Int, FilePath) (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith
{-# INLINE chunkReader #-}
chunkReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath (Array Word8)
chunkReader :: Unfold m FilePath (Array Word8)
chunkReader = Unfold m Handle (Array Word8) -> Unfold m FilePath (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Unfold m Handle a -> Unfold m FilePath a
usingFile Unfold m Handle (Array Word8)
forall (m :: * -> *). MonadIO m => Unfold m Handle (Array Word8)
FH.chunkReader
{-# INLINE readerWith #-}
readerWith :: (MonadIO m, MonadCatch m) => Unfold m (Int, FilePath) Word8
readerWith :: Unfold m (Int, FilePath) Word8
readerWith = Unfold m (Int, Handle) Word8 -> Unfold m (Int, FilePath) Word8
forall (m :: * -> *) x a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 Unfold m (Int, Handle) Word8
forall (m :: * -> *). MonadIO m => Unfold m (Int, Handle) Word8
FH.readerWith
{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead" #-}
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readWithBufferOf :: Unfold m (Int, FilePath) Word8
readWithBufferOf = Unfold m (Int, FilePath) Word8
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readerWith
{-# INLINE reader #-}
reader :: (MonadIO m, MonadCatch m) => Unfold m FilePath Word8
reader :: Unfold m FilePath Word8
reader = Unfold m (Array Word8) Word8
-> Unfold m FilePath (Array Word8) -> Unfold m FilePath Word8
forall (m :: * -> *) b c a.
Monad m =>
Unfold m b c -> Unfold m a b -> Unfold m a c
UF.many Unfold m (Array Word8) Word8
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader (Unfold m Handle (Array Word8) -> Unfold m FilePath (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Unfold m Handle a -> Unfold m FilePath a
usingFile Unfold m Handle (Array Word8)
forall (m :: * -> *). MonadIO m => Unfold m Handle (Array Word8)
FH.chunkReader)
{-# INLINE concatChunks #-}
concatChunks :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
concatChunks :: Stream m (Array a) -> Stream m a
concatChunks = Unfold m (Array a) a -> Stream m (Array a) -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
S.unfoldMany Unfold m (Array a) a
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader
{-# INLINE read #-}
read :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
read :: FilePath -> Stream m Word8
read FilePath
file = Stream m (Array Word8) -> Stream m Word8
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
concatChunks (Stream m (Array Word8) -> Stream m Word8)
-> Stream m (Array Word8) -> Stream m Word8
forall a b. (a -> b) -> a -> b
$ FilePath
-> IOMode
-> (Handle -> Stream m (Array Word8))
-> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
ReadMode Handle -> Stream m (Array Word8)
forall (m :: * -> *). MonadIO m => Handle -> Stream m (Array Word8)
FH.readChunks
{-# DEPRECATED toBytes "Please use 'read' instead" #-}
{-# INLINE toBytes #-}
toBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
toBytes :: FilePath -> Stream m Word8
toBytes = FilePath -> Stream m Word8
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8
read
{-# INLINE fromChunksMode #-}
fromChunksMode :: (MonadIO m, MonadCatch m)
=> IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode :: IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
mode FilePath
file Stream m (Array a)
xs = Fold m () () -> Stream m () -> m ()
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
S.fold Fold m () ()
forall (m :: * -> *) a. Monad m => Fold m a ()
drain (Stream m () -> m ()) -> Stream m () -> m ()
forall a b. (a -> b) -> a -> b
$
FilePath -> IOMode -> (Handle -> Stream m ()) -> Stream m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
mode (\Handle
h -> (Array a -> m ()) -> Stream m (Array a) -> Stream m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
S.mapM (Handle -> Array a -> m ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
FH.putChunk Handle
h) Stream m (Array a)
xs)
{-# INLINE fromChunks #-}
fromChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array a) -> m ()
fromChunks :: FilePath -> Stream m (Array a) -> m ()
fromChunks = IOMode -> FilePath -> Stream m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
WriteMode
{-# INLINE fromBytesWith #-}
fromBytesWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith :: Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith Int
n FilePath
file Stream m Word8
xs = FilePath -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
fromChunks FilePath
file (Stream m (Array Word8) -> m ()) -> Stream m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Stream m Word8 -> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
S.chunksOf Int
n Stream m Word8
xs
{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
{-# INLINE fromBytesWithBufferOf #-}
fromBytesWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWithBufferOf :: Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWithBufferOf = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith
{-# INLINE fromBytes #-}
fromBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
fromBytes :: FilePath -> Stream m Word8 -> m ()
fromBytes = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith Int
defaultChunkSize
{-# INLINE writeChunks #-}
writeChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Fold m (Array a) ()
writeChunks :: FilePath -> Fold m (Array a) ()
writeChunks FilePath
path = ((Fold m (Array a) (), Handle)
-> Array a -> m (Step (Fold m (Array a) (), Handle) ()))
-> m (Step (Fold m (Array a) (), Handle) ())
-> ((Fold m (Array a) (), Handle) -> m ())
-> Fold m (Array a) ()
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> Fold m a b
Fold (Fold m (Array a) (), Handle)
-> Array a -> m (Step (Fold m (Array a) (), Handle) ())
forall (m :: * -> *) a b b.
(MonadCatch m, MonadIO m) =>
(Fold m a b, Handle) -> a -> m (Step (Fold m a b, Handle) b)
step m (Step (Fold m (Array a) (), Handle) ())
forall a b. m (Step (Fold m (Array a) (), Handle) b)
initial (Fold m (Array a) (), Handle) -> m ()
forall (m :: * -> *) a b. MonadIO m => (Fold m a b, Handle) -> m b
extract
where
initial :: m (Step (Fold m (Array a) (), Handle) b)
initial = do
Handle
h <- IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IOMode -> IO Handle
openFile FilePath
path IOMode
WriteMode)
Fold m (Array a) ()
fld <- Fold m (Array a) () -> m (Fold m (Array a) ())
forall (m :: * -> *) a b. Monad m => Fold m a b -> m (Fold m a b)
FL.reduce (Handle -> Fold m (Array a) ()
forall (m :: * -> *) a. MonadIO m => Handle -> Fold m (Array a) ()
FH.writeChunks Handle
h)
m (Fold m (Array a) ()) -> m () -> m (Fold m (Array a) ())
forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO ()
hClose Handle
h)
Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b))
-> Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b)
forall a b. (a -> b) -> a -> b
$ (Fold m (Array a) (), Handle)
-> Step (Fold m (Array a) (), Handle) b
forall s b. s -> Step s b
FL.Partial (Fold m (Array a) ()
fld, Handle
h)
step :: (Fold m a b, Handle) -> a -> m (Step (Fold m a b, Handle) b)
step (Fold m a b
fld, Handle
h) a
x = do
Fold m a b
r <- Fold m a b -> a -> m (Fold m a b)
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> a -> m (Fold m a b)
FL.snoc Fold m a b
fld a
x m (Fold m a b) -> m () -> m (Fold m a b)
forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO ()
hClose Handle
h)
Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b))
-> Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b)
forall a b. (a -> b) -> a -> b
$ (Fold m a b, Handle) -> Step (Fold m a b, Handle) b
forall s b. s -> Step s b
FL.Partial (Fold m a b
r, Handle
h)
extract :: (Fold m a b, Handle) -> m b
extract (Fold s -> a -> m (Step s b)
_ m (Step s b)
initial1 s -> m b
extract1, Handle
h) = do
IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hClose Handle
h
Step s b
res <- m (Step s b)
initial1
case Step s b
res of
FL.Partial s
fs -> s -> m b
extract1 s
fs
FL.Done b
fb -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
fb
{-# INLINE writeWith #-}
writeWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Fold m Word8 ()
writeWith :: Int -> FilePath -> Fold m Word8 ()
writeWith Int
n FilePath
path =
Int
-> Fold m Word8 (Array Word8)
-> Fold m (Array Word8) ()
-> Fold m Word8 ()
forall (m :: * -> *) a b c.
Monad m =>
Int -> Fold m a b -> Fold m b c -> Fold m a c
groupsOf Int
n (Int -> Fold m Word8 (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
writeNUnsafe Int
n) (FilePath -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Fold m (Array a) ()
writeChunks FilePath
path)
{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
{-# INLINE writeWithBufferOf #-}
writeWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Fold m Word8 ()
writeWithBufferOf :: Int -> FilePath -> Fold m Word8 ()
writeWithBufferOf = Int -> FilePath -> Fold m Word8 ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWith
{-# INLINE write #-}
write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 ()
write :: FilePath -> Fold m Word8 ()
write = Int -> FilePath -> Fold m Word8 ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWith Int
defaultChunkSize
{-# INLINE appendChunks #-}
appendChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array a) -> m ()
appendChunks :: FilePath -> Stream m (Array a) -> m ()
appendChunks = IOMode -> FilePath -> Stream m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
AppendMode
{-# INLINE appendWith #-}
appendWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
appendWith :: Int -> FilePath -> Stream m Word8 -> m ()
appendWith Int
n FilePath
file Stream m Word8
xs = FilePath -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
appendChunks FilePath
file (Stream m (Array Word8) -> m ()) -> Stream m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Stream m Word8 -> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
S.chunksOf Int
n Stream m Word8
xs
{-# INLINE append #-}
append :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
append :: FilePath -> Stream m Word8 -> m ()
append = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
appendWith Int
defaultChunkSize